Python 如何在Mac上将.ui文件转换为.py文件?
如何将QT Creator生成的.ui文件转换为.py文件 我曾经在windows上使用.bat文件将.ui文件转换为.py文件:Python 如何在Mac上将.ui文件转换为.py文件?,python,pyqt,pyqt5,Python,Pyqt,Pyqt5,如何将QT Creator生成的.ui文件转换为.py文件 我曾经在windows上使用.bat文件将.ui文件转换为.py文件: @echo off for %%f in (*.ui) do ( `echo %%f` `C:\Python34\Lib\site-packages\PyQt5\pyuic5.bat -x %%f -o %%`[`~nf.py`](https://~nf.py) ) pause 现在,我不再能够访问PC进行转换(加上我已经厌倦了仅仅为了转换文件而切换计算机),因
@echo off
for %%f in (*.ui) do (
`echo %%f`
`C:\Python34\Lib\site-packages\PyQt5\pyuic5.bat -x %%f -o %%`[`~nf.py`](https://~nf.py)
)
pause
现在,我不再能够访问PC进行转换(加上我已经厌倦了仅仅为了转换文件而切换计算机),因此我需要能够在mac OS中将.ui文件转换为.py。我使用下面的脚本自动生成.ui文件和资源,它应该可以在任何OS中工作 只需将
input_path
设置到包含ui文件的文件夹,并将output_path
设置到要生成Python文件的文件夹:
请注意,为了提高安全性,脚本将检查.ui文件是否以qtcreator添加的注释开头:“此文件中所做的所有更改都将丢失”
# Use this script to convert Qt Creator UI and resource files to python
import os
import subprocess
input_path = os.path.join(os.path.dirname(__file__), 'resources', 'qt')
output_path = os.path.join(os.path.dirname(__file__), 'src', 'widgets')
def check_file(file):
if 'All changes made in this file will be lost' in open(file).read():
return True
return False
for f in os.listdir(input_path):
if not os.path.isfile(f):
pass
file_name, extension = os.path.splitext(f)
if extension == '.ui':
input_file = os.path.join(input_path, f)
output_file = os.path.join(output_path, file_name + '.py')
if os.path.isfile(output_file):
if not check_file(output_file):
print('Warning: tried to overwrite a file generated outside Qt Creator. {}'.format(output_file))
continue
subprocess.call('pyuic5 --import-from=widgets -x {} -o {}'.format(input_file, output_file), shell=True)
elif extension == '.qrc':
input_file = os.path.join(input_path, f)
output_file = os.path.join(output_path, file_name + '_rc.py')
if os.path.isfile(output_file):
if not check_file(output_file):
print('Warning: tried to overwrite a file generated outside Qt Creator. {}'.format(output_file))
continue
subprocess.call('pyrcc5 {} -o {}'.format(input_file, output_file), shell=True)