Home > Blockchain >  Binding widget properties to Python variables
Binding widget properties to Python variables

Time:10-12

was trying to understand https://wiki.python.org/moin/PyQt/Binding widget properties to Python variables :

"Binding widget properties to Python variables"

down below my modified code that took a while to me, but kind of visualize better what bind, example code, here, does:

def bind(objectName, propertyName, type):

    def getter(self):
        return type(self.findChild(QObject, objectName).property(propertyName).toPyObject())
    
    def setter(self, value):
        self.findChild(QObject, objectName).setProperty(propertyName, QVariant(value))
    
    return property(getter, setter)

my complete code is:

import sys

from PyQt5.QtWidgets import QWidget, QLineEdit, QTextEdit, QCheckBox, QFormLayout, QPushButton

from PyQt5 import QtWidgets


def bind(objectName, propertyName, type):

    def getter(self):
        return type(self.findChild(QObject, objectName).property(propertyName).toPyObject())
    
    def setter(self, value):
        self.findChild(QObject, objectName).setProperty(propertyName, QVariant(value))
    
    return property(getter, setter)

class Window(QWidget):

    def __init__(self, parent = None):
    
        super().__init__(parent)
        self.nameEdit = QLineEdit()
        self.nameEdit.setObjectName("nameEdit")
        self.addressEdit = QTextEdit()
        self.addressEdit.setObjectName("addressEdit")
        self.contactCheckBox = QCheckBox()
        self.contactCheckBox.setObjectName("contactCheckBox")
        self.button_1 = QPushButton('press me !!!')
        self.button_1.clicked.connect(self.pushButton_1_Pressed)
        
        self.button_2 = QPushButton('press me !!! second')
        self.button_2.clicked.connect(self.pushButton_2_Pressed)
        

        self.layout = QFormLayout(self)
        self.layout.addRow(self.tr("Name:"), self.nameEdit)
        self.layout.addRow(self.tr("Address:"), self.addressEdit)
        self.layout.addRow(self.tr("Receive extra information:"), self.contactCheckBox)
        
        self.layout.addWidget(self.button_1)
        self.layout.addWidget(self.button_2)
        
        self.setLayout(self.layout)
        
        self.name = bind('nameEdit', 'text', str)
        self.address = bind("addressEdit", "plainText", str)
        self.contact = bind("contactCheckBox", "checked", bool)
            
    def pushButton_1_Pressed(self):
        
        print(self.nameEdit.text())
        
        print(self.addressEdit.toPlainText())
        
        print(self.contactCheckBox.isChecked())
        
    def pushButton_2_Pressed(self):

        self.nameEdit.setText('pippo')
        
        
        self.addressEdit.clear()
        self.addressEdit.insertPlainText('papero')
        
        self.contactCheckBox.setChecked(True)
        
        print(self.nameEdit.text())
        
        print(self.addressEdit.toPlainText())
        
        print(self.contactCheckBox.isChecked())

    


if __name__ == "__main__":

    app = QtWidgets.QApplication(sys.argv)
    window = Window()
    window.show()
    sys.exit(app.exec_())


after you insert text into a QLineEdit or QTextEdit or check a QCheckBox widget you can print the variables defined by bind and pressing the second button you change the variable values and the text/values of the widgets at the same time (got some insight from Binding a PyQT/PySide widget to a local variable in Python.

since the inners of Python and PyQt5 are to hard to me right know, is about a dumb-proof descriprion about how bind works in the code on the three widgets.

CodePudding user response:

The article you are referring to tries to implement the python properties using the QObjects properties.

Since it is a property it should not be declared within the class but at the method level as class attribute. On the other hand, the code must be updated since it is written for PyQt4 where the conversion between objects from python to Qt was not implicit, considering the above, the solution is:

import sys

from PyQt5.QtCore import QObject
from PyQt5.QtWidgets import (
    QApplication,
    QWidget,
    QLineEdit,
    QTextEdit,
    QCheckBox,
    QFormLayout,
    QPushButton,
)


def bind(objectName, propertyName):
    def getter(self):
        return self.findChild(QObject, objectName).property(propertyName)

    def setter(self, value):
        self.findChild(QObject, objectName).setProperty(propertyName, value)

    return property(getter, setter)


class Window(QWidget):
    name = bind("nameEdit", "text")
    address = bind("addressEdit", "plainText")
    contact = bind("contactCheckBox", "checked")

    def __init__(self, parent=None):
        super().__init__(parent)
        self.nameEdit = QLineEdit()
        self.nameEdit.setObjectName("nameEdit")
        self.addressEdit = QTextEdit()
        self.addressEdit.setObjectName("addressEdit")
        self.contactCheckBox = QCheckBox()
        self.contactCheckBox.setObjectName("contactCheckBox")
        self.button_1 = QPushButton("press me !!!")
        self.button_1.clicked.connect(self.pushButton_1_Pressed)

        self.button_2 = QPushButton("press me !!! second")
        self.button_2.clicked.connect(self.pushButton_2_Pressed)

        layout = QFormLayout(self)
        layout.addRow(self.tr("Name:"), self.nameEdit)
        layout.addRow(self.tr("Address:"), self.addressEdit)
        layout.addRow(self.tr("Receive extra information:"), self.contactCheckBox)

        layout.addWidget(self.button_1)
        layout.addWidget(self.button_2)

    def pushButton_1_Pressed(self):
        print(self.name)
        print(self.address)
        print(self.contact)

    def pushButton_2_Pressed(self):
        self.name = "pippo"
        self.address = ""
        self.address  = "papero"
        self.contact = True
        print(self.address)
        print(self.contact)


if __name__ == "__main__":

    app = QApplication(sys.argv)
    window = Window()
    window.show()
    sys.exit(app.exec_())
  • Related