简体   繁体   中英

Python cant resolve countdown timer

Hello how are you? I wanted to ask you two things, I'm creating a countdown timer, but I can't make it go to seconds when a minute ends, and the other question is that I can't figure out how to stop the time so that later if I click start it returns from where I am paused.

class Product:

    def __init__(self, window):
      
        self.tablero_principal = window
        self.tablero_principal.title('Tablero')
        self.tablero_principal.attributes('-fullscreen', True)
        self.tablero_principal.configure(background='BLACK')

        frame_tablero_arriba = customtkinter.CTkFrame(self.tablero_principal, corner_radius=0, fg_color='black')
   
        self.text_minutos = StringVar(value="12") 
        self.text_segundos = StringVar(value="12")   

        self.label_minutos = Label(frame_tablero_arriba, font = ('DS-Digital', 270,BOLD),textvariable=self.text_minutos,bg = 'black', foreground = 'red') 
        self.label_minutos.grid(column=1,row=0,rowspan=6)

        self.label_puntos = Label(frame_tablero_arriba, font = ('DS-Digital', 270,BOLD),text=":",bg = 'black', foreground = 'red') 
        self.label_puntos.grid(column=2,row=0,rowspan=6)

        self.label_segundos = Label(frame_tablero_arriba, font = ('DS-Digital', 270,BOLD),text="12",bg = 'black', foreground = 'red') 
        self.label_segundos.grid(column=3,row=0,rowspan=6)

        button_tiempo_iniciar = customtkinter.CTkButton(master=frame_tablero_arriba,width=120,command=lambda :self.countdown(self.text_minutos.get()), corner_radius=4,fg_color="darkorange",hover_color="darkorange4", text='Iniciar')
        button_tiempo_iniciar.grid(column=4,row=0)        

        button_tiempo_parar = customtkinter.CTkButton(master=frame_tablero_arriba,width=120, corner_radius=4,fg_color="darkorange",hover_color="darkorange4", text='Detener')
        button_tiempo_parar.grid(column=4,row=1)

        button_tiempo_modificar = customtkinter.CTkButton(master=frame_tablero_arriba,width=120, corner_radius=4,fg_color="darkorange",hover_color="darkorange4", text='Modificar')
        button_tiempo_modificar.grid(column=4,row=2)

    def countdown(self,minutos):
      minutos_final=int(minutos)
      self.text_minutos.set(minutos_final)

      if minutos_final > 0:
        self.tablero_principal.after(1000, self.countdown, minutos_final-1)       

To go to seconds, you will have to put an else in countdown, perhaps calling another function that uses 60 seconds and counts down. A start & stop example.

import sys
if 3 == sys.version_info[0]:  ## 3.X is default if dual system
    import tkinter as tk     ## Python 3.x
    print("Using version 3")
else:
    import Tkinter as tk     ## Python 2.x
    print("Using version 2")

def test():
    """
    """
    class TimerTest():
        def __init__(self, root):
            self.root=root

            self.is_running=False
            self.count=tk.IntVar()
            self.max_seconds=60
            tk.Label(root, textvariable=self.count, font=('DejaVuSansMono', 12, "bold"),
                  bg="lightyellow").grid(row=1, column=0, columnspan=2, sticky="ew")

            tk.Button(root, text="Start", fg="blue", width=15,
                                command=self.startit).grid(row=10, column=0, sticky="nsew")
            tk.Button(root, text="Stop", fg="red", width=15,
                                command=self.stopit).grid(row=10, column=1, sticky="nsew")
            tk.Button(self.root, text="Quit", bg="orange",
                                command=self.root.quit).grid(row=11, column=0,
                                columnspan=2, sticky="nsew")

        def startit(self):
            if not self.is_running:  ## avoid 2 button pushes
                self.is_running=True
                self.increment_counter()

        def increment_counter(self):
            if self.is_running:
                 c=self.count.get() +1
                 self.count.set(c)
                 if c < self.max_seconds:
                     self.root.after(1000, self.increment_counter)  ## every second
                 else:
                     self.is_running=False
                     tk.Label(root, text="Time Is Up", font=('DejaVuSansMono', 14, "bold"),
                     bg="red").grid(row=5, column=0, columnspan=2, sticky="ew")

        def stopit(self):
            self.is_running = False

    root = tk.Tk()
    TT=TimerTest(root)
    root.mainloop()

test()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM