Use Python to control the available properties of widgets in Qt Designer

Use Python to control the available properties of widgets in Qt Designer

When designing UIs in Qt Designer, we often need to control the availability of widgets, so that we can choose to disable or enable them according to the context. This can be easily achieved with the enabled attribute. In this article, we will show you how to use Python to control the enabled property of widgets in Qt Designer.

First, we need to make sure the PyQt5 library is installed. It can be installed using the following command in the terminal:

pip install PyQt5

Next, we'll use a sample UI to illustrate how to control the enabled property of widgets in Qt Designer. Note that the UI contains a QPushButton and a QLineEdit, and we will control their enabled properties respectively.

Please refer to the following code, which shows how to load and instantiate the ui file, and read the references of QPushButton and QLineEdit:

from PyQt5 import QtWidgets, uic

# 加载UI文件
qt_creator_file = "example_ui.ui"
Ui_MainWindow, QtBaseClass = uic.loadUiType(qt_creator_file)

class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
    def __init__(self):
        # 初始化主窗口
        QtWidgets.QMainWindow.__init__(self)
        Ui_MainWindow.__init__(self)
        self.setupUi(self)
        
        # 读取QPushButton和QLineEdit的引用
        self.pushButton = self.findChild(QtWidgets.QPushButton, "pushButton")
 

Guess you like

Origin blog.csdn.net/update7/article/details/131496655