在许多PyGTk教程中,事件处理程序的定义如下:
window.connect("destroy", self.close)
button.connect("clicked", self.print_hello_world)
是否有任何类封装"destroy", "clicked"字符串字量,因为我想将它们作为常量访问
在小型应用程序中,我们可以这样写代码:
class MyApp():
def __init__(self):
self.win = Gtk.Window()
self.win.set_size_request(400, 300)
self.win.connect('destroy', self.on_app_exit)
btn = Gtk.Button("hello")
btn.connect('clicked', self.on_button_clicked)
def run(self):
self.win.show_all()
Gtk.main()
def on_app_exit(self, window):
// do something.
Gtk.main_quit()
def on_button_clicked(self, btn):
print('hello, world')
def main():
app = MyApp()
app.run()
if __name__ == '__main__':
main()