'How do I make two seperate stopwatches controlled by two different stop and start buttons in tkinter gui. My computer wont let me
I am trying to make a timesheet where every employee times in in the morning and times out at the end of the day. It is going well so far but, I the Why is the the Jimena stopwatch button controls are controlling the gagan stopwatch? I want the start and stop of each name to work seperetley (2, individual stopwatches)? Can you assist me?
import tkinter as tk
from tkinter import ttk
from tkinter import *
# JIMENA
class JimenaStopwatch():
def __init__(self):
self.counter = 0
self.running = None
def start(self):
self.running = True
print("CLICKED")
self.run()
def run(self):
if self.running == True:
start["state"] = tk.DISABLED
self.counter += 1
timer["text"] = self.counter
timer.after(1000, self.run)
def stop(self):
start["state"] = tk.NORMAL
self.running = False
self.run()
root = tk.Tk()
root.geometry("750x500")
root.resizable(height=False, width=False)
stopwatch = JimenaStopwatch()
timer = ttk.Label(root, text=stopwatch.counter)
start = ttk.Button(root, text="TIME IN", command=stopwatch.start, state=tk.NORMAL)
stop = ttk.Button(root, text="TIME OUT", command=stopwatch.stop)
jimena = Label(root, text="JIMENA", height=2, width=10)
jimena.grid(column=0, row=3)
start.grid(column=1, row=3, padx=10)
stop.grid(column=2, row=3, padx=10)
timer.grid(column=5, row=3, padx=10)
# GAGAN
class GaganStopwatch:
def __init__(self):
self.counter = 0
self.running = None
def start(self):
self.running = True
print("CLICKED")
self.run()
def run(self):
if self.running:
start["state"] = tk.DISABLED
self.counter += 1
timer["text"] = self.counter
timer.after(1000, self.run)
def stop(self):
start["state"] = tk.NORMAL
self.running = False
self.run()
gagan = Label(root, text="GAGAN", height=2, width=10)
gagan.grid(column=0, row=4)
stopwatch = GaganStopwatch()
timer = ttk.Label(root, text=stopwatch.counter)
start = ttk.Button(root, text="TIME IN", command=stopwatch.start, state=tk.NORMAL)
stop = ttk.Button(root, text="TIME OUT", command=stopwatch.stop)
start.grid(column=1, row=4, padx=10)
stop.grid(column=2, row=4, padx=10)
timer.grid(column=5, row=4, padx=10)
root.mainloop()
Solution 1:[1]
Your basic issue is that you have used the same (global) variables for both the stop watches. Use different variable names for the class GaganStopwatch:
class GaganStopwatch:
def __init__(self):
self.counter = 0
self.running = None
def start(self):
self.running = True
print("CLICKED")
self.run()
def run(self):
if self.running:
start_1["state"] = tk.DISABLED
self.counter += 1
timer_1["text"] = self.counter
timer_1.after(1000, self.run)
def stop(self):
start_1["state"] = tk.NORMAL
self.running = False
self.run()
gagan = Label(root, text="GAGAN", height=2, width=10)
gagan.grid(column=0, row=4)
stopwatch_g = GaganStopwatch()
timer_1 = ttk.Label(root, text=stopwatch_g.counter)
start_1 = ttk.Button(root, text="TIME IN", command=stopwatch_g.start, state=tk.NORMAL)
stop_1 = ttk.Button(root, text="TIME OUT", command=stopwatch_g.stop)
start_1.grid(column=1, row=4, padx=10)
stop_1.grid(column=2, row=4, padx=10)
timer_1.grid(column=5, row=4, padx=10)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | FrainBr33z3 |