DPaul wrote: ↑Thu Nov 16, 2017 6:39 am
The if statement inside the loop, is what i am trying to avoid, because i need many,
for different situations.
I seem to observe that inside a loop, response to button-presses is not so snappy.
The "break" idea is something that i am going to follow-up on,
because i can get the different events to produce the same trigger
that stops the while loop.
Let's try that.
thx,
Paul
looks to me like you need threading
heres an example of threading using a stopwatch in tkinter, you can run this from windows etc.
Code: Select all
from tkinter import *
import threading
import time
master = Tk()
timer = 0
pause_time = 0
paused = 0
start_time = 0
started = 0
pause_began = 0
canvas = Canvas(width=1280, height=720, bg='black')
canvas.grid(rowspan=5, columnspan=8, sticky='W,E,N,S')
def background_math():
global timer
while 1:
if started == 1 and paused == 0:
now = time.time()
timer = (now - start_time) - pause_time
time_label.config(text=timer)
master.update_idletasks()
time.sleep(0.1)
def start_timer():
global start_time, timer, pause_time, started, paused, pause_began
timethisdef = time.time()
if started == 0:
start_time = time.time()
start_button.config(text="Pause")
started = 1
else:
if started == 1 and paused == 0:
pause_began = time.time()
start_button.config(text="Resume")
paused = 1
else:
if started == 1 and paused == 1:
pause_end = time.time()
pause_time += (pause_end - pause_began)
start_button.config(text="Pause")
paused = 0
master.update_idletasks()
deftimefin = time.time()
mathtime = deftimefin - timethisdef
print(mathtime)
def stop_timer():
global start_time, timer, pause_time, started, paused
timer = 0
pause_time = 0
paused = 0
start_time = 0
started = 0
start_button.config(text="Start")
master.update()
start_button = Button(master, text="Start", command=start_timer, width=5, font=('aharoni', 18, 'bold'),
bg='orange', fg='white')
start_button.grid(padx=10, pady=10, row=4, column=0, sticky='W,E,N,S')
stop_button = Button(master, text="Stop", command=stop_timer, width=5, font=('aharoni', 18, 'bold'),
bg='orange', fg='white')
stop_button.grid(padx=10, pady=10, row=4, column=2, sticky='W,E,N,S')
time_label = Label(master, text=timer, width=10, font=('aharoni', 18, 'bold'), bg='black', fg='white')
time_label.grid(padx=5, pady=10, row=3, column=0, columnspan=4, sticky='W,E,N,S')
t = threading.Thread(target=background_math)
t.daemon = True
t.start()
master.mainloop()
the timer is worked out within its own thread, then updated to our tkinter label
within the main thread we have tkinter buttons which when pressed start, stop and pause the timer. to create the pause i put in an additional timer which started with pause and stopped with resume, this then added to itself with each pause/resume and was deducted from the run time until stop is issued then all is reset to 0
as you put "The if statement inside the loop, is what i am trying to avoid" the if statement takes hardly no time as long as it doesnt contain time.sleep() or other time delays.
ive added
Code: Select all
timethisdef = time.time()
deftimefin = time.time()
mathtime = deftimefin - timethisdef
print(mathtime)
to "def start_timer" so you can see how long an if statement will actually take, on my pc it takes 0.0005 - 0.001 seconds to perform the start/pause/resume script, as our clock is done by time stamp the delay will not effect the clock but could delay time stamping by amount said and the "def start_timer" statement would also delay the tkinter window as they are run within the same thread at mainloop() but the delay is minimal 0.001 secs on a pc