使用subprocess.Popen启动pyuic5

时间:2018-09-12 11:45:03

标签: python-3.x pyqt5 pyuic

我试图用pyuic5调用subprocess.Popen,以将Windows的python脚本中的qt5 .ui文件转换为python。

command = "pyuic5 -x " + filein + " -o " + fileout
process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=None, shell=False, cwd=folderPath)
output = process.communicate()

给我以下错误:

Traceback (most recent call last):
  File "N:\My Documents\Code\Python Projects\Work projects\PyQtConverter\src\fonctions.py", line 36, in convert_qt_2_py
    process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=None, shell=False, cwd=folderPath)
  File "C:\Python35\lib\subprocess.py", line 709, in __init__
    restore_signals, start_new_session)
  File "C:\Python35\lib\subprocess.py", line 997, in _execute_child
    startupinfo)
FileNotFoundError: [WinError 2] Le fichier spécifié est introuvable

问题似乎出在调用pyuic5上(尽管Windows cmd将该命令识别为有效命令吗?)。 设置shell=True可解决此问题,但我一直在阅读,此选项可能会带来安全风险,因此不建议使用。我应该做些不同的事情吗?

1 个答案:

答案 0 :(得分:0)

采取这个。在胜利10上运行良好;)

必须更改gui.ui(变量名称:guiNameUi)和您的outputfile.py(变量名称:guiNameUi)的名称

例如:guiNameUi = r'Button.ui'到guiNameUi = r'yourGuiName.ui'      guiNamePy = r'Button.py'到guiNameUi = r'yourGuiName.py'

至少有一个必须将您的路径更改为pyuic5,如下所示: 转换器= r'C:\ Users \ YourPC \ Anaconda3 \ Library \ bin'

转换后,gui的python文件在标准应用中打开


"""
Created on Fri May 10 16:59:26 2019

@author: BlindSide
"""
"""
--version             show program's version number and exit
  -h, --help            show this help message and exit
  -p, --preview         show a preview of the UI instead of generating code
  -o FILE, --output=FILE
                        write generated code to FILE instead of stdout
  -x, --execute         generate extra code to test and display the class
  -d, --debug           show debug output
  -i N, --indent=N      set indent width to N spaces, tab if N is 0 [default:
                        4]
  -w, --pyqt3-wrapper   generate a PyQt v3 style wrapper

  Code generation options:
    --from-imports      generate imports relative to '.'
    --resource-suffix=SUFFIX
                        append SUFFIX to the basename of resource files
                        [default: _rc]
"""

#Used in converting Script
import os
import subprocess

# python file ConvertUiToPy.py and example.ui lies in same folder
# directory of file
currentWorkingDir = os.getcwd()

#name of GUI.ui
guiNameUi = r'Button.ui'

#name of GUI.py
guiNamePy = r'Button.py'

# concat WorkingDir und GUI
fullpathUi = os.path.join(currentWorkingDir, guiNameUi)
fullpathPy = os.path.join(currentWorkingDir, guiNamePy)

# directory of pyuic5.bat -> eg. Anaconda 3 replace XYZ with userAccount
converter = r'C:\Users\YourPC\Anaconda3\Library\bin'

try:
    # change directory from  to pyuic5
    os.chdir(converter)
    print("directory changed... \n...executing conversion")  
    #execute pyuic5 to convert files
    p1 = subprocess.call("pyuic5 -x \"" + fullpathUi + "\" -o \"" + fullpathPy + "\"", shell=True)
    #file exists?
    if(os.path.isfile(fullpathPy)):
        print("...open new gui.py")
        #open file in standard app
        os.startfile("\"" + fullpathPy + "\"")
    else:
        #file doesnt exists? throw exception!!!
        raise

# handling exeception
except:
    print("something went wrong")

# further work in workingDirectory
finally:
    print("restore old path")
    os.chdir(currentWorkingDir)
    print("changed to:", os.getcwd())