这是GTK教程中的示例2
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()当我尝试运行时,它什么也不显示,当我关闭窗口时,我收到一条消息,告诉我程序仍在运行。我已经删除了代码,通过删除按钮,窗口将会出现,所以我认为button.add中有一个错误。
发布于 2015-04-29 00:01:40
在您的代码中,包含‘==’的行有一个意外的缩进错误,并且它没有使用__name__类'__main__‘技巧(尽管这只是一个好习惯)。
这应该是可行的。至少在我的Ubuntu dist上使用Python3.4的Gtk+3是这样的。
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")
if __name__ == '__main__':
win = MyWindow()
win.connect("delete-event", Gtk.main_quit)
win.show_all()
Gtk.main()发布于 2016-07-24 01:36:08
尝试从这种方式导入
import gi
gi.require_version('Gtk', '3.0')
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()然后使用您的ide重新压缩所有行。
如果不能正常工作,请卸载所有gtk模块,请安装
https://sourceforge.net/projects/pygobjectwin32/files/
然后重试
https://stackoverflow.com/questions/29882151
复制相似问题