Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/340.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 有没有一种方法可以迭代时间,然后返回函数?_Python_Python 3.x_Tkinter - Fatal编程技术网

Python 有没有一种方法可以迭代时间,然后返回函数?

Python 有没有一种方法可以迭代时间,然后返回函数?,python,python-3.x,tkinter,Python,Python 3.x,Tkinter,我正在与Tkinter一起开发Pomotoro应用程序。我想将工作/休息时间发送到一个函数,以倒计时发送的时间。这些工作/休息时间应在规定的会话数内循环 我的尝试是将值发送到函数,并在使用时间计数器完成后返回。当我与代码分离时,计数器代码为我工作。但我的问题是,完成后如何返回 class Window(Frame): def __init__(self, master = None): Frame.__init__(self, master) self.m

我正在与Tkinter一起开发Pomotoro应用程序。我想将工作/休息时间发送到一个函数,以倒计时发送的时间。这些工作/休息时间应在规定的会话数内循环

我的尝试是将值发送到函数,并在使用时间计数器完成后返回。当我与代码分离时,计数器代码为我工作。但我的问题是,完成后如何返回

class Window(Frame):
    def __init__(self, master = None):
        Frame.__init__(self, master)
        self.master = master
        self.init__window()

        self.remaining = 0
        self.remainingSessions = 0
        
        self.countdownLabel = Label(self, text="", width=10, height=5)
        self.countdownLabel.pack()

        self.work = 5
        self.rest = 3
        self.sessions = 2

        self.startButton = Button(self, text="Start", command= partial(self.sessionsCountdown, self.work, self.rest, self.sessions))
        self.startButton.pack()

    def init__window(self):
        self.master.title("Pomodoro")
        self.pack(fill=BOTH, expand=1)

    def sessionsCountdown(self, remainingWork = None, remainingRest = None, sessions = None):
        self.remainingSessions = sessions

        while self.remainingSessions >= 0:
            print("session number = ", self.remainingSessions)
            self.countdown(remainingWork)
            print("done with work")
            self.countdown(remainingRest)
            print("done with rest")
            self.remainingSessions = self.remainingSessions - 1 

        self.countdownLabel.configure(text="time's up!")
          
    def countdown(self, remaining = None):
        if remaining is not None:
            self.remaining = remaining

        print(remaining)

        if self.remaining <= 0:
            return

        else:
            mins, secs = divmod(self.remaining, 60)
            timer = '{:02}:{:02d}'.format(mins, secs)
            self.countdownLabel.configure(text=timer)
            self.remaining = self.remaining - 1
            self.after(1000, self.countdown)
不,你不能

GUI使用“事件驱动编程”,这与大多数人最初学习的过程编程非常不同。当您使用类似于
self.after
的函数时,您将安排一个事件稍后发生,但同时允许其他事件发生。重要的事情,如响应点击和更新显示,还有一些小事情,如移动窗口或在鼠标悬停在窗口上时稍微更改按钮颜色


要想做你想做的事情,你需要设置一些变量来跟踪你在系列中的位置并使用它们。我认为您知道这一点,因为您已经有了这些实例变量。剩下的就是在你的
if self中触发右边的一个。非常感谢,这个概念对我很有用,让我可以开发更多的功能。你的代码很容易理解。对于剩余=0是倒计时函数的用法,我只是有点困惑。你能告诉我你为什么添加它吗?啊,是的,这是一个叫做“打字错误”的高级功能。我打算展示一种不同的倒计时方法,它涉及到一个论点,然后决定反对,但忘了删除论点。哈哈哈,谢谢你解释这个特性。这确实很普遍。再次感谢你的帮助
session number =  2
5
done with work     
3
done with rest     
session number =  1
5
done with work     
3
done with rest     
session number =  0
5
done with work     
3
done with rest     
None
None
None
None
None
None
None
None
from tkinter import *

class Window(Frame):
    def __init__(self, master = None):
        super().__init__(master) # the modern way
        # self.master = master # not needed
        self.init__window()

        self.remaining = 0
        self.remainingSessions = 3
        self.current = None

        self.current_activity = Label(self, text="", width=10, height=5)
        self.current_activity.pack()
        self.countdownLabel = Label(self, text="", width=10, height=5)
        self.countdownLabel.pack()

        self.startButton = Button(self, text="Start", command= self.sessionsCountdown)
        self.startButton.pack()

    def init__window(self):
        self.master.title("Pomodoro")
        self.pack(fill=BOTH, expand=1) # this line is traditionally not in the class

    def sessionsCountdown(self):
        '''choose the right activity to trigger'''
        if self.remainingSessions <= 0:
            self.current_activity.config(text="Done!")
            self.countdownLabel.configure(text="time's up!")
            return
        else:
            self.remainingSessions -= 1

        if self.current == "resting":
            self.current = 'working'
            self.current_activity.config(text="Working:")
            self.remaining = 5
        else:
            self.current = 'resting'
            self.current_activity.config(text="Resting:")
            self.remaining = 3
        self.countdown() # start the timer again.

    def countdown(self, remaining=0):
        if self.remaining <= 0:
            self.sessionsCountdown()
        else:
            mins, secs = divmod(self.remaining, 60)
            timer = '{:02}:{:02d}'.format(mins, secs)
            self.countdownLabel.configure(text=timer)
            self.remaining = self.remaining - 1
            self.after(1000, self.countdown)
r = Tk()
Window(r)
r.mainloop()