簡體   English   中英

如何更新 pyqt5 GUI label LED 圖標(像素圖)

[英]how to update pyqt5 GUI label led icons(pixmap)

我有包含所有線程和字典的 main.py 文件,一個是我在 main.py 文件中定義的 GUI 線程。

現在在我的 gui 線程中,我定義了一個 function 到

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")

但我希望此更新標簽繼續檢查字典中是否有任何輸入,如果冰箱關閉 = 1,則 LED 應變為綠色,如果冰箱關閉 = 0,則 LED 應自動變為黑色。 我是否需要為此使用工作線程,如果是,那么如何分配信號槽。

您可以使object_dictionary成為 Gui class 的成員,並將所有編輯內容包裝到一個發出信號的方法中。 當您需要在 class 之外編輯字典時,只需使用 class 實例中的editDictionary()即可。

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"))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM