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+ 循环进程