更新PyQT实验室

2024-04-20 03:24:25 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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

Tags: thefromimportselfapptimemaindef
1条回答
网友
1楼 · 发布于 2024-04-20 03:24:25

您的代码有一些错误,如果您要使用带有保留字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

相关问题 更多 >