PyGObject入门
使用Gtk.Builder引入glade的UI文件
<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.22.1 -->
<interface>
<requires lib="gtk+" version="3.20"/>
<object class="GtkAccelGroup" id="accelgroup1"/>
<object class="GtkTextBuffer" id="textbuffer1">
<property name="text" translatable="yes">hello world!</property>
</object>
<object class="GtkWindow" id="window1">
<property name="can_focus">False</property>
<property name="title" translatable="yes">Hello World</property>
<property name="default_width">800</property>
<property name="default_height">600</property>
<child>
<placeholder/>
</child>
<child>
<object class="GtkBox">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="orientation">vertical</property>
<property name="spacing">6</property>
<child>
<object class="GtkLabel">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">label</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" translatable="yes">button</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">1</property>
</packing>
</child>
<child>
<object class="GtkTextView" id="textview1">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="cursor_visible">False</property>
<property name="buffer">textbuffer1</property>
<property name="monospace">True</property>
</object>
<packing>
<property name="expand">True</property>
<property name="fill">True</property>
<property name="position">2</property>
</packing>
</child>
<child>
<object class="GtkImage" id="image1">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="stock">gtk-about</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">3</property>
</packing>
</child>
<child>
<placeholder/>
</child>
</object>
</child>
</object>
</interface>
import gi
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk
def btn1_callback(inst):
print("hello")
builder = Gtk.Builder()
builder.add_from_file('hello.glade')
window = builder.get_object('window1')
button1 = builder.get_object('button1')
window.show()
window.connect("destroy",Gtk.main_quit)
button1.connect("pressed",btn1_callback)
Gtk.main()
运行图
参考链接:
https://pygobject.readthedocs.io/en/latest/guide/api/signals.html
https://developer.gnome.org/platform-overview/unstable/tour-summary.html.zh_CN