我已经尝试了很多时间来为Python项目创建可执行文件。在这个项目中,我需要使用:无法用PyInstaller和PyQt创建功能可执行文件
- PyQt的(4):我的GUI,
- PySerial:有一个Arduino沟通,
- 子过程:推出一些AVR事情.bat文件
事实上,可执行文件已创建,但是当我尝试启动它时,没有任何反应,除了我的鼠标告诉我她已被占用。
因此,我试图通过编写一些基本程序来了解哪里可能存在问题,这些基本程序会浓缩我项目所需的每个功能。当我从python(3.5)启动它时,所有东西都是有效的,但是当我执行由pyinstaller生成的文件时并没有。 (该interface.py文件here, in a pastebin.com file,如果你愿意,我认为这不是很重要:它只有一个按钮形式)
from PyQt4 import QtGui
from interface import Ui_Form
import serial
import subprocess
import sys, os
class win(QtGui.QWidget, Ui_Form):
"""docstring for win"""
def __init__(self):
super(win, self).__init__()
self.setupUi(self)
self.ser = serial.Serial("COM3", 9600)
self.pathBat = "cmd.bat"
def on_pushButton_clicked(self):
#if (self.ser.isOpen() and self.serAvr.isOpen()):
if True:
self.ser.write("start".encode())
p = subprocess.call(self.pathBat, creationflags=subprocess.CREATE_NEW_CONSOLE, **self.subprocess_args())
if p == 1:
self.writeLog("Works")
self.ser.write("stop".encode())
#self.writeLog(p.returncode)
def subprocess_args(include_stdout=True):
# The following is true only on Windows.
if hasattr(subprocess, 'STARTUPINFO'):
# On Windows, subprocess calls will pop up a command window by default
# when run from Pyinstaller with the ``--noconsole`` option. Avoid this
# distraction.
si = subprocess.STARTUPINFO()
si.dwFlags |= subprocess.STARTF_USESHOWWINDOW
# Windows doesn't search the path by default. Pass it an environment so
# it will.
env = os.environ
else:
si = None
env = None
ret = {}
# On Windows, running this from the binary produced by Pyinstaller
# with the ``--noconsole`` option requires redirecting everything
# (stdin, stdout, stderr) to avoid an OSError exception
# "[Error 6] the handle is invalid."
ret.update({'stdin': subprocess.PIPE,
'stderr': subprocess.PIPE,
'startupinfo': si,
'env': env })
return ret
app = QtGui.QApplication(sys.argv)
v = win()
v.show()
sys.exit(app.exec_())
我补充说:“cmd.bat”中的数据的.spec文件pyinstaller,并且功能subprocess_arg是在这里,以避免与子问题
首先,我认为这个问题是有联系的子过程(文档here作为mentionned),我试图删除它的所有引用,仍然没有工作。相同的串行。此外,我试图通过在.spec文件中设置debug = True
来调试可执行文件,但是如果我尝试从控制台执行该文件,则什么都没有发生,它在第一行保持阻塞状态。
所以,如果任何人都可以帮忙!先谢谢你 !
你使用'console = False'吗?这可能是您在命令行上看不到任何内容的原因。我最好的猜测是'界面'是问题的原因。确保它包含在构建中或创建一个没有它的例子并尝试“冻结”它。 – Repiklis
我使用'console = True'和'debug = True'。我试图将'interface.py'添加到spec文件中包含的数据中,但仍然无法工作 – Wogle220