I am new to Python and Py QT5, for the tutorial on the following page : https://www.learnpyqt.com/courses/qt-creator/qt-designer-gui-layout/, 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.
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() main.show() sys.exit(app.exec_())
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
Thank your very much for providing the example code!
Hi Martin, thank you very much for answering my question.