简体   繁体   中英

how to update pyqt5 GUI label led icons(pixmap)

I have main.py file which contain all threads and dictionary, one is my GUI thread which i have define in main.py file.

now in my gui thread i have define a function to

gui.py

class Ui_MainWindow(object):
    def setupUi(self, MainWindow, object_dictionary):

        self.closed_led = QtWidgets.QLabel(self.central_widget)
        self.closed_led.setGeometry(QtCore.QRect(910, 70, 61, 61))
        self.closed_led.setText("")
        self.closed_led.setPixmap(QtGui.QPixmap("black.jpg"))
        self.closed_led.setScaledContents(True)
        self.closed_led.setObjectName("closed_led")
        self.update_label(object_dictionary)
        self.timer = QTimer()
        self.timer.timeout.connect(lambda: self.update_label(object_dictionary))
        self.timer.start(1000)  # repeat self.update_label every 1 sec

    def update_label(self, object_dictionary):
    
        if object_dictionary['fridge_closed'] != 0:
            self.closed_led.setPixmap(QtGui.QPixmap("green.jpg"))
            print("green")
        else:
            self.closed_led.setPixmap(QtGui.QPixmap("black.jpg"))
            print("black")

but i want this updatelabel to keep checking the if any input is given in dictionary, if fridge_closed = 1 then the led should become green and if fridge_closed = 0 then led should become black automatically. Do i need to use worker thread for this, and if yes then how to assign signal slot.

You could make object_dictionary a member of the Gui class and wrap all edits to the dictionary in a method that emits a signal. When you need to edit the dictionary outside of the class, just use editDictionary() from the class instance.

class MainWindow(QtWidgets.QWidget):

    # Signal for when dictionary is changed
    objectDictionaryChanged = QtCore.Signal()

    def __init__(self):
        super(MainWindow, self).__init__()
        self.mainLayout = QtWidgets.QVBoxLayout()
        self.setLayout(self.mainLayout)
        # Object dictionary becomes part of the class
        self.object_dictionary = {}

        self.closed_led = QtWidgets.QLabel()
        # A spinbox that edits the value of 'fridge_closed' in the dictionary
        self.dictionarySpinBox = QtWidgets.QSpinBox()
        self.dictionarySpinBox.setMinimum(0)
        self.dictionarySpinBox.setMaximum(1)

        # The connections that handle the changes
        self.objectDictionaryChanged.connect(self.update_label)
        self.dictionarySpinBox.valueChanged.connect(
            lambda: self.editDictionary('fridge_closed', self.dictionarySpinBox.value())
        )

        self.mainLayout.addWidget(self.closed_led)
        self.mainLayout.addWidget(self.dictionarySpinBox)

        self.dictionarySpinBox.setValue(1)

    def editDictionary(self, key, value):
        # All edits to object dictionary should pass through here
        self.object_dictionary[key] = value
        self.objectDictionaryChanged.emit()

    def update_label(self):
        state = self.object_dictionary['fridge_closed']

        if state is 0:
            self.closed_led.setPixmap(QtGui.QPixmap("black.jpg"))
        elif state is 1:
            self.closed_led.setPixmap(QtGui.QPixmap("green.jpg"))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM