Python Forum
Stop Watch star/stop problem
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Stop Watch star/stop problem
#1
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()
Reply
#2
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [PyQt] start and stop websocket-client using button GUI zinho 5 2,372 Dec-27-2022, 06:06 PM
Last Post: deanhystad
  Start stop button stack stergeol 5 3,778 Feb-04-2021, 07:57 PM
Last Post: stergeol
  Stop import from executing function, but allow the function to execute from tk btn. MrBitPythoner 4 2,624 Dec-08-2020, 10:00 PM
Last Post: MrBitPythoner
  Progressbar with start and stop jelo34 3 4,874 Nov-30-2020, 03:36 AM
Last Post: deanhystad
  [Tkinter] Noob question:Using pyttsx3 with tkinter causes program to stop and close, any ideas? Osman_P 4 5,232 Nov-14-2020, 10:51 AM
Last Post: Osman_P
  How to stop time counter in Tkinter LoneStar 1 4,291 Sep-11-2020, 08:56 PM
Last Post: Yoriz
  [Tkinter] How do I stop window changing size? microphone_head 1 2,306 May-02-2019, 11:34 AM
Last Post: microphone_head
  Stop Timer in Different Class ian 0 2,271 Aug-06-2017, 02:37 AM
Last Post: ian
  How to stop a tkinter function, without closing the window? keakins 5 12,948 Jun-29-2017, 11:53 AM
Last Post: keakins

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020