PyQt6 create a window with Qt designer and load it

created at May 9, 2021, 4:14 p.m. views: 0

After install and start Qt designer, we can play with it, for example, let's try to create a window with Qt Designer.

Start Qt Designer

reference: install and start Qt designer

Create a blank window

choose Widget and click create:

we will get a empty window:

Set properties of the new window

on the right the yellow table are used to set the display attributes of our window, such as Title, Font, Location of appearance on screen…and so on.

Here we change the windowTitle from default form to PYWD:

Save the window (.ui file)

File--→save, it would be better to create a folder under your project directory to save the ui file. 

and remember to change the default file name untitled.ui to YourName.ui for loading it conveniently.
Mine is:

Loading the ui file (window)

method 1: manually load the ui file

First, we need to create a .py script in the same folder of our .ui file, we name it loadWindow.py, and write in:

from PyQt6.QtWidgets import QApplication, QWidget
from PyQt6 import uic
import sys


class UI(QWidget):
    def __init__(self):
        super().__init__()

        uic.loadUi("Window.ui", self) # load the ui file


app = QApplication(sys.argv)
window = UI()
window.show()

sys.exit(app.exec())

click run and you will  get a window like:

method 2: use command line to create the loading file

Up to now, our folder looks like this:

switch to the terminal and enter in this folder:

(venv) E:\project\FluidIML>cd PYWD
(venv) E:\project\FluidIML\PYWD>

use this command to create the loading file:

pyuic6 -x Window.ui -o runwindow.py
  • Window.ui: your ui file
  • runwindow.py: script to load your ui file

refresh our folder, we will see the script has been created:

what's in it?

# Form implementation generated from reading ui file 'Window.ui'
#
# Created by: PyQt6 UI code generator 6.0.3
#
# WARNING: Any manual changes made to this file will be lost when pyuic6 is
# run again.  Do not edit this file unless you know what you are doing.


from PyQt6 import QtCore, QtGui, QtWidgets


class Ui_Form(object):
    def setupUi(self, Form):
        Form.setObjectName("Form")
        Form.resize(400, 300)

        self.retranslateUi(Form)
        QtCore.QMetaObject.connectSlotsByName(Form)

    def retranslateUi(self, Form):
        _translate = QtCore.QCoreApplication.translate
        Form.setWindowTitle(_translate("Form", "PYWD"))


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    Form = QtWidgets.QWidget()
    ui = Ui_Form()
    ui.setupUi(Form)
    Form.show()
    sys.exit(app.exec())

References

https://www.youtube.com/watch?v=ot94H3-d5d8

Please log in to leave a comment.