暂停后重启时间

时间:2019-09-21 19:20:33

标签: python tkinter

我正在尝试在tkinter(python)中进行一些倒计时 我已经设法使它正常工作,但是当我在启动时暂停时,它从头开始。

如何使暂停按钮在暂停后继续实际时间?

谢谢

    def countdown(self):


    if self.state == True:

        if (self.mins == 0) and (self.secs == 0):
            self.display.config(text="Done!")
            self.state = False
        else:
            self.display.config(text="%02d:%02d" % (self.mins, self.secs))

            if self.secs == 0:
                self.mins -= 1
                self.secs = 59
            else:
                self.secs -= 1

            self.master.after(1000, self.countdown)

def start(self):
    if self.state == False:
        self.state = True
        self.mins = self.minutes
        self.secs = self.seconds
        self.countdown()

def pause(self):
    if self.state == True:
        self.state = False

1 个答案:

答案 0 :(得分:0)

class Counter:
    def __init__(self, minutes, seconds):
        self.minutes = minutes
        self.seconds = seconds 
        self.state = False
    def countdown(self):
        if self.state == True:
            if (self.mins == 0) and (self.secs == 0):
                print("Done!")
                #self.display.config(text="Done!")
                #self.state = False
            else:
                #self.display.config(text="%02d:%02d" % (self.mins, self.secs))
                if self.secs == 0:
                    self.mins -= 1
                    self.secs = 59
                else:
                    self.secs -= 1
                #self.master.after(1000, self.countdown)
    def start(self):
        if self.state == False:
            self.state = True
            self.mins = self.minutes
            self.secs = self.seconds
            self.countdown()

    def pause(self):
        if self.state == True:
            self.state = False
            #Add these lines into your code#
            self.minutes = self.mins
            self.seconds = self.secs
obj1 = Counter(10,40)
obj1.start()
obj1.pause()
obj1.start()
print(obj1.minutes, obj1.seconds)

我修改了您的代码以在计算机上运行。我在pause方法中添加了两行。 编辑:缩进错误已修复。