更新 PyQT 标签
Posted
技术标签:
【中文标题】更新 PyQT 标签【英文标题】:Updating PyQT label 【发布时间】: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()
【问题讨论】:
【参考方案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()
【讨论】:
像魅力一样工作!谢谢以上是关于更新 PyQT 标签的主要内容,如果未能解决你的问题,请参考以下文章
PyQt5 - 使用 Line Edit 从编辑窗口更新标签