使用 PyGobject 创建您自己的“Web 浏览器”和“桌面录音机”应用程序 - 第 3 部分
这是有关使用 PyGObject 在 Linux 桌面下创建 GUI 应用程序的系列的第 3 部分。今天我们将讨论在我们的程序中使用一些高级 Python 模块和库,例如“os”、“WebKit”、“requests”和其他,以及其他一些对编程有用的信息。
要求
您必须从这里开始阅读本系列之前的所有部分,才能继续了解有关创建更高级应用程序的进一步说明:
- 使用 PyGObject 在 Linux 桌面下创建 GUI 应用程序 – 第 1 部分
- 在 Linux 上创建高级 PyGobject 应用程序 - 第 2 部分
Python中的模块和库非常有用,您不必编写许多子程序来完成一些复杂的工作,这将花费大量的时间和工作,您只需导入它们即可!是的,只需将您需要的模块和库导入到您的程序中,您将能够节省大量时间和精力来完成您的程序。
Python 有许多著名的模块,您可以在 Python 模块索引中找到它们。
你也可以为你的Python程序导入库,从“gi.repository import Gtk”这一行将GTK库导入到Python程序中,还有许多其他库,如Gdk、WebKit等。
创建高级 GUI 应用程序
今天,我们将创建 2 个程序:
- 一个简单的网络浏览器;它将使用 WebKit 库。
- 使用“avconv”命令的桌面录音机;它将使用 Python 中的“os”模块。
从现在开始我不会解释如何在Glade设计器中拖放小部件,我只会告诉您需要创建的小部件的名称,另外我会给您.glade 文件,当然还有 Python 文件。
创建一个简单的网络浏览器
为了创建一个网络浏览器,我们必须使用“WebKit”引擎,它是一个开源的网络渲染引擎,与 中使用的引擎相同。 Chrome/Chromium,更多信息可以参考Webkit.org官方网站。
首先,我们必须创建 GUI,打开 Glade 设计器并添加以下小部件。有关如何创建小部件的更多信息,请参阅本系列的第 1 部分和第 2 部分(上面给出的链接)。
- 创建“window1”小部件。
- 创建“box1”和“box2”小部件。
- 创建“button1”和“button2”小部件。
- 创建“entry1”小部件。
- 创建“scrolledwindow1”小部件。
创建完小部件后,您将看到以下界面。
除了“滚动窗口”小部件之外,没有什么新内容;这个小部件非常重要,它可以让 WebKit 引擎植入其中,使用“滚动窗口”小部件,您还可以在浏览时水平和垂直滚动。浏览网站。
您现在必须将“backbutton_clicked”处理程序添加到Back按钮“clicked”信号、“refreshbutton_clicked”刷新按钮“clicked signal”的处理程序和条目“activated”信号的“enterkey_clicked”处理程序。
界面的完整 .glade 文件位于此处。
<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.16.1 -->
<interface>
<requires lib="gtk+" version="3.10"/>
<object class="GtkWindow" id="window1">
<property name="can_focus">False</property>
<property name="title" translatable="yes">Our Simple Browser</property>
<property name="window_position">center</property>
<property name="default_width">1000</property>
<property name="default_height">600</property>
<property name="icon_name">applications-internet</property>
<child>
<object class="GtkBox" id="box1">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="orientation">vertical</property>
<child>
<object class="GtkBox" id="box2">
<property name="visible">True</property>
<property name="can_focus">False</property>
<child>
<object class="GtkButton" id="button1">
<property name="label">gtk-go-back</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<property name="relief">half</property>
<property name="use_stock">True</property>
<property name="always_show_image">True</property>
<signal name="clicked" handler="backbutton_clicked" swapped="no"/>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkButton" id="button2">
<property name="label">gtk-refresh</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<property name="relief">half</property>
<property name="use_stock">True</property>
<property name="always_show_image">True</property>
<signal name="clicked" handler="refreshbutton_clicked" swapped="no"/>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">1</property>
</packing>
</child>
<child>
<object class="GtkEntry" id="entry1">
<property name="visible">True</property>
<property name="can_focus">True</property>
<signal name="activate" handler="enterkey_clicked" swapped="no"/>
</object>
<packing>
<property name="expand">True</property>
<property name="fill">True</property>
<property name="position">2</property>
</packing>
</child>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkScrolledWindow" id="scrolledwindow1">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="hscrollbar_policy">always</property>
<property name="shadow_type">in</property>
<child>
<placeholder/>
</child>
</object>
<packing>
<property name="expand">True</property>
<property name="fill">True</property>
<property name="position">1</property>
</packing>
</child>
</object>
</child>
</object>
</interface>
现在复制上述代码并将其粘贴到主文件夹中的“ui.glade”文件中。现在创建一个名为“mywebbrowser.py”的新文件,并在其中输入以下代码,所有解释都在注释中。
#!/usr/bin/python
-*- coding: utf-8 -*-
## Here we imported both Gtk library and the WebKit engine.
from gi.repository import Gtk, WebKit
class Handler:
def backbutton_clicked(self, button):
## When the user clicks on the Back button, the '.go_back()' method is activated, which will send the user to the previous page automatically, this method is part from the WebKit engine.
browserholder.go_back()
def refreshbutton_clicked(self, button):
## Same thing here, the '.reload()' method is activated when the 'Refresh' button is clicked.
browserholder.reload()
def enterkey_clicked(self, button):
## To load the URL automatically when the "Enter" key is hit from the keyboard while focusing on the entry box, we have to use the '.load_uri()' method and grab the URL from the entry box.
browserholder.load_uri(urlentry.get_text())
## Nothing new here.. We just imported the 'ui.glade' file.
builder = Gtk.Builder()
builder.add_from_file("ui.glade")
builder.connect_signals(Handler())
window = builder.get_object("window1")
## Here's the new part.. We created a global object called 'browserholder' which will contain the WebKit rendering engine, and we set it to 'WebKit.WebView()' which is the default thing to do if you want to add a WebKit engine to your program.
browserholder = WebKit.WebView()
## To disallow editing the webpage.
browserholder.set_editable(False)
## The default URL to be loaded, we used the 'load_uri()' method.
browserholder.load_uri("https://linux-console.net")
urlentry = builder.get_object("entry1")
urlentry.set_text("https://linux-console.net")
## Here we imported the scrolledwindow1 object from the ui.glade file.
scrolled_window = builder.get_object("scrolledwindow1")
## We used the '.add()' method to add the 'browserholder' object to the scrolled window, which contains our WebKit browser.
scrolled_window.add(browserholder)
## And finally, we showed the 'browserholder' object using the '.show()' method.
browserholder.show()
## Give that developer a cookie !
window.connect("delete-event", Gtk.main_quit)
window.show_all()
Gtk.main()
保存文件并运行它。
chmod 755 mywebbrowser.py
./mywebbrowser.py
这就是您将得到的。
您可以参考 WebKitGtk 官方文档以发现更多选项。
创建一个简单的桌面录音机
在本节中,我们将学习如何使用“os”模块从 Python 文件运行本地系统命令或 shell 脚本,这将帮助我们使用'avconv'命令。
打开 Glade 设计器,并创建以下小部件:
- 创建“window1”小部件。
- 创建“box1”小部件。
- 创建“button1”、“button2”和“button3”小部件。
- 创建“entry1”小部件。
创建完上述小部件后,您将看到以下界面。
这是完整的 ui.glade 文件。
<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.16.1 -->
<interface>
<requires lib="gtk+" version="3.10"/>
<object class="GtkWindow" id="window1">
<property name="can_focus">False</property>
<property name="title" translatable="yes">Our Simple Recorder</property>
<property name="window_position">center</property>
<property name="default_width">300</property>
<property name="default_height">30</property>
<property name="icon_name">applications-multimedia</property>
<child>
<object class="GtkBox" id="box1">
<property name="visible">True</property>
<property name="can_focus">False</property>
<child>
<object class="GtkEntry" id="entry1">
<property name="visible">True</property>
<property name="can_focus">True</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkButton" id="button1">
<property name="label">gtk-media-record</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<property name="use_stock">True</property>
<property name="always_show_image">True</property>
<signal name="clicked" handler="recordbutton" swapped="no"/>
</object>
<packing>
<property name="expand">True</property>
<property name="fill">True</property>
<property name="position">1</property>
</packing>
</child>
<child>
<object class="GtkButton" id="button2">
<property name="label">gtk-media-stop</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<property name="use_stock">True</property>
<property name="always_show_image">True</property>
<signal name="clicked" handler="stopbutton" swapped="no"/>
</object>
<packing>
<property name="expand">True</property>
<property name="fill">True</property>
<property name="position">2</property>
</packing>
</child>
<child>
<object class="GtkButton" id="button3">
<property name="label">gtk-media-play</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<property name="use_stock">True</property>
<property name="always_show_image">True</property>
<signal name="clicked" handler="playbutton" swapped="no"/>
</object>
<packing>
<property name="expand">True</property>
<property name="fill">True</property>
<property name="position">3</property>
</packing>
</child>
</object>
</child>
</object>
</interface>
像往常一样,复制上面的代码并将其粘贴到主目录中的文件“ui.glade”中,创建一个新的“myrecorder.py”文件并输入以下内容其中的代码(每一个新行都在注释中进行了解释)。
#!/usr/bin/python
-*- coding: utf-8 -*-
## Here we imported both Gtk library and the os module.
from gi.repository import Gtk
import os
class Handler:
def recordbutton(self, button):
## We defined a variable: 'filepathandname', we assigned the bash local variable '$HOME' to it + "/" + the file name from the text entry box.
filepathandname = os.environ["HOME"] + "/" + entry.get_text()
## Here exported the 'filepathandname' variable from Python to the 'filename' variable in the shell.
os.environ["filename"] = filepathandname
## Using 'os.system(COMMAND)' we can execute any shell command or shell script, here we executed the 'avconv' command to record the desktop video & audio.
os.system("avconv -f x11grab -r 25 -s `xdpyinfo | grep 'dimensions:'|awk '{print $2}'` -i :0.0 -vcodec libx264 -threads 4 $filename -y & ")
def stopbutton(self, button):
## Run the 'killall avconv' command when the stop button is clicked.
os.system("killall avconv")
def playbutton(self, button):
## Run the 'avplay' command in the shell to play the recorded file when the play button is clicked.
os.system("avplay $filename &")
## Nothing new here.. We just imported the 'ui.glade' file.
builder = Gtk.Builder()
builder.add_from_file("ui.glade")
builder.connect_signals(Handler())
window = builder.get_object("window1")
entry = builder.get_object("entry1")
entry.set_text("myrecording-file.avi")
## Give that developer a cookie !
window.connect("delete-event", Gtk.main_quit)
window.show_all()
Gtk.main()
现在通过在终端中应用以下命令来运行该文件。
chmod 755 myrecorder.py
./myrecorder.py
这样你就得到了你的第一台桌面录音机。
您可以在 Python OS 库中找到有关“os”模块的更多信息。
就是这样,使用 PyGObject 创建 Linux 桌面应用程序并不难,您只需创建 GUI、导入一些模块并将 Python 文件与 GUI 链接起来,仅此而已。 PyGObject 网站上有许多关于执行此操作的有用教程:
您是否尝试过使用 PyGObject 创建应用程序?你觉得这样做怎么样?您之前开发过哪些应用程序?