我正在尝试使用PyQt5创建一个简单的应用程序,并且试图设置一个标签来显示每0.01秒的时间,但是一小段时间后它就会停止。
当我使用print(self.label_9.text())
时,循环不会停止。我看到该软件改变了它的价值,但是当我看屏幕时,标签只是没有变化。
这是我所有的代码(类中的计时器函数是计时器):
from PyQt5 import QtCore, QtGui, QtWidgets
import threading
from time import sleep
from datetime import datetime
import mouse
def extract(string) :
string = string[12:]
spliter = string.split(':')
hours = int(spliter[0])
minutes = int(spliter[1])
spliter = spliter[2].split('.')
seconds = int(spliter[0])
miliseconds = int(spliter[1])
return hours,minutes,seconds,miliseconds
class Ui_Dialog(object):
killer = False
funcKiller = False
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.resize(404, 262)
self.label = QtWidgets.QLabel(Dialog)
self.label.setGeometry(QtCore.QRect(20, 20, 311, 21))
self.label.setObjectName("label")
self.label_2 = QtWidgets.QLabel(Dialog)
self.label_2.setGeometry(QtCore.QRect(30, 50, 321, 21))
self.label_2.setObjectName("label_2")
self.spinBox = QtWidgets.QSpinBox(Dialog)
self.spinBox.setGeometry(QtCore.QRect(223, 100, 40, 22))
self.spinBox.setMaximum(59)
self.spinBox.setObjectName("spinBox")
self.spinBox_2 = QtWidgets.QSpinBox(Dialog)
self.spinBox_2.setGeometry(QtCore.QRect(316, 100, 42, 22))
self.spinBox_2.setMaximum(23)
self.spinBox_2.setObjectName("spinBox_2")
self.label_3 = QtWidgets.QLabel(Dialog)
self.label_3.setGeometry(QtCore.QRect(366, 100, 31, 21))
self.label_3.setObjectName("label_3")
self.label_4 = QtWidgets.QLabel(Dialog)
self.label_4.setGeometry(QtCore.QRect(265, 100, 31, 21))
self.label_4.setObjectName("label_4")
self.spinBox_3 = QtWidgets.QSpinBox(Dialog)
self.spinBox_3.setGeometry(QtCore.QRect(130, 100, 42, 22))
self.spinBox_3.setMaximum(59)
self.spinBox_3.setObjectName("spinBox_3")
self.label_5 = QtWidgets.QLabel(Dialog)
self.label_5.setGeometry(QtCore.QRect(170, 100, 31, 21))
self.label_5.setObjectName("label_5")
self.spinBox_4 = QtWidgets.QSpinBox(Dialog)
self.spinBox_4.setGeometry(QtCore.QRect(20, 100, 42, 22))
self.spinBox_4.setMaximum(999)
self.spinBox_4.setObjectName("spinBox_4")
self.label_6 = QtWidgets.QLabel(Dialog)
self.label_6.setGeometry(QtCore.QRect(62, 100, 51, 21))
self.label_6.setObjectName("label_6")
self.pushButton = QtWidgets.QPushButton(Dialog)
self.pushButton.setGeometry(QtCore.QRect(103, 150, 191, 31))
self.pushButton.setObjectName("pushButton")
self.line = QtWidgets.QFrame(Dialog)
self.line.setGeometry(QtCore.QRect(-3, 197, 411, 20))
self.line.setFrameShape(QtWidgets.QFrame.HLine)
self.line.setFrameShadow(QtWidgets.QFrame.Sunken)
self.line.setObjectName("line")
self.label_9 = QtWidgets.QLabel(Dialog)
self.label_9.setGeometry(QtCore.QRect(22, 226, 361, 21))
font = QtGui.QFont()
font.setPointSize(20)
self.label_9.setFont(font)
self.label_9.setObjectName("label_9")
self.retranslateUi(Dialog)
QtCore.QMetaObject.connectSlotsByName(Dialog)
self.pushButton.clicked.connect(self.button1)
Dialog.finished.connect(self.button)
threading.Thread(target=self.timer).start()
def button(self) :
self.killer = True
def timer(self) :
while True :
try :
if not self.killer :
sleep(0.01)
print("working")
self.label_9.setText(str(datetime.now()))
print(self.label_9.text())
else :
print("it is broken")
break
except Exception as e :
print(str(e))
def func(self) :
while True :
try :
print('func 1')
if self.funcKiller :
self.funcKiller = False
break
if self.killer :
break
print('func 2')
hours = self.spinBox_2.value()
minutes = self.spinBox.value()
seconds = self.spinBox_3.value()
miliseconds = self.spinBox_4.value()
print('func 3')
t = datetime.now()
h,m,s,m_s = extract(str(t))
print('func 4')
if h >= hours and m >= minutes and s >= seconds and m_s >= miliseconds :
mouse.click()
break
sleep(0.0005)
print('func 5')
except Exception as e :
print(str(e))
def button1(self) :
'''
This is a start operation button
'''
try :
self.funcKiller = True
sleep(0.01)
threading.Thread(target=self.func).start()
except Exception as e :
print(str(e))
def retranslateUi(self, Dialog):
_translate = QtCore.QCoreApplication.translate
Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
self.label.setText(_translate("Dialog", "لطفا زمانی که میخواهید کلیک انجام شود را با دقت بنویسید "))
self.label_2.setText(_translate("Dialog", "سپس موس را روی جایی که میخواهید روی آن کلیک شود نگه دارید"))
self.label_3.setText(_translate("Dialog", "ساعت"))
self.label_4.setText(_translate("Dialog", "دقیقه"))
self.label_5.setText(_translate("Dialog", "ثانیه"))
self.label_6.setText(_translate("Dialog", "میلی ثانیه"))
self.pushButton.setText(_translate("Dialog", "ثبت"))
self.label_9.setText(_translate("Dialog", "2016-15-01 13:01:01.655834"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
Dialog = QtWidgets.QDialog()
ui = Ui_Dialog()
ui.setupUi(Dialog)
Dialog.show()
sys.exit(app.exec_())