首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用subprocess.Popen启动pyuic5

使用subprocess.Popen启动pyuic5
EN

Stack Overflow用户
提问于 2018-09-12 19:45:03
回答 1查看 160关注 0票数 0

我正在尝试用subprocess.Popen调用pyuic5,以便从Windows上的python脚本中将qt5 .ui文件转换为python。

代码语言:javascript
复制
command = "pyuic5 -x " + filein + " -o " + fileout
process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=None, shell=False, cwd=folderPath)
output = process.communicate()

出现以下错误:

代码语言:javascript
复制
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解决了这个问题,但我一直在读到这个选项可能会带来安全风险,所以不推荐这样做。我应该以不同的方式做事情吗?

EN

回答 1

Stack Overflow用户

发布于 2019-05-12 06:25:19

拿这个吧。在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’GuiName.py‘

至少有一个必须像这样更改pyuic5的路径: converter = r'C:\Users\YourPC\Anaconda3\Library\bin‘

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

代码语言:javascript
复制
"""
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())
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52294361

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档