
timer button, timer runs when program starts:
from tkinter import *
x = 0
def run_counter(lab):
def counting():
global x
x += 1
lab.config( text = str(x) )
lab.after(1000, counting)
counting()
window = Tk()
window.title('Demo 02')
window.geometry("500x400+100+50")
lab0 = Label(window, width = 15, height=3,
bg="#ccddee ",
text="cursor:\\nbottom_left_corner",
justify="center",
font="Arial 20 bold",
cursor='bottom_left_corner')
lab0.pack()
run_counter(lab0)
btnExit = Button(
window, width=15, height=2,
text="exit",
foreground="#ff2233",
font="Arial 20 bold",
command=window.destroy
)
btnExit.pack(pady=10)
window.mainloop()
Here's a modified version of the program that includes a "Start/Stop" button that toggles the timer on and off:
from tkinter import *
x = 0
timer_running = False
def start_timer():
global timer_running
timer_running = True
run_counter()
def stop_timer():
global timer_running
timer_running = False
def run_counter():
global x
if timer_running:
x += 1
lab.config( text = str(x) )
lab.after(1000, run_counter)
window = Tk()
window.title('Demo 02')
window.geometry("500x400+100+50")
lab0 = Label(window, width = 15, height=3,
bg="#ccddee",
text="cursor:\\nbottom_left_corner",
justify="center",
font="Arial 20 bold",
cursor='bottom_left_corner')
lab0.pack()
lab = Label(window, width = 15, height=3,
bg="#ccddee",
text="0",
justify="center",
font="Arial 20 bold")
lab.pack()
btnStartStop = Button(
window, width=15, height=2,
text="Start",
foreground="#33aa33",
font="Arial 20 bold",
command=lambda: start_timer() if not timer_running else stop_timer()
)
btnStartStop.pack(pady=10)
btnExit = Button(
window, width=15, height=2,
text="Exit",
foreground="#ff2233",
font="Arial 20 bold",
command=window.destroy
)
btnExit.pack()
window.mainloop()
modify the program to change the text of the "Start/Stop" button to "Stop" when the timer is running. Here's the modified code:
from tkinter import *
x = 0
timer_running = False
def start_timer():
global timer_running
timer_running = True
run_counter()
btnStartStop.config(text="Stop", foreground="#aa3333", command=stop_timer)
def stop_timer():
global timer_running
timer_running = False
btnStartStop.config(text="Start", foreground="#33aa33", command=start_timer)
def run_counter():
global x
if timer_running:
x += 1
lab.config( text = str(x) )
lab.after(1000, run_counter)
window = Tk()
window.title('Demo 02')
window.geometry("500x400+100+50")
lab0 = Label(window, width = 15, height=3,
bg="#ccddee",
text="cursor:\\nbottom_left_corner",
justify="center",
font="Arial 20 bold",
cursor='bottom_left_corner')
lab0.pack()
lab = Label(window, width = 15, height=3,
bg="#ccddee",
text="0",
justify="center",
font="Arial 20 bold")
lab.pack()
btnStartStop = Button(
window, width=15, height=2,
text="Start",
foreground="#33aa33",
font="Arial 20 bold",
command=start_timer
)
btnStartStop.pack(pady=10)
btnExit = Button(
window, width=15, height=2,
text="Exit",
foreground="#ff2233",
font="Arial 20 bold",
command=window.destroy
)
btnExit.pack()
window.mainloop()