gpt4 book ai didi

python - 更新 UI PyQt5 中的时钟和文本

转载 作者:行者123 更新时间:2023-12-01 07:15:52 33 4
gpt4 key购买 nike

我在 UI 运行时输入实际时间时遇到问题,而且文本 y 希望在特定时间段内更改它。我还尝试在我的例子 Reloj.update() 中使用 MainWindow.update() 但它继续出现同样的问题,将其放在循环中这是一个坏主意。

我将放置一些代码来看看它是如何工作的

Ui由QtDesigner制作,然后导出到python。我的问题是我有一个显示所有内容但不更新时钟和文本的用户界面。我想输入用户界面运行时的实际时间。此外,文本也会在一段时间内发生变化。我想在 15 分钟后更改文本,但在本例中我设置了 15 秒的“延迟”,并且 Ui 没有更改。

我还尝试使用 Reloj.update() 更新 Ui,但它也没有改变。

示例如下:

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

编译时没有错误,但应该更新时钟和文本。

最佳答案

您不应在主 GUI 线程中使用 time.sleep(),因为它会阻塞事件循环,而应使用 QTimer。另一方面,不要修改 Qt Designer 生成的代码,而是创建另一个从适当的小部件继承的类,并使用初始类来填充它。

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

关于python - 更新 UI PyQt5 中的时钟和文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57958870/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com