【问题标题】:How to make the class run again如何让课堂再次运行
【发布时间】:2019-11-03 22:17:40
【问题描述】:

我现在正在用 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_())

【问题讨论】:

    标签: python pyqt


    【解决方案1】:

    我已为您记录了我更改的行。 试试看。

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

    【讨论】:

      猜你喜欢
      • 2013-11-08
      • 2016-10-10
      • 2017-03-25
      • 2013-04-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多