简体   繁体   English

Python Gtk3可执行文件

[英]Python Gtk3 executable

I started using Gtk3 with Python. 我开始在Python中使用Gtk3。 I have one question: how I could make an executable windows file from my gtk3 python source using pyinstaller, cx_freeze or py2exe? 我有一个问题:如何使用pyinstaller,cx_freeze或py2exe从我的gtk3 python源创建一个可执行的Windows文件? I tried a lot of answers from stack overflow and many other web pages, but none worked. 我从堆栈溢出和许多其他网页尝试了很多答案,但没有一个工作。 I tried to make it with pyinstaller (I think it could be the easiest way) and my source code looks like: 我尝试使用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()

but I get the following error: 但是我收到以下错误:

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

What shall I do with this or can you please explain me how to make an executable in py2exe or cx_freeze? 我该怎么办或者你能解释一下如何在py2exe或cx_freeze中创建一个可执行文件吗? Please help me! 请帮我! Thanks! 谢谢!

Install cx_Freeze 安装cx_Freeze

You should just be able to do pip install cx_Freeze on windows. 你应该能够在Windows上执行pip install cx_Freeze Or you could go to there official website http://cx-freeze.sourceforge.net/ 或者你可以去官方网站http://cx-freeze.sourceforge.net/


Create a setup.py file in the same folder as your program. 在与程序相同的文件夹中创建setup.py文件。

setup.py: setup.py:

import cx_Freeze

executables = [cx_Freeze.Executable("file.py")]

cx_Freeze.setup(
    name="WhatEverYouWantToNameIt",
    options={"build_exe": {"packages":["gi"]}},
    executables = executables

    )

Open command prompt in the file location of your program. 在程序的文件位置打开命令提示符。 On windows you should just have to shift + left-click the folder and click open command window here. 在Windows上,你只需要shift + left-click文件夹,然后单击打开命令窗口。 Once that opens up type python setup.py build . 一旦打开类型python setup.py build If you get an error stating that Python is not in your path, then give the full path. 如果您收到错误声明Python不在您的路径中,那么请提供完整路径。 For example, on Windows, with Python 3.4, you would do: 例如,在Windows上,使用Python 3.4,您可以:

C:/Python34/python setup.py build

If you're on a mac, then you would do: 如果你在Mac上,那么你会这样做:

python setup.py bdist_dmg

Once it's done come back an tell me if it woks. 一旦完成,请回来告诉我它是否适合。 If it doesn't work just give me the errror message and I'll fix the problem. 如果它不起作用只是给我错误的消息,我会解决问题。 Good luck! 祝好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM