PyGObject是一个用于将GTK+和其他GLib库与Python语言绑定的工具。它允许开发者使用Python语言编写基于GTK+和GLib的应用程序,为Python提供了访问GTK3及其依赖库的接口。
brew install gobject-introspection libffi
# 检查 libffi 路径
brew --prefix libffi
# 添加libffi环境变量
export PKG_CONFIG_PATH="/usr/local/opt/libffi:$PKG_CONFIG_PATH"
pip3 install --upgrade pip
# gtk4
pip3 install pygobject
brew install pygobject3 gtk4
使用gtk+3版本,需要将 gtk4 版本先卸载
gi.require_version("Gtk", "3.0")
# 卸载gtk4
brew uninstall gtk4
import sys
import gi
gi.require_version("Gtk", "4.0")
from gi.repository import GLib, Gtk
class MyApplication(Gtk.Application):
def __init__(self):
super().__init__(application_id="com.example.MyGtkApplication")
GLib.set_application_name("My Gtk Application")
def do_activate(self):
window = Gtk.ApplicationWindow(application=self, title="Hello World")
window.present()
app = MyApplication()
exit_status = app.run(sys.argv)
sys.exit(exit_status)
git clone https://github.com/GNOME/glade.git
# 目前版本支持gtk+3
brew install glade
glade --version
# 启动
glade
<interface>
<requires lib="gtk+" version="3.24"/>
<object class="GtkWindow" id="window">
<property name="width-request">400property>
<property name="height-request">200property>
<property name="can-focus">Falseproperty>
<property name="title" translatable="yes">demoproperty>
<child>
<object class="GtkBox" id="box">
<property name="visible">Trueproperty>
<property name="can-focus">Falseproperty>
<property name="orientation">verticalproperty>
<child>
<object class="GtkButton" id="button">
<property name="label" translatable="yes">buttonproperty>
<property name="visible">Trueproperty>
<property name="can-focus">Trueproperty>
<property name="receives-default">Trueproperty>
<property name="margin-start">10property>
<property name="margin-end">10property>
<property name="margin-top">10property>
<property name="margin-bottom">10property>
object>
<packing>
<property name="expand">Falseproperty>
<property name="fill">Trueproperty>
<property name="position">0property>
packing>
child>
<child>
<object class="GtkComboBoxText" id="combobox">
<property name="visible">Trueproperty>
<property name="can-focus">Falseproperty>
<property name="margin-start">10property>
<property name="margin-end">10property>
<property name="margin-top">10property>
<property name="margin-bottom">10property>
<property name="active">0property>
<property name="active-id">1property>
<items>
<item id="1" translatable="yes">item1item>
<item id="2" translatable="yes">item2item>
<item id="3" translatable="yes">item3item>
<item id="4" translatable="yes">item4item>
items>
object>
<packing>
<property name="expand">Falseproperty>
<property name="fill">Trueproperty>
<property name="position">1property>
packing>
child>
<child>
<object class="GtkEntry" id="entry">
<property name="visible">Trueproperty>
<property name="can-focus">Trueproperty>
<property name="margin-start">10property>
<property name="margin-end">10property>
<property name="margin-top">10property>
<property name="margin-bottom">10property>
object>
<packing>
<property name="expand">Falseproperty>
<property name="fill">Trueproperty>
<property name="position">2property>
packing>
child>
object>
child>
object>
interface>
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("ui/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()