hi guys.. ive tried to integrate a timer code (which works fine) into a tkinter window but im having errors. can u plz fix it 4 me? thanks.
this is the code i want to integrate.
import Tkinter as tk
import time
def start():
global count_flag
count_flag = True
count = 0.0
while True:
if count_flag == False:
break
# put the count value into the label
label['text'] = str(count)
# wait for 0.1 seconds
time.sleep(0.1)
# needed with time.sleep()
root.update()
# increase count
count += 0.1
def stop():
global count_flag
count_flag = False
# create a Tkinter window
root = tk.Tk()
# this will be a global flag
count_flag = True
# create needed widgets
label = tk.Label(root, text='0.0')
btn_start = tk.Button(root, text='start', command=start)
btn_stop = tk.Button(root, text='stop', command=stop)
# use a grid to place the widgets
label.grid(row=0, column=0, columnspan=2)
btn_start.grid(row=1, column=0, padx=5, pady=5)
btn_stop.grid(row=1, column=1, padx=5)
# start the event loop
root.mainloop()
i want to integrate the above code in this tkinter a window but its not working.
import Tkinter as tk
import time
import sys,Queue
import datetime as dt
from Tkinter import *
class stopwatch(Label):
def __init__(self, parent, *args, **kwargs):
Label.__init__(self, parent, *args, **kwargs)
btn_start = Button(root, text='start', command=self.start)
btn_stop = Button(root, text='stop', command=self.stop)
canvas.grid(row=0, column=0, columnspan=2)
btn_start.grid(row=1, column=0, padx=5, pady=5)
btn_stop.grid(row=1, column=1, padx=5)
def start(self):
global count_flag
count_flag = True
count = 0.0
while True:
if count_flag == False:
break
# put the count value into the label
label['text'] = str(count)
# wait for 0.1 seconds
time.sleep(0.1)
# needed with time.sleep()
root.update()
# increase count
count += 0.1
def stop(self):
global count_flag
count_flag = False
# create a Tkinter window
root=tk.Tk()
canvas = tk.Canvas(root, width=1000, height=300, bg="#FFFFFF")
stopwatch=stopwatch(root, text='0.0')
stopwatch.pack(fill=BOTH, expand=1)
canvas.pack()
# this will be a global flag
count_flag = True
# create needed widgets
#label = tk.Label(root, text='0.0')
# use a grid to place the widgets
#label.grid(row=0, column=0, columnspan=2)
# start the event loop
root.mainloop()