fosKeyMan
Loading...
Searching...
No Matches
convertpyfile.py
Go to the documentation of this file.
2r"""
3This script is used to convert the Qt Designer UI files and icons to Python files.
4"""
5
6import os
7import subprocess
8import re
9
10base_dir = os.path.abspath(os.path.dirname(__file__))
11ui_source_dir = os.path.join(base_dir, 'resources/ui')
12qrc_source_dir = os.path.join(base_dir, 'resources')
13py_target_dir = os.path.join(base_dir, 'frontend/ui')
14
15
17 r"""
18 Convert Qt Designer UI files to Python files.
19 """
20 for file_name in os.listdir(ui_source_dir):
21 if file_name.endswith('.ui'):
22 ui_file_path = os.path.join(ui_source_dir, file_name).replace("\\", "/")
23 py_file_name = f"ui{os.path.splitext(file_name)[0]}.py"
24 py_file_path = os.path.join(py_target_dir, py_file_name).replace("\\", "/")
25
26 if os.path.exists(ui_file_path):
27 command = f'./pyside6-uic "{ui_file_path}" -o "{py_file_path}"'
28 try:
29 result = subprocess.run(command, check=True, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
30 print(f"Successfully converted {ui_file_path} to {py_file_path}.")
31 modify_import_statement(py_file_path)
32 except subprocess.CalledProcessError as e:
33 print(f"Converting {ui_file_path} failed: {e}")
34 else:
35 print(f"File does not exist: {ui_file_path}")
36
37
38def modify_import_statement(py_file_path):
39 with open(py_file_path, 'r', encoding='utf-8') as file:
40 content = file.read()
41 # 'import toolicons_rc' -> 'from frontend.ui import toolicons_rc'
42 content = re.sub(r'import (\w+_rc)', r'from frontend.ui import \1', content)
43 with open(py_file_path, 'w', encoding='utf-8') as file:
44 file.write(content)
45
46
48 r"""
49 Convert ressource files to Python files.
50 """
51 for file_name in os.listdir(qrc_source_dir):
52 if file_name.endswith('.qrc'):
53 qrc_file_path = os.path.join(qrc_source_dir, file_name)
54 py_file_name = f"{os.path.splitext(file_name)[0]}_rc.py"
55 py_file_path = os.path.join(py_target_dir, py_file_name).replace("\\", "/")
56
57 if os.path.exists(qrc_file_path):
58 command = f'./pyside6-rcc "{qrc_file_path}" -o "{py_file_path}"'
59 try:
60 result = subprocess.run(command, check=True, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
61 print(f"Successfully converted {qrc_file_path} to {py_file_path}")
62 except subprocess.CalledProcessError as e:
63 print(f"Converting {qrc_file_path} failed: {e}")
64 else:
65 print(f"The file does not exist:{qrc_file_path}")
66
67
68if __name__ == '__main__':
69 if not os.path.exists(py_target_dir):
70 os.makedirs(py_target_dir)
71
72 print("Start converting .ui files…")
74
75 print("\nStart converting .qrc files…")
77
convert_qrc_files()
Convert ressource files to Python files.
modify_import_statement(py_file_path)
convert_ui_files()
Convert Qt Designer UI files to Python files.