'How to convert a .ui file to .py file with PyQt5 and python 3.6
Converting a .ui file to a .py file using cmd
C:/Python36/Lib/site-packages/PyQt5/pyuic.cpython-36.pyc -x mainwindow.ui -o mainwindow.py
The error shows like
The system cannot find the path specified.
I checked the path, unfortunately, there is no file named in the PyQt folder
please, someone, check this problem
Solution 1:[1]
Pretty late, but it might help someone
Tested on windows 10 pro, python 3.9.10, Pyqt 6.2.3
import os
import sys
import subprocess
import traceback
# create folders for storing ui files and for storing py file in your project
#.project
# ui_pyfiles
# __init__.py
# ui_pyfile1.py
# ui_pyfile2.py
# ui_xmlfiles
# ui_xmlfile1.ui
# ui_xmlfile2.ui
# convert_uifile.py
# main.py
# keep path of ui file and path of py file in a dictionary,
# use name of ui file as key (or any other name, your choice)
file_paths = {
'main_window': [os.path.abspath('.\\ui_xmlfiles\\main_window.ui'), os.path.abspath('.\\ui_pyfiles\\ui_main_window.py')],
'file_display_list_mode': [os.path.abspath('.\\ui_xmlfiles\\file_display_list_mode.ui'), os.path.abspath('.\\ui_pyfiles\\ui_file_display_list_mode.py')],
'playlistview': [os.path.abspath('.\\ui_xmlfiles\\playlistview.ui'), os.path.abspath('.\\ui_pyfiles\\ui_playlistview.py')],
'property_item_display': [os.path.abspath('.\\ui_xmlfiles\\property_item_display.ui'), os.path.abspath('.\\ui_pyfiles\\ui_property_item_display.py')],
'group_property_display': [os.path.abspath('.\\ui_xmlfiles\\group_property_display.ui'), os.path.abspath('.\\ui_pyfiles\\ui_group_property_display.py')],
'properties_display': [os.path.abspath('.\\ui_xmlfiles\\properties_display.ui'), os.path.abspath('.\\ui_pyfiles\\ui_properties_display.py')],
# 'window_v2': [os.path.abspath('.\\ui_xmlfiles\\window_v2.ui'), os.path.abspath('.\\ui_pyfiles\\ui_window_v2.py')],
}
# provide full path of folder containing pyuic6.exe
# on windows run 'where pyuic6' on cmd to locate pyuic6.exe then grab the dir
pyuic6_dir = 'C:\\Users\\Hp\\pyvenv\\pyqt6_python3_9_10.venv\\Scripts'
current_working_dir = os.getcwd()
def do_convert(key_: str):
"""@param key_: str -> name of ui file (a key in the file_paths dictionary)"""
if key_.lower() in ('*', 'all'):
for key_ in file_paths: do_convert(key_)
uifile_path = file_paths[key_][0]
pyfile_path = file_paths[key_][1]
# create the py files folder if it does not exist
# here, i'm considering the folder as package, create __init__.py file
dir_ = os.path.split(pyfile_path)[0]
if not os.path.exists(dir_):
os.makedirs(dir_); open(f'{dir_}\\__init__.py', 'w').close()
if os.path.getsize(uifile_path) == 0: return
os.chdir(pyuic6_dir)
cmd = f'pyuic6 -x {uifile_path} -o {pyfile_path}'
try:
subprocess.call(cmd, shell=True)
except Exception as err:
print(f'an error occured: {str(err)}')
# traceback.print_exc()
finally:
os.chdir(current_working_dir)
def update_imports():
items = os.listdir('./ui_pyfiles')
t = ", ".join([item[:-3] for item in items if item.endswith(".py") and not item in ("__init__.py",)])
t = f'from ui_pyfiles import {t}\n' if t else ""
with open('./ui_pyfiles/__init__.py', 'w') as file:
file.seek(0); file.truncate()
file.write(t)
if __name__ == '__main__':
try:
do_convert(sys.argv[1])
print('success')
except IndexError:
print('error: must supply name of ui file to convert to pyfile')
except KeyError:
print(f'error: the ui file \'{sys.argv[1]}\' not found. available ui files: ({", ".join(file_paths.keys())})')
finally:
update_imports()
Save as convert_uifile.py
Usage on cmd: convert_uifile.py main_window
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
