更新PyQT标签

时间:2017-01-24 02:33:43

标签: python python-3.x pyqt pyqt5

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

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

1 个答案:

答案 0 :(得分: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()

enter image description here

enter image description here