Skip to content

Instantly share code, notes, and snippets.

@michalmonday
Created January 24, 2023 11:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save michalmonday/a217778f385d3509442b6c3ad6c45e4c to your computer and use it in GitHub Desktop.
Save michalmonday/a217778f385d3509442b6c3ad6c45e4c to your computer and use it in GitHub Desktop.
How to create and use Qt Designer or Qt Creator to create a GUI for python.
  • Create "ui" file in Qt Designer or Qt Creator.
  • Run update_MainWindow.py which runs pyuic6 -o my_file.py <file>.ui and moves imports from the bottom to the top of the file, because promoted classes happen to be imported at the bottom for some reason which was causing an error.
  • See files in this folder (e.g. file_loader.py) to see how to create/connect signals and slots.
#!py -3
''' Converts .ui file (outputted by Qt Designer) to .py file (for PyQt6)
and imports from the bottom to the top of the file, because promoted
classes happen to be imported at the bottom for some reason which was
causing an error. '''
import os
import re
out_fname = 'MainWindow.py'
print( os.popen(f'pyuic6 mainwindow.ui -o {out_fname}').read() )
with open(out_fname) as f:
lines = [line.rstrip() for line in f.readlines()]
lines_to_move = []
for line in reversed(lines):
# if re.search(r'^from.+import', line):
if line.startswith('from'):
lines_to_move.append(line)
else:
break
for line in lines_to_move:
lines.remove(line)
for i, line in enumerate(lines_to_move):
lines_to_move[i] = re.sub('from ', 'from .', line)
lines = lines_to_move + lines
with open(out_fname, 'w') as f:
f.write('\n'.join(lines))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment