我现在正在用pyqt做一个计时器。 如果我在 self.now 中输入时间,该时间将被递减计数。
时间减少到零时,会弹出一个窗口,提示“重试一次?”
当我按下“是”按钮时,我想再次倒数。 按下“是”按钮时,此代码不会设置 self.now 。
倒数窗口
弹出窗口
这是我的代码
import sys
from PyQt5 import QtCore, QtGui, uic
from PyQt5 import QtWidgets
from PyQt5 import uic
from PyQt5.QtCore import pyqtSlot
class MainWindow(QtWidgets.QDialog):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
uic.loadUi('Mainwindow.ui', self)
self.timer = QtCore.QTimer()
self.now = 10
self.cnt_set = 0
self.timer.timeout.connect(self.tick_timer)
self.timer.start(1000)
self.update_timer()
def update_timer(self):
self.runtime = "%02d:%02d" % (self.now/60,self.now % 60)
self.lcdNumber.display(self.runtime)
if self.now == 0:
self.cnt_set += 1
print(self.cnt_set)
self.stop_timer()
self.mw_CONTINUE = CONTINUE()
self.mw_CONTINUE.show()
def tick_timer(self):
self.now -= 1
self.update_timer()
def stop_timer(self):
self.timer.stop()
class CONTINUE(QtWidgets.QDialog):
def __init__(self, parent=None):
QtWidgets.QDialog.__init__(self, parent)
self.ui = uic.loadUi("continue.ui", self)
self.Button1.clicked.connect(self.B1_clicked)
def B1_clicked(self):
self.hide()
print("B1 clicked!")
MainWindow()
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
mw_NFC = MainWindow()
mw_NFC.show()
sys.exit(app.exec_())
答案 0 :(得分:0)
我为您指出了我更改的内容。 试试吧。
import sys
from PyQt5 import QtCore, QtGui, QtWidgets #, uic
#from PyQt5.QtCore import pyqtSlot
class MainWindow(QtWidgets.QDialog):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
# uic.loadUi('Mainwindow.ui', self)
self.lcdNumber = QtWidgets.QLCDNumber(self)
self.timer = QtCore.QTimer()
self.now = 10
self.cnt_set = 0
self.timer.timeout.connect(self.tick_timer)
# self.timer.start(1000) # -
self.update_timer()
def update_timer(self):
self.timer.start(1000) # +
self.runtime = "%02d:%02d" % (self.now / 60, self.now % 60)
self.lcdNumber.display(self.runtime)
if self.now == 0:
self.cnt_set += 1
print("cnt_set = {}".format(self.cnt_set))
self.stop_timer()
self.mw_CONTINUE = CONTINUE(self) # + self
self.mw_CONTINUE.show()
def tick_timer(self):
self.now -= 1
self.update_timer()
def stop_timer(self):
self.timer.stop()
class CONTINUE(QtWidgets.QDialog):
def __init__(self, parent=None):
QtWidgets.QDialog.__init__(self, parent)
self.parent = parent # +
# self.ui = uic.loadUi("continue.ui", self)
self.Button1 = QtWidgets.QPushButton("Try again?", self)
self.Button1.clicked.connect(self.B1_clicked)
def B1_clicked(self):
self.hide()
print("B1 clicked!")
# MainWindow() # -
self.parent.now = 10 # +
self.parent.update_timer() # +
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
mw_NFC = MainWindow()
mw_NFC.show()
sys.exit(app.exec_())