Python脚本可使用cx_Freeze执行,exe无功能 [英] Python script to executable with cx_Freeze, exe does nothing

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

问题描述

出于实践考虑,我决定编写一个Passwordgenerator并将其设为可执行文件.
我的脚本正在按预期运行,并且编译也可以正常运行,但是当我运行exe文件时,没有任何反应.我运行Windows 10系统并使用Python 3.6.x,但我不是python本身的初学者.

I have decided for practice purposes, I'd write a Passwordgenerator and make it an executable.
My script is running as it is intended, and the compiling works as well, but when I run the exe file, nothing happens. I run a Windows 10 system and use Python 3.6.x and I am not a beginner of python itself.

我在Internet上浏览了各个页面,但是没有发现任何可以帮助我解决该问题的东西,我的第一个问题是编译没有用,但是我已经找到了解决方案.

I looked up various pages on the internet, but I found nothing that helped me on that problem, my first problem was that the compiling didn't work but I already found that solution.

我尝试使用cmd运行exe,但没有任何输出,而是出现了新行.

I tried to run the exe with the cmd and I get no output, instead I get a new line.

这是设置代码:

import sys
from cx_Freeze import setup, Executable

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

setup(name="Password",
      version="1.0",
      description="Generates a password made of 20 characters",
      options={"build_exe": build_exe_options},
      executables=[Executable("pass.py", base=base)])

这是我的程序:

import random
import string

for i in range(20):
   k = random.choice(string.ascii_letters)
   j = random.randint(0, 9)
   z = random.randint(1, 2)
   if z == 1:
      x = k
   if z == 2:
      x = j
   print(x, end=" ")

我很感谢任何见识.

推荐答案

删除两行

if sys.platform == "win32":
   base = "Win32GUI"

从您的安装脚本开始,它应该可以工作.

from your setup script and it should work.

base ="Win32GUI" 告诉 cx_Freeze 不要启动控制台窗口,仅当主应用程序启动GUI时才应使用(例如,使用PySide,PyQt,Tk),...).如果您从已经启动的控制台运行可执行文件,则它也可能会将标准输出重定向到控制台之外.在您的情况下,您有一个基于控制台的应用程序,因此希望启动一个控制台并接收标准输出. cx_Freeze 文档.

base = "Win32GUI" tells cx_Freeze not to start a console window and should be used only if the main application starts a GUI (e.g. with PySide, PyQt, Tk, ...). It presumably also redirects the standard output away from the console if you run the executable from an already started console. In your case you have a console-based application and you thus want a console to be started and to receive the standard output. This behavior is partially explained in the cx_Freeze documentation.

现在,如果您在不使用cmd的情况下运行可执行文件(例如,通过在Windows-Explorer中双击它),它将启动控制台窗口,在该窗口中打印输出,并在执行完成后立即关闭控制台.在示例脚本中,您希望有时间在控制台关闭之前读取输出,因此您需要的是告诉脚本在完成之前要等待的内容,例如直到按下某个键.您可以添加

Now if you run your executable without using the cmd (e.g. by double-clicking it in Windows-Explorer), it starts a console window, prints the output there, and closes the console immediately when the execution is finished. In your example script, you would like to have the time to read the output before the console closes, so what you need then is something to tell your script to wait before finishing, for example until you press a key. You can add

input("Press Enter to continue...")

为此,请在脚本末尾

参见如何使python等待按键按下.

这篇关于Python脚本可使用cx_Freeze执行,exe无功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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