我开始在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中创建一个可执行文件? 请帮我!谢谢!
答案 0 :(得分:2)
您应该只能在Windows上执行pip install cx_Freeze
。或者你可以去官方网站http://cx-freeze.sourceforge.net/
setup.py:
import cx_Freeze
executables = [cx_Freeze.Executable("file.py")]
cx_Freeze.setup(
name="WhatEverYouWantToNameIt",
options={"build_exe": {"packages":["gi"]}},
executables = executables
)
在程序的文件位置打开命令提示符。在Windows上,您只需shift + left-click
该文件夹,然后单击打开命令窗口。一旦打开,请键入python setup.py build
。如果您收到错误声明Python不在您的路径中,那么请提供完整路径。例如,在Windows上,使用Python 3.4,您可以:
C:/Python34/python setup.py build
如果您使用的是Mac,那么您可以这样做:
python setup.py bdist_dmg
一旦完成后再回来告诉我它是否适合。如果它没有工作只是给我错误的消息,我将解决问题。祝你好运!