在 UI PyQt5 中更新时钟和文本 [英] Update Clock and Text in UI PyQt5
问题描述
我在输入 UI 运行时的实际时间时遇到问题,还有文本 y 想在特定时间段内更改它.我也尝试在我的例子 Reloj.update() 中使用 MainWindow.update() 但它继续出现同样的问题,把它放在一个循环中这是一个坏主意.
I have a problem to put the actual time while the UI is running, also the text y would like to change it in a certain period of time. I also try to use MainWindow.update() in my case Reloj.update() but it continue with the same problem, put it with an loop it's a bad idea.
我会放一些代码来看看效果如何
I will put some code just to see how is working
UI 由 QtDesigner 制作,然后导出到 python.我的问题是我有一个显示所有内容的 UI,但它不更新时钟和文本.我想在 UI 运行时输入实际时间.此外,文本将在一段时间内发生变化.我想在 15 分钟后更改文本,但在这种情况下,我将延迟"设置为 15 秒,而 Ui 没有更改.
The Ui is made by QtDesigner and then export to python. My problem is that I have an UI that it shows all but it doesn't update the clock and the text. I want to put the actual time while the UI is running. Also the text is going to change in a certain period of time. I want to change the text after 15 minutes, but in this case I put a "delay" of 15 seconds, And the Ui doesn't change.
我也尝试使用 Reloj.update() 更新 Ui,但它也没有改变.
Also i try to update the Ui using Reloj.update(), but it doesn't change too.
示例如下:
from PyQt5 import QtCore, QtGui, QtWidgets
import time
class Ui_Reloj(object):
def setupUi(self, Reloj):
Reloj.setObjectName("Reloj")
Reloj.resize(400, 300)
self.centralWidget = QtWidgets.QWidget(Reloj)
self.centralWidget.setObjectName("centralWidget")
self.gridLayout = QtWidgets.QGridLayout(self.centralWidget)
self.gridLayout.setContentsMargins(11, 11, 11, 11)
self.gridLayout.setSpacing(6)
self.gridLayout.setObjectName("gridLayout")
self.Texto = QtWidgets.QLabel(self.centralWidget)
self.Texto.setObjectName("Texto")
self.gridLayout.addWidget(self.Texto, 0, 0, 1, 1)
self.Reloj_2 = QtWidgets.QLCDNumber(self.centralWidget)
self.Reloj_2.setObjectName("Reloj_2")
self.gridLayout.addWidget(self.Reloj_2, 0, 1, 1, 1)
Reloj.setCentralWidget(self.centralWidget)
self.menuBar = QtWidgets.QMenuBar(Reloj)
self.menuBar.setGeometry(QtCore.QRect(0, 0, 400, 20))
self.menuBar.setObjectName("menuBar")
Reloj.setMenuBar(self.menuBar)
self.mainToolBar = QtWidgets.QToolBar(Reloj)
self.mainToolBar.setObjectName("mainToolBar")
Reloj.addToolBar(QtCore.Qt.TopToolBarArea, self.mainToolBar)
self.statusBar = QtWidgets.QStatusBar(Reloj)
self.statusBar.setObjectName("statusBar")
Reloj.setStatusBar(self.statusBar)
self.retranslateUi(Reloj)
QtCore.QMetaObject.connectSlotsByName(Reloj)
""" Reloj """
time = QtCore.QTime.currentTime()
texto_reloj = time.toString('HH:mm')
if (time.second() % 2) == 0:
texto_reloj = texto_reloj[:2] + ' ' + texto_reloj[3:]
self.Reloj_2.display(texto_reloj)
""" Texto que Cambia """
vec = ['Hola','Que Tal?', 'No se toca', 'paradise']
self.cambiar_texto(vec)
def retranslateUi(self, Reloj):
_translate = QtCore.QCoreApplication.translate
Reloj.setWindowTitle(_translate("Reloj", "Reloj"))
self.Texto.setText(_translate("Reloj", "Texto que cambia"))
def showTime(self):
time = QtCore.QTime.currentTime()
text = time.toString('HH:mm')
if (time.second() % 2) == 0:
text = text[:2] + ' ' + text[3:]
self.Reloj_2.display(text)
""" Cambiar Texto cada X tiempo (ejemplo 15 Minutos) """
def cambiar_texto (self,vec):
i=0
length_string = len(vec)
time.sleep(15)
self.Texto.setText(vec[i])
if (i == 3):
i=0
else:
i+=1
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
Reloj = QtWidgets.QMainWindow()
ui = Ui_Reloj()
ui.setupUi(Reloj)
Reloj.show()
sys.exit(app.exec_())
编译时没有错误,但它应该更新时钟和文本.
There is no errors while it's compiling, but it should update the clock and the text.
推荐答案
您不应在主 GUI 线程中使用 time.sleep(),因为它会阻塞事件循环,而应使用 QTimer.另一方面,不要修改 Qt Designer 生成的代码,而是创建另一个继承自相应小部件的类并使用初始类来填充它.
You should not use time.sleep() in the main GUI thread as it blocks the event loop, instead use QTimer. On the other hand do not modify the code generated by Qt Designer but create another class that inherits from the appropriate widget and use the initial class to fill it.
from itertools import cycle
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Reloj(object):
def setupUi(self, Reloj):
Reloj.setObjectName("Reloj")
Reloj.resize(400, 300)
self.centralWidget = QtWidgets.QWidget(Reloj)
self.centralWidget.setObjectName("centralWidget")
self.gridLayout = QtWidgets.QGridLayout(self.centralWidget)
self.gridLayout.setContentsMargins(11, 11, 11, 11)
self.gridLayout.setSpacing(6)
self.gridLayout.setObjectName("gridLayout")
self.Texto = QtWidgets.QLabel(self.centralWidget)
self.Texto.setObjectName("Texto")
self.gridLayout.addWidget(self.Texto, 0, 0, 1, 1)
self.Reloj_2 = QtWidgets.QLCDNumber(self.centralWidget)
self.Reloj_2.setObjectName("Reloj_2")
self.gridLayout.addWidget(self.Reloj_2, 0, 1, 1, 1)
Reloj.setCentralWidget(self.centralWidget)
self.menuBar = QtWidgets.QMenuBar(Reloj)
self.menuBar.setGeometry(QtCore.QRect(0, 0, 400, 20))
self.menuBar.setObjectName("menuBar")
Reloj.setMenuBar(self.menuBar)
self.mainToolBar = QtWidgets.QToolBar(Reloj)
self.mainToolBar.setObjectName("mainToolBar")
Reloj.addToolBar(QtCore.Qt.TopToolBarArea, self.mainToolBar)
self.statusBar = QtWidgets.QStatusBar(Reloj)
self.statusBar.setObjectName("statusBar")
Reloj.setStatusBar(self.statusBar)
self.retranslateUi(Reloj)
QtCore.QMetaObject.connectSlotsByName(Reloj)
def retranslateUi(self, Reloj):
_translate = QtCore.QCoreApplication.translate
Reloj.setWindowTitle(_translate("Reloj", "Reloj"))
self.Texto.setText(_translate("Reloj", "Texto que cambia"))
class MainWindow(QtWidgets.QMainWindow, Ui_Reloj):
def __init__(self, parent=None):
super().__init__(parent)
self.setupUi(self)
timer1 = QtCore.QTimer(self, interval=1000, timeout=self.showTime)
timer1.start()
self.showTime()
vec = ["Hola", "Que Tal?", "No se toca", "paradise"]
self.texts = cycle(vec)
timer2 = QtCore.QTimer(self, interval=15 * 1000, timeout=self.cambiar_texto)
timer2.start()
self.cambiar_texto()
@QtCore.pyqtSlot()
def showTime(self):
time = QtCore.QTime.currentTime()
text = time.toString("HH mm" if time.second() % 2 == 0 else "HH:mm")
self.Reloj_2.display(text)
@QtCore.pyqtSlot()
def cambiar_texto(self):
text = next(self.texts)
self.Texto.setText(text)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())
这篇关于在 UI PyQt5 中更新时钟和文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!