【发布时间】:2014-01-03 16:09:30
【问题描述】:
我刚刚开始学习如何在 Python 中创建 GUI 应用程序。我决定使用 Gtk 版本 3。 根据http://python-gtk-3-tutorial.readthedocs.org/ 上的(官方?)教程,构建 hello world 应用程序的正确方法是:
from gi.repository import Gtk
class MyWindow(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self)
self.set_default_size(200, 100)
self.connect('destroy', Gtk.main_quit)
self.show_all()
MyWindow()
Gtk.main()
在其他教程 (http://www.micahcarrick.com/gtk3-python-hello-world.html) 中,我发现完全不同的方法是:
from gi.repository import Gtk, Gio
class HelloWorldApp(Gtk.Application):
def __init__(self):
Gtk.Application.__init__(self, application_id="apps.test.helloworld",
flags=Gio.ApplicationFlags.FLAGS_NONE)
self.connect("activate", self.on_activate)
def on_activate(self, data=None):
window = Gtk.Window(type=Gtk.WindowType.TOPLEVEL)
window.set_title("Gtk3 Python Example")
window.set_border_width(24)
label = Gtk.Label("Hello World!")
window.add(label)
window.show_all()
self.add_window(window)
if __name__ == "__main__":
app = HelloWorldApp()
app.run(None)
在这个领域有经验的人能告诉我这些天我应该以什么方式在 python 中编写 Gtk 3 应用程序?我已经熟悉编写 GUI(在 Java 的 Swing 中花了几个月的时间),因此您可以继续使用事件、回调等术语。
【问题讨论】: