Python Forum
Stop Watch star/stop problem - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: GUI (https://python-forum.io/forum-10.html)
+--- Thread: Stop Watch star/stop problem (/thread-19069.html)



Stop Watch star/stop problem - macellan85 - Jun-12-2019

Hello

I' m having problem with my stop watch code on a GUI window. Well the problem one is I have to press start button two times and If I continue to press start button then it behaves like several stop watch are working together. Second problem is the stop button doesn' t make any sense. As you can see I wan to control the start/stop process by using a variable "running". Start button changes it to "True", but stop button doesn' t turn it to "False"??

Hope you can give some idea.

Best wishes...


#=======================#
# imports		#
#=======================#
import tkinter as tk
from tkinter import *
from tkinter import ttk
from threading import Timer
import time
from time import  sleep
import os
import tkinter.font as tkFont
from tkinter import scrolledtext
from tkinter import Menu
from threading import Thread
from queue import Queue


#=====================================================
class HMCC_GUI():
	def __init__(self):         # Initializer method
		
		# Create instance
		self.root = tk.Tk()
		
		# Add a title
		self.root.title("Python GUI")
		self.create_widgets()
		self.running=None
		
	def ElapsedTimeThread(self):
		while self.running==True:
			self.timer[2] += 1
			
			if (self.timer[2] >= 60):
					self.timer[2] = 0
					self.timer[1] += 1
					
			if (self.timer[1] >= 60):
					self.timer[0] += 1
					self.timer[1] = 0
					
			timeString=self.pattern.format(self.timer[0], self.timer[1], self.timer[2])
			self.timeText.configure(text=timeString)
			
			time.sleep(1)
			
	def Create_ElapsedTimeThread(self):
		self.run_thread = Thread(target=self.ElapsedTimeThread)
		self.run_thread.setDaemon(True)
		self.run_thread.start()
		print(self.running)
		
	def start(self):
		print("started")
		self.Create_ElapsedTimeThread()
		self.running=True
		print(self.running)
		
	def stop(self):
		print("stopped")
		self.runing=False
		print(self.running)
		
	def reset(self):
		print("resetted")
		
	def quit_(self):
		self.root.quit()
		self.root.destroy()
		
	def create_widgets(self):
		
		tabControl = ttk.Notebook(self.root)
		tab1 = ttk.Frame(tabControl)
		tabControl.add(tab1, text='CONTROL')
		tabControl.pack(expand=1, fill="both")
		
		buttons_frame=ttk.Frame(tab1, relief="raised")
		buttons_frame.place(x=10, y=50, height=200, width=140)
		
		button1 = tk.Button(tab1, text='START', fg='black', font='none 15 bold', command=self.start)
		button2 = tk.Button(tab1, text='STOP' , fg='black', font='none 15 bold', command=self.stop)
		button1.place(x= 80 , y= 100, anchor='c')
		button2.place(x= 80 , y= 200, anchor='c')
		
		reset_frame=ttk.Frame(tab1, relief="raised")
		reset_frame.place(x=6, y=320, height=80, width=150)
		button3 = tk.Button(tab1, text='RESET', fg='black', font='none 15 bold', command=self.reset)
		button3.place(x= 80 , y= 360, anchor='c')
		
		Elapsed_Time_Info = ttk.Label(tab1, text = 'Elapsed Time: ')
		Elapsed_Time_Info.place(x=400, y= 460, anchor='c')
		Elapsed_Time_Info.config(font='none 15 bold')
		
		self.timer 	 = [0, 0, 0]
		self.pattern  = '{0:02d}:{1:02d}:{2:02d}'
		self.timeText = ttk.Label(tab1, text='00:00:00', font=('none 15'))
		self.timeText.place(x=550, y=460, anchor='c')
		
		
#======================
# Start GUI
#======================
gui = HMCC_GUI()
gui.root.geometry('750x500')
gui.root.mainloop()



RE: Stop Watch star/stop problem - woooee - Jun-12-2019

See the clock code at https://pythonbasics.org/tkinter-label/ to see how to do this. You should be able to add an if self.running: to know if to update, and then be good to go.