[英]Timer Using QTime in PyQt5
I used this code to manage a countdown using PyQt5.我使用此代码使用 PyQt5 管理倒计时。 code -
代码 -
import time
import serial
from PyQt5 import QtCore
argument = serial.Serial('COM5',9600)
countDown = 9999
def timerEvent():
global time
time = time.addSecs(-1)
global argument
global new
data = time.toString("mmss")
print(data)
argument.write(data.encode());
if data == '0000':
argument.close()
timer.stop()
global countDown
countDown = data
time.sleep(2);
app = QtCore.QCoreApplication(sys.argv)
timer = QtCore.QTimer()
time = QtCore.QTime(0, 1,0)
timer.timeout.connect(timerEvent)
timer.start(200)
But i want is i will input the minute and increment value using a GUI.But, can not implement.但我想要的是我将使用 GUI 输入分钟和增量值。但是,无法实现。 Any suggestions how can i implement that?
有什么建议我该如何实施? heres my GUI
这是我的图形用户界面
I created a new self function under class Ui_Mainwindow that connects the button to implement timer.我在 class Ui_Mainwindow 下创建了一个新的自我 function ,它连接按钮以实现定时器。 However, it always says self error.
但是,它总是说自我错误。 i know how to get the lineEdit values but when i want to put it in
我知道如何获取 lineEdit 值但是当我想把它放进去时
line 38 time = QtCore.QTime(0, 1,0)
it shows self error.第 38 行
time = QtCore.QTime(0, 1,0)
它显示自我错误。 again the line no 40 timer.timeout.connect(timerEvent)
timerevent shows error.第 40 行
timer.timeout.connect(timerEvent)
timerevent 再次显示错误。
any idea how to implement using the GUI?知道如何使用 GUI 实现吗? Sorry for my poor explanation.
对不起,我的解释很糟糕。 is there any other way to do this?
有没有其他方法可以做到这一点?
heres the code, i just tried to do similar like base code mentioned above.继承人的代码,我只是尝试做类似上面提到的基本代码。
import time
countDown = 9999
from PyQt5 import QtCore, QtGui, QtWidgets
inc = 9999
newTime = 9999
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800, 600)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.groupBox = QtWidgets.QGroupBox(self.centralwidget)
self.groupBox.setGeometry(QtCore.QRect(70, 180, 131, 121))
self.groupBox.setTitle("")
self.groupBox.setObjectName("groupBox")
self.label_2 = QtWidgets.QLabel(self.groupBox)
self.label_2.setGeometry(QtCore.QRect(6, 50, 51, 20))
self.label_2.setObjectName("label_2")
self.lineEdit_Time = QtWidgets.QLineEdit(self.groupBox)
self.lineEdit_Time.setGeometry(QtCore.QRect(60, 20, 61, 20))
self.lineEdit_Time.setObjectName("lineEdit_Time")
self.lineEdit_Increment = QtWidgets.QLineEdit(self.groupBox)
self.lineEdit_Increment.setGeometry(QtCore.QRect(60, 50, 61, 20))
self.lineEdit_Increment.setObjectName("lineEdit_Increment")
self.pushButton_Start = QtWidgets.QPushButton(self.groupBox)
self.pushButton_Start.setGeometry(QtCore.QRect(10, 90, 111, 23))
self.pushButton_Start.setObjectName("pushButton_Start")
self.label = QtWidgets.QLabel(self.groupBox)
self.label.setGeometry(QtCore.QRect(10, 20, 47, 13))
self.label.setObjectName("label")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 21))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
self.pushButton_Start.clicked.connect(self.update)
global inc
inc = self.lineEdit_Time.text()
global newTime
newTime = self.lineEdit_Increment.text()
def update(self):
global time
time = time.addSecs(-1)
global argument
global new
data = time.toString("mmss")
print(data)
argument.write(data.encode());
if data == '0000':
timer.stop()
global countDown
countDown = data
return time
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.label_2.setText(_translate("MainWindow", "Increment"))
self.pushButton_Start.setText(_translate("MainWindow", "START"))
self.label.setText(_translate("MainWindow", "Time"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
global time
timer = QtCore.QTimer()
time = QtCore.QTime(0, inc,0)
timer.timeout.connect(update)
timer.start(newTime)
MainWindow.show()
sys.exit(app.exec_())
Something like this?像这样的东西? You want to access your increment and start time from the text values of the self.lineEdit_Time and self.lineEdit_Increment widgets.
您想从 self.lineEdit_Time 和 self.lineEdit_Increment 小部件的文本值访问您的增量和开始时间。 Then you use this in a start_timer method, which you connect to the start buttons clicked signal.
然后在 start_timer 方法中使用它,该方法连接到启动按钮单击信号。 In the start_timer method, we create a timer which is a member of the main_window object, and this timer's timeout signal is connected to our update_clock method, which updates the countdown based on the self.increment value.
在 start_timer 方法中,我们创建了一个定时器,它是 main_window object 的成员,这个定时器的超时信号连接到我们的 update_clock 方法,它根据 self.increment 值更新倒计时。
class Ui_MainWindow(object):
#...
self.pushButton_Start.pressed.connect(self.start_timer)
def start_timer(self):
self.countdown = int(self.lineEdit_Time.text())
self.increment = int(self.lineEdit_Increment.text())
self.countdownTimer = QtCore.QTimer()
self.countdownTimer.timeout.connect(self.update_clock)
self.countdownTimer.start(1000) # fires every 1000ms = 1s
def update_clock(self):
self.countdown -= self.increment
#update a clock label that shows the countdown value
self.clockLabel.setText(self.countdown)
#or just print the vaule for now
print(self.countdown)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.