![](/img/trans.png)
[英]How to make a Python script run without it opening the console window with tkinter?
[英]How to make a Python script run without it opening the console window?
我想雙擊我的Python腳本(使用Tkinter GUI),只希望它打開Tkinter窗口,而不是控制台窗口。
為此,我將擴展名從.py更改為.pyw,這似乎在Windows上可以正常使用,但是當我在Linux計算機上雙擊.pyw文件時,它不起作用。 它只是凍結了,我不得不重新啟動系統。
請提出一個獨立於平台的解決方案,該解決方案可以幫助我在不打開終端/命令提示符的情況下運行Python腳本。
自從我在Linux上嘗試以來已經有一段時間了,但是我認為它應該相當簡單,首先您需要在腳本的頂部放置一個shebang,以便您的shell知道要使用哪個可執行文件:
#!/usr/bin/python
或者,如果您想要特定的版本,則可以將其擴展為:
#!/usr/bin/python3.2
使用您想要的任何版本(僅適用於前2位數字)
那么您需要將其標記為可執行文件:
chmod 711 myfile.py
有關此的更多信息,請參見此頁面
然后當您雙擊它時,在rpi(我上次使用linux)上,它詢問您是否要執行它,或在終端中執行它。
如果選擇不使用終端執行該命令,則應該只看到tkinter GUI
您可以使用pyinstaller
為所需的不同平台創建可執行文件。
例如,
pyinstaller命令的語法為:
pyinstaller [options] script [script ...] | specfile
在最簡單的情況下,將當前目錄設置為程序myscript.py的位置,然后執行:
pyinstaller myscript.py
PyInstaller分析myscript.py並執行以下操作:
Writes myscript.spec in the same folder as the script.
Creates a folder build in the same folder as the script if it does not exist.
Writes some log files and working files in the build folder.
Creates a folder dist in the same folder as the script if it does not exist.
Writes the myscript executable folder in the dist folder.
在dist文件夾中,您找到分發給用戶的捆綁應用程序。
通常,您在命令行上命名一個腳本。 如果您命名更多,將對所有內容進行分析並將其包含在輸出中。 但是,第一個名為的腳本提供了規范文件以及可執行文件夾或文件的名稱。 它的代碼是第一個在運行時執行的代碼。
對於某些用途,您可以編輯myscript.spec的內容(在“使用規格文件”中進行了介紹)。 完成此操作后,將規范文件命名為PyInstaller而不是腳本:
pyinstaller myscript.spec
您可以提供腳本或規范文件的路徑,例如
pyinstaller options... ~/myproject/source/myscript.py
或者,在Windows上,
pyinstaller "C:\Documents and Settings\project\myscript.spec"
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.