【问题标题】:Updating PyQT label更新 PyQT 标签
【发布时间】:2017-01-24 02:33:43
【问题描述】:

我正在尝试使用计时器来安排更新网格中的某些值。下面是我尝试根据定时事件更新标签的示例。我已经成功让它调用该函数,但我无法更新标签。有什么想法吗?

import sys
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *

from PyQt5 import QtCore, QtGui, QtWidgets

class App(QWidget):

    def __init__(self):
        super().__init__() #these values change where the main window is placed
        self.title = 'This is my title'
        self.left = 400
        self.top = 400 
        self.width = 300
        self.height = 200
        self.initUI()


    def initUI(self):
        self.setWindowTitle(self.title)
        self.setGeometry(self.left, self.top, self.width, self.height)

        # call the gridlayout function
        self.createGridLayout()
        self.time_label.text = 'change the value'
        windowLayout = QVBoxLayout()
        windowLayout.addWidget(self.horizontalGroupBox)
        self.setLayout(windowLayout)
        self.show() #this sets the main window to the screen size

    def createGridLayout(self): 
        time = self.getTime()
        self.time_label = QLabel(time, self)

        self.horizontalGroupBox = QGroupBox()
        layout = QGridLayout()
        layout.addWidget(QPushButton('1'),0,0) 
        layout.addWidget(QPushButton(time),0,1) 
        layout.addWidget(self.time_label,0,2) 
        self.horizontalGroupBox.setLayout(layout)
    def getTime(self):
        time = QTime.currentTime().toString()
        return time

    def updateTime():
        App.time = QTime.currentTime().toString()      
        time = QTime.currentTime().toString()
        print("Time: " + time)
        # self.time_label = 'change the value'
        # self..layout.time_label = 'asdf'
        return time


def main():
    app = QApplication(sys.argv)
    ex = App()

    timer=QtCore.QTimer()
    timer.timeout.connect(App.updateTime)
    timer.start(1000)

    sys.exit(app.exec_())

if __name__ == '__main__':
    # App.main()
    main()

【问题讨论】:

    标签: python python-3.x pyqt pyqt5


    【解决方案1】:

    你的代码有一些错误,如果你想使用带有保留字self的类的属性,这个方法必须是类的方法,为此它会改变:

    def updateTime():
    

    def updateTime(self):
    

    如果您想更改QLabel 的文本,您必须使用它的setText()

    import sys
    from PyQt5.QtWidgets import *
    from PyQt5.QtCore import *
    
    
    class App(QWidget):
        def __init__(self, parent=None):
            super(App, self).__init__(parent=parent)  # these values change where the main window is placed
            self.title = 'This is my title'
            self.left = 400
            self.top = 400
            self.width = 300
            self.height = 200
            self.initUI()
    
        def initUI(self):
            self.setWindowTitle(self.title)
            self.setGeometry(self.left, self.top, self.width, self.height)
    
            # call the gridlayout function
            self.createGridLayout()
            self.time_label.text = 'change the value'
            windowLayout = QVBoxLayout()
            windowLayout.addWidget(self.horizontalGroupBox)
            self.setLayout(windowLayout)
            self.show()  # this sets the main window to the screen size
    
        def createGridLayout(self):
            time = self.getTime()
            self.time_label = QLabel(time, self)
            self.horizontalGroupBox = QGroupBox()
            layout = QGridLayout()
            layout.addWidget(QPushButton('1'), 0, 0)
            layout.addWidget(QPushButton(time), 0, 1)
            layout.addWidget(self.time_label, 0, 2)
            self.horizontalGroupBox.setLayout(layout)
    
        def getTime(self):
            time = QTime.currentTime().toString()
            return time
    
        def updateTime(self):
            time = QTime.currentTime().toString()
            print("Time: " + time)
            self.time_label.setText(time)
            return time
    
    
    def main():
        app = QApplication(sys.argv)
        ex = App()
    
        timer = QTimer()
        timer.timeout.connect(ex.updateTime)
        timer.start(1000)
    
        sys.exit(app.exec_())
    
    
    if __name__ == '__main__':
        main()
    

    【讨论】:

    • 像魅力一样工作!谢谢
    猜你喜欢
    • 1970-01-01
    • 2016-08-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-26
    • 2023-03-16
    • 1970-01-01
    相关资源
    最近更新 更多