python3 可执行文件,Python Gtk3可执行文件

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 "", line 2, in

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!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值