Python Gtk3可执行文件 [英] Python Gtk3 executable

查看:175
本文介绍了Python Gtk3可执行文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始在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

您应该只能在Windows上执行pip install cx_Freeze.或者,您可以访问官方网站 http://cx-freeze.sourceforge.net/


在与程序相同的文件夹中创建一个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

    )

在程序的文件位置中打开命令提示符.在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


一旦完成,请回来告诉我它是否可以启动.如果它不起作用,请给我错误消息,然后我将解决问题.祝你好运!

I started using Gtk3 with Python. I have one question: how I could make an executable windows file from my gtk3 python source using pyinstaller, cx_freeze or py2exe? 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:

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? Please help me! Thanks!

解决方案

Install cx_Freeze

You should just be able to do pip install cx_Freeze on windows. Or you could go to there official website http://cx-freeze.sourceforge.net/


Create a setup.py file in the same folder as your program.

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. Once that opens up type python setup.py build. If you get an error stating that Python is not in your path, then give the full path. For example, on Windows, with Python 3.4, you would do:

C:/Python34/python setup.py build

If you're on a mac, then you would do:

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!

这篇关于Python Gtk3可执行文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆