我有一个问题,就是在用户界面运行时输入实际时间,而且文本y想在某个时间段内更改它。我也试着用主窗口.更新()就我而言重新登记更新()但它仍然存在同样的问题,用循环来处理它是个坏主意。在
我会放一些代码来看看是怎么工作的
Ui由QtDesigner生成,然后导出到python。 我的问题是我有一个用户界面,它显示所有,但它不更新时钟和文本。 我想在UI运行时输入实际时间。 文本在一定时期内也会发生变化。我想在15分钟后更改文本,但在本例中,我将“延迟”设置为15秒,用户界面不会更改。在
我还尝试使用重新登记更新(),但它也没有改变。在
示例如下:
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线程中,因为它阻止事件循环,请改用QTimer。另一方面,不要修改Qt Designer生成的代码,而是创建另一个继承自相应小部件的类,并使用初始类填充它。在
相关问题 更多 >
编程相关推荐