2017-10-17 105 views
2

所以我创建了一个tkinter程序并将其保存为一个.pyw文件,以便当我运行它时它不会显示CLI背景窗口,但是当我使用py2exe和cx-Freeze编译它时,运行它显示CLI提示符的可执行文件。我怎样才能将它编译成可执行文件,在运行时它不会显示黑色窗口?转换Tkinter应用程序与CLI CLI

回答

0

在Windows中,你可以使用隐藏CLI:

base = Win32GUI 

例如,在此脚本:

import sys 
from cx_Freeze import setup, Executable 

build_exe_options = {"packages": ["os"], "excludes": []} 
base = None 
if sys.platform == "win32": 
    base = "Win32GUI" 

setup( name = "guifoo", 
     version = "0.1", 
     description = "My GUI application!", 
     options = {"build_exe": build_exe_options}, 
     executables = [Executable("guifoo.py", base=base)]) 

编辑:

你提不包括媒体文件。这很正常,你必须手动指定这些。虽然有很多方法可以做到这一点,但最简单的方法是使用include_files参数。这是相当简单,可以这样做:

import sys 
from cx_Freeze import setup, Executable 

build_exe_options = {"packages": ["os"], "excludes": [], "include_files":["Path/To/Media/Files.jpg"]} 
base = None 
if sys.platform == "win32": 
    base = "Win32GUI" 

setup( name = "guifoo", 
     version = "0.1", 
     description = "My GUI application!", 
     options = {"build_exe": build_exe_options}, 
     executables = [Executable("guifoo.py", base=base)]) 

使用这种方法,你可以包括这样的整个文件夹:

"include_files":["path/to/folder"] 

或单个文件(如上图所示),这样的:

"include_files":["path/to/file.png"] 
+0

谢谢西蒙。它确实有效,但有些错误。构建成功,但所有媒体文件未被复制,因此我必须手动移动它。顺便说一句,如果我想使用py2exe而不是cx_Freeze,你能帮助我吗? –

+0

@LeeYou没有抱歉,我不知道py2exe(它也只适用于python 2)还包括媒体文件的名称和文件路径是什么?一旦你告诉我,我会编辑包含这些内容。 – Simon