【发布时间】:2016-01-16 15:49:00
【问题描述】:
我开始在 Python 中使用 Gtk3。我有一个问题:如何使用 pyinstaller、cx_freeze 或 py2exe 从我的 gtk3 python 源创建可执行的 windows 文件?我从堆栈溢出和许多其他网页中尝试了很多答案,但都没有奏效。 我尝试使用 pyinstaller 来实现(我认为这可能是最简单的方法),我的源代码如下所示:
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
class ButtonWindow(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self, title="Button Demo")
self.set_border_width(10)
hbox = Gtk.Box(spacing=6)
self.add(hbox)
button = Gtk.Button.new_with_label("Click Me")
button.connect("clicked", self.on_click_me_clicked)
hbox.pack_start(button, True, True, 0)
button = Gtk.Button.new_with_mnemonic("_Open")
button.connect("clicked", self.on_open_clicked)
hbox.pack_start(button, True, True, 0)
button = Gtk.Button.new_with_mnemonic("_Close")
button.connect("clicked", self.on_close_clicked)
hbox.pack_start(button, True, True, 0)
def on_click_me_clicked(self, button):
print("\"Click me\" button was clicked")
def on_open_clicked(self, button):
print("\"Open\" button was clicked")
def on_close_clicked(self, button):
print("Closing application")
Gtk.main_quit()
win = ButtonWindow()
win.connect("delete-event", Gtk.main_quit)
win.show_all()
Gtk.main()
但我收到以下错误:
Traceback (most recent call last):
File "<string>", line 2, in <module>
File "c:\python34\lib\gi\__init__.py", line 102, in require_version
raise ValueError('Namespace %s not available' % namespace)
ValueError: Namespace Gtk not available
gtk returned -1
我该怎么办,或者你能解释一下如何在 py2exe 或 cx_freeze 中制作可执行文件吗? 请帮我!谢谢!
【问题讨论】:
-
您应该使用 cx_Freeze,因为 py2exe 适用于 python 2 我将发布一个布局示例,我希望您运行它,然后运行 .exe 文件并告诉我是否存在任何错误消息,我会帮助您修复它们。
-
我在Python 3.4上使用了很长时间的py2exe。错误在您所需的版本中,而不是在 py2exe..
-
ValueError: Namespace Gtk not available 由
的第 2 行提出 -
OP 在使用 pyinstaller 而不是 py2exe 时出现该错误。
-
@mabe02 我讨厌 python 到 exe 转换问题的一点是,它们从来没有真正引起注意,而且大多数时候你必须自己弄清楚,或者如果你回答一个问题,你的答案不会没有得到任何关注,并且 OP 大部分时间都不会回来确认您的回答。