0

我正在尝试调用pyuic5subprocess.Popen将 qt5 .ui 文件从 Windows 上的 python 脚本中转换为 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解决了问题,但我一直在阅读此选项可能存在安全风险,不推荐使用。我应该以不同的方式做事吗?

4

1 回答 1

0

拿这个。在 win 10 上运行良好;)

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

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

至少有一个人必须像这样将您的路径更改为 pyuic5:converter = 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())
于 2019-05-11T22:25:19.430 回答