Guys, I need help with the implementation of the countdown timer, implemented exactly on python + tkinter. Functionality: 1 start button - start counting; 2- restart-button to reset the timer to its original position (2 min. 00 s); 3 (or 3 and 4) - data output on the screen. Starting position -2 minutes. Today, for the first time I ran into Tkinter and was already dizzy. That's what sketched

from tkinter import * import time class Application(Frame): def __init__(self, master= None): Frame.__init__(self,master) self.pack() self.createWidgets() def createWidgets(self): self.minutes = IntVar() self.l_min = Label(self,font=('Helvetica',24)) self.l_min.pack(side='top') self.l_min['textvariable'] = self.minutes self.seconds = IntVar() self.l_sec = Label(self, font=('Helvetica', 24)) self.l_sec.pack(side='top') self.l_sec['textvariable'] = self.seconds self.b_start = Button(self, text ='START',fg='red') self.b_start.bind('<Button-1>', self.start_timer) self.b_start.pack(side= 'bottom') self.b_restart = Button(self, text ="RESTART", fg='blue') # self.b_restart.bind('<Button-1>',reset_timer) self.b_restart.pack(side= 'bottom') def start_timer(self,mins): for mins in range(1,-1,-1): self.minutes.set(mins) for secs in range(59,-1,-1): self.seconds.set(secs) time.sleep(1) def reset_timer(self): pass root= Tk() app = Application(master=root) root.mainloop() 

I would be grateful for the help or hint. ZY Similar topics are many, but only under javascript or other PL.

Closed due to the fact that off-topic participants jfs , Denis Bubnov , user194374, aleksandr barakin , ߊߚߤߘ 5 Feb '17 at 9:23 .

It seems that this question does not correspond to the subject of the site. Those who voted to close it indicated the following reason:

  • “Questions asking for help with debugging (“ why does this code not work? ”) Should include the desired behavior, a specific problem or error, and a minimum code for playing it right in the question . Questions without an explicit description of the problem are useless for other visitors. See How to create minimal, self-sufficient and reproducible example . " - jfs, Denis Bubnov, Community Spirit, aleksandr barakin, ߊߚߤߘ
If the question can be reformulated according to the rules set out in the certificate , edit it .

  • one
    But the problem is what? Any exceptions? - m9_psy

2 answers 2

 from tkinter import Tk, Label, Button timer_running = False # запущен ли таймер default_seconds = 120 # изначальное положение(2 мин 00 сек) timer_seconds = default_seconds # текущее положение таймера, сек def timer_start_pause(): global timer_running timer_running = not timer_running # работа или пауза if timer_running: # работа timer_tick() def timer_reset(): global timer_running, timer_seconds timer_running = False # стоп timer_seconds = default_seconds # изначальное положение show_timer() def timer_tick(): if timer_running and timer_seconds: label.after(1000, timer_tick) # перезапустить через 1 сек # уменьшить таймер global timer_seconds timer_seconds -= 1 show_timer() def show_timer(): '''отобразить таймер''' m = timer_seconds//60 s = timer_seconds-m*60 label['text'] = '%02d:%02d' % (m, s) if __name__ == '__main__': root = Tk() label = Label(root) label.pack() Button(root, text='start/pause', command=timer_start_pause).pack() # запуск/пауза отсчета Button(root, text='reset', command=timer_reset).pack() # сброс timer_reset() root.mainloop() 
     import tkinter as tk class CountdownTimer(tk.Tk): def __init__(self): tk.Tk.__init__(self) self.label_mins = tk.Label(self, text="2", bg ="white") self.label_mins.grid(row=1,column=2 ) self.label_secs = tk.Label(self, text="0", bg ="white") self.label_secs.grid(row=1, column = 3) self.rem_seconds = 59 self.rem_minutes = 1 self.stop_marker = False self.b_start=tk.Button(self, text="START",command = self.start_countdown) self.b_start.grid(row = 2, column= 2, sticky = "S") self.b_reset = tk.Button(self, text="RESET", command = self.reset_countdown) self.b_reset.grid(row = 2, column =3, sticky = "S") self.b_stop = tk.Button(self, text="STOP", command = self.stop_countdown) self.b_stop.grid(row=2, column = 4, sticky = "S") def start_countdown(self): if self.stop_marker!=True: if self.rem_seconds==0 and self.rem_minutes==0: self.label_mins.configure(text="Time") self.label_secs.configure(text= "is up !!!") elif self.rem_seconds<0: self.rem_minutes-=1 self.rem_seconds =59 self.start_countdown() else: self.label_mins.configure(text = "%d"%self.rem_minutes) self.label_secs.configure(text = "%d"%self.rem_seconds) self.rem_seconds -= 1 self.after(1000,self.start_countdown) def stop_countdown(self): self.stop_marker = True def reset_countdown(self): self.label_mins.configure(text = "2") self.label_secs.configure(text = "0") self.rem_minutes = 1 self.rem_seconds = 59 self.stop_marker = False if __name__ == "__main__": app = CountdownTimer() app.mainloop() 

    That's what I did, really with my bikes. And the problem was that I used time.sleep ().