Importing the .ui form

Heads up! You've already completed this tutorial.

Albert_Ang | 2020-05-25 10:29:00 UTC | #1

I am new to Python and Py QT5, for the tutorial on the following page :, how do I import the form UI file in Python and interact with the Text labels? Previously there was only a tutorial on importing the MainWindow UI.

tcarson4344 | 2020-05-25 14:13:43 UTC | #2


I am also new to Python so there may be more elegant solutions. In the sample below created a class called MyWindow that inherits from QWidget instead of QMainWindow. If you made a dialog ui file use QDialog.

from PyQt5 import QtWidgets, uic
import sys, os

class MyWindow(QtWidgets.QWidget):

    def __init__(self, *args, **kwargs):
        super(MyWindow, self).__init__(*args, **kwargs)

        # Load the UI Page - added path too
        ui_path = os.path.dirname(os.path.abspath(__file__))
        uic.loadUi(os.path.join(ui_path, "chanlCfg.ui"), self)

if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    main = MyWindow()

martin | 2020-05-25 18:35:24 UTC | #3

Hi @Albert_Ang welcome to the forum!

It's a great question -- I plan to add a more in-depth example of this in the tutorial. But the simple answer is that you can import any UI type in Python in much the same way (as @tcarson4344 showed is right). How you interact with the form elements though depends on how you name them in Qt Designer.

In Qt Designer click on any of the widgets you work with, then in the panel find the "objectName" parameter. This is the name that that widget will be known by in Python.


So, for example, if you enter input_name as an objectName then that widget will be available in Python under self.input_name.

Albert_Ang | 2020-05-27 04:13:12 UTC | #4

Thank your very much for providing the example code!

Albert_Ang | 2020-05-27 04:13:46 UTC | #5

Hi Martin, thank you very much for answering my question.

covaj318218817 | 2020-12-01 05:54:31 UTC | #6

convert the ui to python script. open cmd in same folder (as ui file) and type pyui5 -x file.ui -o then edit the

Over 10,000 developers have bought Create GUI Applications with Python & Qt!
Create GUI Applications with Python & Qt5
Take a look

Downloadable ebook (PDF, ePub) & Complete Source code

Also available from Leanpub and Amazon Paperback

[[ discount.discount_pc ]]% OFF for the next [[ discount.duration ]] [[discount.description ]] with the code [[ discount.coupon_code ]]

Purchasing Power Parity

Developers in [[ country ]] get [[ discount.discount_pc ]]% OFF on all books & courses with code [[ discount.coupon_code ]]
Well done, you've finished this tutorial! Mark As Complete
[[ user.completed.length ]] completed [[ user.streak+1 ]] day streak

Importing the .ui form was written by Martin Fitzpatrick .

Martin Fitzpatrick has been developing Python/Qt apps for 8 years. Building desktop applications to make data-analysis tools more user-friendly, Python was the obvious choice. Starting with Tk, later moving to wxWidgets and finally adopting PyQt.