Python桌面应用开发GTK3 Glade

390 阅读4分钟

PyGObject简介

PyGObject是一个用于将GTK+和其他GLib库与Python语言绑定的工具。它允许开发者使用Python语言编写基于GTK+和GLib的应用程序,为Python提供了访问GTK3及其依赖库的接口。

  • GTK3绑定: PyGObject主要用于与GTK3库进行绑定,使开发者能够使用Python语言构建现代的图形用户界面。
  • GLib绑定: 除了GTK3,PyGObject还提供了对GLib库的绑定。GLib是GTK+的底层库,提供了许多通用的功能,例如事件循环、数据结构等。
  • 对象系统: PyGObject使用了GTK+的对象系统。在Python中,你可以使用gi.repository模块导入并操作GTK3的类,例如Gtk.Window、Gtk.Button等。
  • 自动类型转换: PyGObject通过使用gobject-introspection库,实现了自动的类型转换。这意味着Python开发者可以直接使用GTK3和GLib的API,而无需手动编写大量的绑定代码。
  • 异步支持: PyGObject支持GLib的异步操作,允许你以异步的方式执行任务,而不会阻塞应用程序的用户界面。
  • 版本兼容性: 由于GTK3是一个不断演进的库,PyGObject也在不断更新以保持与GTK3的兼容性。开发者应该根据他们使用的GTK3版本选择相应的PyGObject版本。

PyGObject Github

PyGObject 文档

PyGObject 安装

注: 以下操作均在 Mac 环境

  1. python >= 3.8.16 版本
  2. gtk+3版本

安装GTK+3

# 安装相关依赖
xcode-select --install
brew install pkg-config
brew install gobject-introspection libffi
# 是否支持GTK+
brew search gtk
# 安装gtk+3
brew install gtk+3
# 验证是否安装成功
pkg-config --cflags --libs gtk+-3.0
  • 配置环境变量
# 检查 pkgconfig 路径
find / -name pkgconfig
# 检查 libffi 路径
brew --prefix libffi
# 将以上路径添加到环境变量中(.bash_profile 或 .zshrc)
vim ~/.zshrc
export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig/:/usr/local/opt/libffi:$PKG_CONFIG_PATH
source ~/.zshrc
pip3 install --upgrade pip
pip3 install pygobject

注意版本问题

gi.require_version("Gtk", "3.0")

使用 gtk+3 版本,如果还有安装 gtk4 需要先将 gtk4 版本卸载,不然import会报错

安装 Glade

Glade是一个用于创建GTK图形用户界面的用户界面构建器。它允许开发者通过可视化方式设计和布局GUI元素,而不必手动编写代码。Glade生成XML格式的描述文件,描述了用户界面的结构和属性。然后,这个XML文件可以由程序加载和解释,从而创建用户界面。

  • 可视化设计:Glade提供了一个直观的图形用户界面,允许用户通过拖放方式设计和布局界面元素,无需手动编写代码。
  • 生成XML描述文件:设计完成后,Glade会生成一个XML格式的描述文件,其中包含了用户界面的结构和属性信息。
  • 与多种编程语言兼容:由于生成的描述文件是基于XML的,因此可以与多种编程语言一起使用。GTK支持多种编程语言,包括C、C++、Python等。
  • 与IDE集成:Glade可以与多个集成开发环境(IDE)一起使用,例如GNOME Builder,以便更方便地进行开发和调试。
  • Glade Github
  • Glade 教程
  • 安装 Glade
# 目前版本支持gtk+3
brew install glade
glade --version
# 启动glade
glade
  • Glade 操作界面

45f2bd0e98c8d9a979cbde7142bfa92a.png

  • 保存后会生成如下 demo.glade 文件
<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.40.0 -->
<interface>
  <requires lib="gtk+" version="3.24"/>
  <object class="GtkWindow" id="window">
    <property name="width-request">400</property>
    <property name="height-request">200</property>
    <property name="can-focus">False</property>
    <property name="title" translatable="yes">demo</property>
    <child>
      <object class="GtkBox" id="box">
        <property name="visible">True</property>
        <property name="can-focus">False</property>
        <property name="orientation">vertical</property>
        <child>
          <object class="GtkButton" id="button">
            <property name="label" translatable="yes">button</property>
            <property name="visible">True</property>
            <property name="can-focus">True</property>
            <property name="receives-default">True</property>
            <property name="margin-start">10</property>
            <property name="margin-end">10</property>
            <property name="margin-top">10</property>
            <property name="margin-bottom">10</property>
          </object>
          <packing>
            <property name="expand">False</property>
            <property name="fill">True</property>
            <property name="position">0</property>
          </packing>
        </child>
        <child>
          <object class="GtkComboBoxText" id="combobox">
            <property name="visible">True</property>
            <property name="can-focus">False</property>
            <property name="margin-start">10</property>
            <property name="margin-end">10</property>
            <property name="margin-top">10</property>
            <property name="margin-bottom">10</property>
            <property name="active">0</property>
            <property name="active-id">1</property>
            <items>
              <item id="1" translatable="yes">item1</item>
              <item id="2" translatable="yes">item2</item>
              <item id="3" translatable="yes">item3</item>
              <item id="4" translatable="yes">item4</item>
            </items>
          </object>
          <packing>
            <property name="expand">False</property>
            <property name="fill">True</property>
            <property name="position">1</property>
          </packing>
        </child>
        <child>
          <object class="GtkEntry" id="entry">
            <property name="visible">True</property>
            <property name="can-focus">True</property>
            <property name="margin-start">10</property>
            <property name="margin-end">10</property>
            <property name="margin-top">10</property>
            <property name="margin-bottom">10</property>
          </object>
          <packing>
            <property name="expand">False</property>
            <property name="fill">True</property>
            <property name="position">2</property>
          </packing>
        </child>
      </object>
    </child>
  </object>
</interface>

完整示例 demo.py

示例使用 gtk+3demo.glade 文件构建界面

from gi.repository import Gtk
import gi
gi.require_version('Gtk', '3.0')


class MyApplication:
    def __init__(self):
        builder = Gtk.Builder()
        builder.add_from_file("demo.glade")

        self.window = builder.get_object("window")
        self.window.set_position(Gtk.WindowPosition.CENTER)
        self.window.connect("destroy", self.__on_window_destroy)

        self.__button = builder.get_object("button")
        self.__combobox = builder.get_object("combobox")
        self.__entry = builder.get_object("entry")

        self.__button.connect("clicked", self.__on_button_clicked)
        self.__combobox.connect("changed", self.__on_combobox_changed)
        self.__entry.connect("changed", self.__on_entry_changed)
        self.__entry.connect("activate", self.__on_entry_activate)

    def __on_window_destroy(self, widget):
        print("Close Window")
        Gtk.main_quit()

    def __on_button_clicked(self, widget):
        print("Hello World!")

    def __on_combobox_changed(self, widget):
        selected_index = widget.get_active()
        selected_text = widget.get_active_text()
        print("Selected index option:", selected_index, selected_text)

    def __on_entry_changed(self, widget):
        new_text = widget.get_text()
        print("Entry text changed:", new_text)

    def __on_entry_activate(self, widget):
        entered_text = widget.get_text()
        print("Entry activated with text:", entered_text)


if __name__ == "__main__":
    Gtk.init()
    app = MyApplication()
    app.window.show_all()
    Gtk.main()

运行效果

python demo.py

6e883489d2fb70339a65bcf495a03d89.png

GTK 主题

推荐主题

配置主题

主题配置待续...