You may have already created a QMainWindow with Qt Designer. But Qt Designer can also create Form layouts based on QWidget or QDialog — and you might be wondering how to load those into your Python application and start working with the widgets inside them.
Good news - the process is almost identical. Let's walk through it.
Loading a QWidget or QDialog .ui file
When you design a form in Qt Designer using a Widget or Dialog template (rather than Main Window), the base class of that form changes. Instead of QMainWindow, it becomes QWidget or QDialog.
To load it in Python, you just need to match the base class of your Python class to the one used in the .ui file. Here's how that looks for a QWidget-based form:
from PyQt5 import QtWidgets, uic
import sys
import os
class MyWindow(QtWidgets.QWidget):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
# Load the UI Page
ui_path = os.path.dirname(os.path.abspath(__file__))
uic.loadUi(os.path.join(ui_path, "myform.ui"), self)
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
main = MyWindow()
main.show()
sys.exit(app.exec_())
If your .ui file was created using a Dialog template, swap QtWidgets.QWidget for QtWidgets.QDialog:
class MyDialog(QtWidgets.QDialog):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
ui_path = os.path.dirname(os.path.abspath(__file__))
uic.loadUi(os.path.join(ui_path, "mydialog.ui"), self)
Everything else stays the same. The uic.loadUi() function reads the .ui file and sets up all the widgets on self, regardless of whether the form is a main window, a widget, or a dialog. If you're new to working with Qt Designer, see our first steps with Qt Designer tutorial for a full walkthrough of creating .ui files.
The os.path lines ensure that the .ui file is found relative to your Python script's location, which avoids issues when running your script from a different working directory.
Accessing widgets from the .ui file
Once the .ui file is loaded, you can interact with any widget on the form directly from Python. The way you refer to a widget is through its objectName — the name you assign it in Qt Designer.
To find or set a widget's object name, click on the widget in Qt Designer and look at the Property Editor panel on the right. At the top of the properties list, you'll see objectName.
Packaging Python Applications with PyInstaller by Martin Fitzpatrick — This step-by-step guide walks you through packaging your own Python applications from simple examples to complete installers and signed executables.

Whatever name you type here becomes the attribute name on self in Python. So if you set a label's objectName to input_name, you can access it in your code like this:
self.input_name.setText("Hello!")
This works for any widget type — labels, buttons, line edits, combo boxes, and so on. For example, if you have a QPushButton with the objectName btn_submit, you could connect its clicked signal like this:
class MyWindow(QtWidgets.QWidget):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
ui_path = os.path.dirname(os.path.abspath(__file__))
uic.loadUi(os.path.join(ui_path, "myform.ui"), self)
# Connect button signal to a method
self.btn_submit.clicked.connect(self.on_submit)
def on_submit(self):
name = self.input_name.text()
print(f"Hello, {name}!")
Here, input_name is a QLineEdit and btn_submit is a QPushButton, both named via their objectName property in Qt Designer. After loadUi runs, they're available as regular Python attributes on the class.
Choosing meaningful object names
It's worth taking a moment to give your widgets clear, descriptive object names in Qt Designer. The default names — like label, label_2, pushButton_3 — are hard to work with once you have more than a few widgets. Names like label_status, input_email, or btn_cancel make your Python code much easier to read and maintain.
Summary
You can load any .ui file into Python using uic.loadUi(). Just make sure:
- Your Python class inherits from the same base class as the form (
QMainWindow,QWidget, orQDialog). - You pass
selfas the second argument toloadUi()so the widgets get attached to your class. - You give your widgets meaningful objectName values in Qt Designer, so you can access them easily in code with
self.objectName.
That's all there is to it. Whether your form is a full main window or a small dialog, the pattern stays the same — design in Qt Designer, load with uic.loadUi(), and interact with widgets through their object names. For more on building dialogs with Qt Designer, see our tutorial on creating dialogs with Qt Designer, and for a deeper look at designing complete GUI layouts, check out the Qt Designer GUI layout tutorial.
PyQt6 Crash Course by Martin Fitzpatrick — The important parts of PyQt6 in bite-size chunks