2015-01-27 47 views
1

今天我開始學習python。我只使用PHP的各種東西,從來不必打擾建立exe文件。Python 3.4 GUI單個可執行文件

使用我想出了隨機「你點擊多少次」申請一些互聯網Python編程教程和一點點我自己編輯如下圖所示

import winsound 
from tkinter import * 

class Application(Frame): 

    def __init__(self, master): 
     Frame.__init__(self,master) 
     self.pack() 
     self.counts = 0 
     self.create_widgets() 

    def create_widgets(self): 
     Label(self, text = "Test Label").pack() 
     self.button = Button(self, text = "click", command = self.update_text).pack() 

     self.result = Label(self, text = "Clicked 0 times") 
     self.result.pack() 

    def update_text(self): 
     self.counts += 1 

     self.result["text"] = "Clicked " + str(self.counts) + " times" 

     winsound.PlaySound('sound1.wav', winsound.SND_FILENAME | winsound.SND_ASYNC) 


root = Tk() 
root.title("Title") 
root.geometry("300x120") 
app = Application(root) 

root.mainloop 

應用程序工作正常,但啓動時的問題出於方便的原因,我試圖將它編譯成單個exe文件,因爲我打算寫一些小的「讓事情更簡單」的程序。 Py2Exe不想用bundle_files 1編譯程序。 PyInstaller用--onefile編譯它,但執行後,應用程序只會給你提供tkinter錯誤。

有沒有辦法用GUI創建一個小的exe文件或者它是死衚衕? 我不會說謊,但我非常喜歡學習python的想法,並且能夠在Web和桌面應用程序中使用它,這在PHP中不太可能。

我正在使用Python 3.4並測試內置的py2exe和PyInstaller for Python 3.3 - 3.4。

編輯: 樣品setup.py爲py2exe和錯誤

from distutils.core import setup 
import py2exe 
setup(windows=[{"script": "text.py"}], 
     options = {"py2exe": {"bundle_files": 1} 
}) 

錯誤

C:\Users\SEJBR\Desktop\Python>python setup.py py2exe 
running py2exe 

    1 missing Modules 
    ------------------ 
? readline       imported from cmd, code, pdb 
OOPS: tkinter 2 

PyInstaller編譯錯誤

Traceback (most recent call last): 
    File "<string>", line 1, in <module> 
ImportError: No module named 'Tkinter' 
2515 ERROR: TCL/TK seams to be not properly installed on this system 
+0

您是否在py2exe中使用窗口參數而不是控制檯? – 2015-01-27 00:53:44

+0

是的。我在安裝程序和bundle_files中使用了Windows:1,在安裝程序>選項中,py2exe給出了丟失模塊OOPS:由於bundle_files低於2而導致的tkinter 2錯誤。但是,對於高於1的bundle_files,沒有討論單個可執行文件。編輯了這個問題,並添加了示例py2exe setup.py和我得到的錯誤。 – SEJBR 2015-01-27 01:15:40

+0

我不熟悉py2exe,但使用PyInstaller,你得到了什麼錯誤? – fhdrsdg 2015-01-27 11:05:15

回答

0

那麼解決方法是相當簡單的。我降級到Python 2.7.9和PyInstaller編譯應用程序完美沒有公關oblems。 我只是用

from __future__ import print_function 
from __future__ import division 

使用Python 3.X打印功能和應用部門的變化。如果有人提出真正的修復,請回答這個問題。