目录
Github
官方文档
简介
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版本。
环境配置
- python3
brew install gobject-introspection libffi
# 检查 libffi 路径
brew --prefix libffi
# 添加libffi环境变量
export PKG_CONFIG_PATH="/usr/local/opt/libffi:$PKG_CONFIG_PATH"
- 搜索pygobject版本:https://pypi.org/search/
- pygobject历史版本:https://pypi.org/project/PyGObject/#history
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
gtk4 版本 Demo.py
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)
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>
gtk+3 版本 demo.py
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()
标签:__,10,.__,Python,text,self,Gtk,Glade,GTK3
From: https://www.cnblogs.com/wufengsheng/p/18003415