File:HelloWorld.py
#!/usr/bin/python from gi.repository import Gtk class MyWindow(Gtk.Window): def __init__(self): Gtk.Window.__init__(self, title="Hello World") self.button = Gtk.Button(label="Click Here") self.button.connect("clicked", self.on_button_clicked) self.add(self.button) def on_button_clicked(self, widget): print "Hello World" win = MyWindow() win.connect("delete-event", Gtk.main_quit) win.show_all() Gtk.main()
class MyWindow(Gtk.Window):
定义新类MyWindows,继承于Gtk.Winodw
def __init__(self): #构造函数 Gtk.Window.__init__(self, title="Hello World") #调用父类的构造函数初始化,并设置title属性的值为“Hello World” self.button = Gtk.Button(label="Click Here") #创建按钮对象 self.button.connect("clicked", self.on_button_clicked) #把“clicked”信号与方法"on_button_clicked"关联起来 self.add(self.button) #把按钮添加到windows的顶层
def on_button_clicked(self, widget): #实现信号触发的方法 print "Hello World"
win = MyWindow() #实例化MyWindow对象 win.connect("delete-event", Gtk.main_quit) #“delete-event”信号与"Gtk.main_quit"方法关联 win.show_all() #显示 Gtk.main() #启动Gtk+ 循环进程
GTK+ Python 桌面应用示例
本文介绍了一个使用Python和GTK+库创建简单桌面应用程序的例子。该程序展示了一个包含按钮的窗口,当点击按钮时会输出Hello World。通过这个例子,读者可以了解如何使用GTK+的基本组件和信号槽机制。

被折叠的 条评论
为什么被折叠?



