网站搜索

在 Linux 中使用 PyGobject 工具创建更高级的 GUI 应用程序 - 第 2 部分


我们继续关于使用 PyGObject 在 Linux 桌面下创建 GUI 应用程序的系列,这是该系列的第二部分,今天我们将讨论使用一些高级小部件创建更多功能的应用程序。

要求

  1. 使用 PyGObject 在 Linux 下创建 GUI 应用程序 – 第 1 部分

在上一篇文章中我们说过使用 PyGObject 创建 GUI 应用程序有两种方法:code-only-wayGlade 设计器方式,但从现在开始,我们将只解释 Glade 设计器方式,因为它对大多数用户来说更容易,您可以使用 python-gtk3-tutorial 自己学习仅代码方式。

在 Linux 中创建高级 GUI 应用程序

1.让我们开始编程吧!从应用程序菜单中打开 Glade 设计器。

2. 单击左侧边栏上的“窗口”按钮以创建一个新窗口。

3. 单击“Box”小部件并将其释放到空窗口上。

4.系统将提示您输入所需的盒子数量,将其设置为3

您将看到创建了,这些框对我们来说非常重要,以便能够在窗口中添加不仅仅是1小部件。

5. 现在单击box小部件,并将方向类型从垂直更改为水平

6. 为了创建一个简单的程序,添加一个“文本输入”、“组合框文本”和一个“按钮 ” 每个盒子的小部件,你应该有这样的东西。

7. 现在单击右侧边栏中的“window1”小部件,并将其位置更改为“Center”。

向下滚动到“外观”部分。并为窗口“我的程序”添加标题。

8. 您还可以通过单击“图标名称”框为窗口选择图标

9.您还可以更改应用程序的默认高度宽度。完成所有这些之后,您应该有这样的东西。

在任何程序中,最重要的事情之一就是创建一个“关于”窗口,为此,首先我们必须将之前创建的普通按钮更改为库存按钮,看看图片。

10. 现在,我们必须修改一些信号,以便在小部件上发生任何事件时运行特定操作。单击文本输入小部件,切换到右侧边栏中的“信号”选项卡,搜索“激活”并将其处理程序更改为“enter_button_clicked”,“activated”信号是当聚焦于文本时按下“Enter”键时发送的默认信号条目小部件。

我们必须为“关于”按钮小部件的“clicked”信号添加另一个处理程序,单击它并将“clicked”信号更改为“button_is_clicked” “。

11. 转到“Common”选项卡并标记“Has Focus”,如下所示(为“关于”按钮提供默认焦点而不是条目)。

12. 现在,从左侧边栏创建一个新的“关于对话框”窗口。

您会注意到“关于对话框”窗口已创建。

让我们修改它。确保从右侧边栏插入以下设置。

完成上述设置后,您将看到有关 Window 的信息。

在上面的窗口中,您会注意到空白区域,但您可以通过将框的数量从 3 个减少到 2 个来删除它,或者如果需要,您可以向其中添加任何小部件。

13. 现在将文件保存在您的主文件夹中,名称为“ui.glade”,然后打开文本编辑器并在其中输入以下代码。


#!/usr/bin/python
-*- coding: utf-8 -*-

from gi.repository import Gtk
class Handler:

    def button_is_clicked(self, button):
        ## The ".run()" method is used to launch the about window.
         ouraboutwindow.run()
        ## This is just a workaround to enable closing the about window.
         ouraboutwindow.hide()

    def enter_button_clicked(self, button):
        ## The ".get_text()" method is used to grab the text from the entry box. The "get_active_text()" method is used to get the selected item from the Combo Box Text widget, here, we merged both texts together".
         print ourentry.get_text() + ourcomboboxtext.get_active_text()

## Nothing new here.. We just imported the 'ui.glade' file.
builder = Gtk.Builder()
builder.add_from_file("ui.glade")
builder.connect_signals(Handler())

ournewbutton = builder.get_object("button1")

window = builder.get_object("window1")

## Here we imported the Combo Box widget in order to add some change on it.
ourcomboboxtext = builder.get_object("comboboxtext1")

## Here we defined a list called 'default_text' which will contain all the possible items in the Combo Box Text widget.
default_text = [" World ", " Earth ", " All "]

## This is a for loop that adds every single item of the 'default_text' list to the Combo Box Text widget using the '.append_text()' method.
for x in default_text:
  ourcomboboxtext.append_text(x)

## The '.set.active(n)' method is used to set the default item in the Combo Box Text widget, while n = the index of that item.
ourcomboboxtext.set_active(0)
ourentry = builder.get_object("entry1")

## This line doesn't need an explanation :D
ourentry.set_max_length(15)

## Nor this do.
ourentry.set_placeholder_text("Enter A Text Here..")

## We just imported the about window here to the 'ouraboutwindow' global variable.
ouraboutwindow = builder.get_object("aboutdialog1")

## Give that developer a cookie !
window.connect("delete-event", Gtk.main_quit)
window.show_all()
Gtk.main

将文件保存在您的主目录中,名称为“myprogram.py”,并为其授予执行权限并运行它。

chmod 755 myprogram.py
./myprogram.py
This is what you will get, after running above script.

在输入框中输入文本,按键盘上的“Enter”键,您会注意到该句子被打印在 shell 上。

这就是现在的全部内容,它不是一个完整的应用程序,但我只是想向您展示如何使用 PyGObject 将事物链接在一起,您可以在以下位置查看所有 GTK 小部件的所有方法gtk对象。

只需学习方法,使用 Glade 创建小部件,并使用 Python 文件连接信号,就这样!我的朋友,这并不难。

我们将在本系列的下一部分中解释有关 PyGObject 的更多新内容,在此之前请保持更新,并且不要忘记向我们提供您对本文的评论。