Nick Bonne Nick Bonne - 1 year ago 97
Python Question

Need help ignoring button press. Tkinter

I am working on making a stopwatch program. I can't get the start button to do nothing if the timer is already running.

When I search, I see the same 14 year old code. I'd find it hard to believe that all these individuals in the past 14 years have independently arrived at the same solution.

As a beginner, I'd really like to know what I'm doing wrong with what I've written instead of copy/pasting and moving on.

from tkinter import *
import time
import datetime

class Window(Frame):

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

self.master = master


def init_window(self):


self.pack(fill=BOTH, expand=1)

quit_button = Button(self, text = 'Quit', command = self.client_exit)
quit_button.config(width = 9)

start_button = Button(self, text = 'Start', command = self.timer_start)
start_button.config(width= 10), y=0)

stop_button = Button(self, text = 'Stop', command = self.timer_stop)
stop_button.config(width = 10)

reset_button = Button(self, text = 'Reset', command = self.timer_reset)
reset_button.config(width = 10)

self.is_timer_running = False

def client_exit(self):

def timer_start(self):
global sec1
sec1 = time.time()
if self.is_timer_running == False:
self.is_timer_running = True
def tick():
if self.is_timer_running == True:
sec = time.time()
sec = datetime.timedelta(seconds = sec - sec1)
clock['text'] = sec
clock.after(100, tick)

def timer_stop(self):
stop_time = time.time()
if self.is_timer_running == True:
self.is_timer_running = False
def tick_stop():
stop = datetime.timedelta(seconds = stop_time - sec1)
clock['text'] = stop

def timer_reset(self):
self.is_timer_running = False
clock['text'] = '00:00:00'

Answer Source

Set the state of the button to disabled immediately after it's clicked (make sure to update it), and then set it back to normal when the timer stops running.

start_button.config(state = 'disabled')

# do whatever you need to do i.e run the stop watch

start_button.config(state = 'normal') 

And thanks to @NickBonne for further clarification :)

You need to add self.start_button = start_button and self.stop_button = stop_button in init_window(). Then you can use self.start_button.config(state="disabled")

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download