我试图让QLabel使用QTimer来更新自己,但是使用.setText()
时文本不会更新。如果我在self.price_label
上使用.show()
,它将在QLabel的顶部重新绘制文本,因此这显然不是正确的方法
from PyQt5 import QtWidgets, QtGui, QtCore
import getters
class Window(QtWidgets.QMainWindow):
def __init__(self):
super(Window, self).__init__()
self.setGeometry(50, 50, 500, 300)
self.setWindowTitle("Hello There!")
self.setWindowIcon(QtGui.QIcon('logo.png'))
self.exitMenu = QtWidgets.QAction(" &Exit", self)
self.exitMenu.setShortcut("Ctrl+Q")
self.exitMenu.setStatusTip("Leave the app")
self.exitMenu.triggered.connect(self.close_application)
self.statusBar()
self.mainMenu = self.menuBar()
self.fileMenu = self.mainMenu.addMenu("&File")
self.fileMenu.addAction(self.exitMenu)
self.timer = QtCore.QTimer(self)
self.timer.timeout.connect(self.home)
self.timer.start(5000)
def home(self):
self.quit_button = QtWidgets.QPushButton("Quit", self)
self.quit_button.clicked.connect(QtCore.QCoreApplication.instance().quit)
self.quit_button.resize(100,100)
self.quit_button.move(100, 70)
self.symbol = getters.pair.get_price().get("symbol")
self.price = getters.pair.get_price().get("price")
print(self.symbol)
print(self.price)
self.price_label = QtWidgets.QLabel("%s %s" % (self.symbol, self.price), self)
self.price_label.resize(200, 100)
self.price_label.move(200, 100)
self.price_label.setText("%s %s" % (self.symbol, self.price))
QtWidgets.QApplication.processEvents()
self.show()
def close_application(self):
sys.exit()
def run():
app = QtWidgets.QApplication(sys.argv)
GUI = Window()
sys.exit(app.exec_())
run()
我猜我的QTimer可能放错了位置,它没有正确地为QLabel重新绘制文本
你写这篇文章的方式是每五秒钟设置几个UI项。
home
方法中的大部分内容应该移到__init__
。这样,UI只在程序启动时设置如果您只想每五秒钟更改一次标签的文本,请尝试以下操作:
如果你的home方法中的所有内容不在我的方法中,请转到
__init__
。还要确保在__init__
中包含self.symbol
和self.price
的第一个实例相关问题 更多 >
编程相关推荐