Tkinter ignores root.after delay - python

Tkinter ignores root.after
The countdown animation plays but it ignores the delay and does everything that's after root.after before the countdown is over
All tests suggest that the countdown is happening and the variable is changing, it just skips the delay
def StartTheSpam():
global TimeLable, ErrorLabel
#destoryes error labels and the countdown timer
try:
for child in root.winfo_children():
if child == TimeLable or child == ErrorLabel:
child.destroy()
except NameError:
pass
#needed for fail-safe 1
mouse = MouseController()
countdown_value = 5
counter = tk.StringVar()
counter.set(countdown_value)
TimeLable = tk.Label(frame, textvariable=counter, padx=10, pady=5, bg=Modes[mode.get()][3]['background'], fg=Modes[mode.get()][2]['text'])
TimeLable.pack()
#coundown
for countdown in range(1, countdown_value):
root.after(1000 * countdown, counter.set, countdown_value - countdown)
x = 100
y = 100
try:
with open(PreferencesStorage['FilePath'], 'r') as SpamText:
while PreferencesStorage['loop']:
for word in SpamText:
#fail safe 1
if x < 21 and y < 21:
break
TempX, TempY = mouse.position
x = int(TempX)
y = int(TempY)
#fail-safe 2
if keyboard.is_pressed('ctrl+d'):
break
keyboard.write(word)
print(word)
#return to begining when at end, thats why its outside of the loop
SpamText.seek(0)
for word in SpamText:
keyboard.write(word)
except FileNotFoundError:
NoFile = tk.Label(frame, text = 'Please Select A File', padx=10, pady=5, fg=Modes[mode.get()][2]['text'], bg=Modes[mode.get()][3]['background'])
NoFile.pack()

root.after does not cause a delay. What that says is "please have the main loop call this function later". Your code is going to queue up 5 timer callback requests very quickly, and then continue on. The callbacks will happen later.
When you're coding for a GUI, you have to start thinking about event-driven programming. When you create widgets or call "pack", NOTHING HAPPENS. All that does is queue up a message to be handled later. At some future point, when the mainloop is able to execute, your messages will be popped off and processed. Only then will your visible screen change. That requires a change in the way you program things. You set up your screen, start the mainloop, and then wait for events. Your event callbacks change the state and return, and Tk in the background will update the screen.

Related

tkinter root.after to run until condition is met, freezes window nav bar until the condition is met. Why?

So I have tried extensively to figure out the best way to run my code. The best suggestions have been to run a root.after recursively until a condition is met. This works but it freezes the window until the condition is met. I cannot figure out what is wrong or how to fix this.
I would like to display the tkinter dialog window, check every 1000 ms if a condition has been met, once met allow the "NEXT" button to become clickable. This all works except if the condition is never met, there is no way to exit the program because the nav bar is stuck in "not responding". I really need this nav bar to not be messed up. I prefer it over a close button. Here is the code
def checkForPortConnection(root, initial_ports):
new_ports = listSerialPorts()
root.after(1000)
if initial_ports == new_ports:
checkForPortConnection(root, initial_ports)
else:
return
def welcomeGui():
root = tk.Tk()
root.title('Setup Wizard')
canvas1 = tk.Canvas(root, relief = 'flat')
welcome_text='Welcome to the setup wizard for your device'
text2 = 'Please plug your device into a working USB port'
text3 = 'If you have already plugged it in, please unplug it and restart the wizard. \n Do not plug it in until the wizard starts. \n The "NEXT" button will be clickable once the port is detected'
label1 = tk.Label(root, text=welcome_text, font=('helvetica', 18), bg='dark green', fg='light green').pack()
label2 = tk.Label(root, text=text2, font=('times', 14), fg='red').pack()
label3 = tk.Label(root, text=text3, font=('times', 12)).pack()
nextButton = ttk.Button(root, text="NEXT", state='disabled')
nextButton.pack()
initial_ports = listSerialPorts()
root.update()
checkForPortConnection(root, initial_ports)
new_ports = listSerialPorts()
correct_port = [x for x in initial_ports + new_ports if x not in initial_ports or x not in new_ports]
print(correct_port)
nextButton.state(["!disabled"])
root.mainloop()
root.after(1000) is effectively the same as time.sleep(1) - it freezes the UI until the time has expired. It doesn't allow the event loop to process events.
If you want to call checkForPortConnection every second, this is the proper way to do it:
def checkForPortConnection(root, initial_ports):
new_ports = listSerialPorts()
if initial_ports == new_ports:
root.after(1000, checkForPortConnection, root, initial_ports)
That will call checkForPortConnection one second in the future (more or less), passing root and initial_ports as arguments. Each time it runs, it will schedule itself to be run again in the future until the condition is no longer met.
Until the time period has expired, mainloop is able to continue to process events as normal.

call a function inside a function in tkinter

when calling rest function from button, then start function is called and prints values continues every second But when I call again rest function the start function call again but this time start function print values in 2x speed and so on.
But I don't want to print value in 2x speed. I am making a small project where I face this type of problem so that is why I write this small code. Please solve my problem
import tkinter as tk
window = tk.Tk()
window.geometry('400x400')
i = 0
def start():
global i
text_label .config(text=i)
i += 1
text_label .after(1000, start)
def rest():
global i
i=0
start()
text_label = tk.Label(window, text="start")
text_label .pack()
tk.Button(window, text="rest", command=rest).pack()
window.mainloop()
What is happening is that every time you call reset, a new callback is launched that will call start indefinitely every 100ms. Every callback being independent, and having no knowledge of the others, this results in a series of callbacks each calling start on their own time, every 100 ms.
To avoid this "snowballing", you need to cancel the previous callbacks in order to reset properly. You do this by keeping a reference on the callback, and calling tk.after_cancel(callback_id) in reset.
Like this:
import tkinter as tk
def start():
global i, callback_id
text_label.config(text=i)
i += 1
callback_id = text_label.after(1000, start)
def reset():
global i, callback_id
i = 0
if callback_id is not None:
text_label.after_cancel(callback_id)
callback_id = None
start()
window = tk.Tk()
window.geometry('400x400')
text_label = tk.Label(window, text="start")
text_label.pack()
callback_id, i = None, 0
tk.Button(window, text="reset", command=reset).pack()
window.mainloop()

Tkinter SimpleDialog Timeout

I have a program that requires input to a module while it is running, so I am implementing a simple Dialog box to get input from the user to use in my Tkinter program. But I also need it to timeout when running the module as a console program, to pass over it or timeout after so many seconds when the user is not interacting with it. And not to have it just sit there and wait forever until the user interacts with it. How do I terminate the window after a timeout? Here is what I have now ...
def loginTimeout(timeout=300):
root = tkinter.Tk()
root.withdraw()
start_time = time.time()
input1 = simpledialog.askinteger('Sample','Enter a Number')
while True:
if msvcrt.kbhit():
chr = msvcrt.getche()
if ord(chr) == 13: # enter_key
break
elif ord(chr) >= 32: #space_char
input1 += chr
if len(input1) == 0 and (time.time() - start_time) > timeout:
break
print('') # needed to move to next line
if len(input1) > 0:
return input1
else:
return input1
Not entirely sure what the question is, but you could use the tkinter after() method in a similar way to this:
import tkinter as tk
root = tk.Tk()
root.geometry('100x100')
def get_entry() -> str:
"""Gets and returns the entry input, and closes the tkinter window."""
entry = entry_var.get()
root.destroy() # Edit: Changed from root.quit()
return entry
# Your input box
entry_var = tk.StringVar()
tk.Entry(root, textvariable=entry_var).pack()
# A button, or could be an event binding that triggers get_entry()
tk.Button(root, text='Enter/Confirm', command=get_entry).pack()
# This would be the 'timeout'
root.after(5000, get_entry)
root.mainloop()
So, if the User enters something they can hit confirm or launch an event binded to the entry, or after the delay the program runs get_entry anyway. Maybe this will give you an idea, you can also check out the other widget methods:
https://effbot.org/tkinterbook/widget.htm
EDIT: I'm not sure how this is arranged in your program, but root.mainloop() blocks, so once it's running, code after it won't run until the mainloop() exits. If you're function is part of a toplevel() window, you could use wait_window() to prevent the function from advancing until your timeout destroys the toplevel window or the function is called before by the User. The example, although probably not the best solution, would be:
def loginTimeout(timeout_ms: int=5000):
root = tk.Tk()
root.geometry('200x50')
# function to get input and change the function variable
input_ = ''
def get_input():
nonlocal input_
input_ = entry_var.get()
root.destroy()
# build the widgets
entry_var = tk.StringVar()
tk.Entry(root, textvariable=entry_var).pack(side='left')
tk.Button(root, text='Confirm', command=get_input).pack(side='right')
# timeout
root.after(timeout_ms, get_input)
# mainloop
root.mainloop()
# won't get here until root is destroyed
return input_
print(loginTimeout())

How to use after()? [duplicate]

Hey I am new to python and am using tkinter for my gui. I am having trouble using the "after" method.
The goal is to make a random letter appear every 5 seconds.
Here is my code:
import random
import time
from tkinter import *
root = Tk()
w = Label(root, text="GAME")
w.pack()
frame = Frame(root, width=300, height=300)
frame.pack()
L1 = Label(root, text="User Name")
L1.pack(side=LEFT)
E1 = Entry(root, bd =5)
E1.pack(side=LEFT)
tiles_letter = ['a', 'b', 'c', 'd', 'e']
while len(tiles_letter) > 0:
rand = random.choice(tiles_letter)
tile_frame = Label(frame, text=rand)
tile_frame.pack()
frame.after(500)
tiles_letter.remove(rand) # remove that tile from list of tiles
root.mainloop()
can someone please help me --- the problem is definitely frame.after(500):
i'm not sure if it is correct to use "frame" and I don't know what which argument follows the 500.
Thanks
You need to give a function to be called after the time delay as the second argument to after:
after(delay_ms, callback=None, *args)
Registers an alarm callback that is called after a given time.
So what you really want to do is this:
tiles_letter = ['a', 'b', 'c', 'd', 'e']
def add_letter():
rand = random.choice(tiles_letter)
tile_frame = Label(frame, text=rand)
tile_frame.pack()
root.after(500, add_letter)
tiles_letter.remove(rand) # remove that tile from list of tiles
root.after(0, add_letter) # add_letter will run as soon as the mainloop starts.
root.mainloop()
You also need to schedule the function to be called again by repeating the call to after inside the callback function, since after only executes the given function once. This is also noted in the documentation:
The callback is only called once for each call to this method. To keep
calling the callback, you need to reregister the callback inside
itself
Note that your example will throw an exception as soon as you've exhausted all the entries in tiles_letter, so you need to change your logic to handle that case whichever way you want. The simplest thing would be to add a check at the beginning of add_letter to make sure the list isn't empty, and just return if it is:
def add_letter():
if not tiles_letter:
return
rand = random.choice(tiles_letter)
tile_frame = Label(frame, text=rand)
tile_frame.pack()
root.after(500, add_letter)
tiles_letter.remove(rand) # remove that tile from list of tiles
Live-Demo: repl.it
I believe, the 500ms run in the background, while the rest of the code continues to execute and empties the list.
Then after 500ms nothing happens, as no function-call is implemented in the after-callup (same as frame.after(500, function=None))
after is used to delay execution of the program or to execute a command in background sometime in the future. But you can build a loop inside the mainloop by calling itself.
import tkinter as tk #import tkinter
import datetime #import datetime for our clock
def tick(): #function to update the clock
showed_time = clock['text'] #current showed time
current_time = datetime.datetime.now().strftime("%H:%M:%S") #real time
if showed_time != current_time: #if the showed time is not the real time
clock.configure(text=current_time) #update the label with the current time
clock.after(1000, tick) #call yourself in 1000ms (1sec.) again to update the clock
return None
root=tk.Tk()
clock = tk.Label(root)
clock.pack()
tick()
root.mainloop()
In the above script we had built a digital clock and get in touch with the after method. The after method is nothing but an interval and on the end of that interval we want that something happen.
To learn more about this basic widget method [click]
after(delay_ms, callback=None, args)
This method registers a callback function that will be called after a
given number of milliseconds. Tkinter only guarantees that the
callback will not be called earlier than that; if the system is busy,
the actual delay may be much longer.
import tkinter as tk
import datetime
def tick():
showed_time = clock['text']
current_time = datetime.datetime.now().strftime("%H:%M:%S")
if showed_time != current_time:
clock.configure(text=current_time)
global alarm #make sure the alarm is reachable
alarm = clock.after(1000, tick)#assign the alarm to a variable
return None
def stop():
stop.after_cancel(alarm) #cancel alarm
root=tk.Tk()
clock = tk.Label(root)
clock.pack()
stop = tk.Button(root, text='Stop it!', command=stop)
stop.pack()
tick()
root.mainloop()
Here we have the same code but with the ability to cancel our loop with the after_cancel method of tkinter. You dont need to global the alarm inside a class. self.alarm = self.clock.after(...) works fine.
after_cancel(id)
Cancels an alarm callback.
id
Alarm identifier.

Python GUI buttons wont execute

I'm writing a basic war-driving program. I have gotten it to loop the command to pull all the wireless access points near by. The problem is my stop button doesn't work and I am unable to update the label(I'm not even sure if I can update the label).
import sys, os, subprocess, re
from Tkinter import *
missionGO = 0
count = 0
class App:
def __init__(self, master):
frame = Frame(master)
frame.pack()
self.start = Button(frame, text="Start", fg="green",
command=self.startButtonClick)
self.start.grid(row=3)
self.stop = Button(frame, text="Stop", fg="red",
command=self.stopButtonClick)
self.stop.grid(row=3, column=1)
self.totalSSIDLabel = Label(frame, text="Current Access Points: ")
self.totalSSIDLabel.grid(row=0)
self.totalSSID = Label(frame, text=count)
self.totalSSID.grid(row=0, column=1)
def startButtonClick(self):
missionGO = 1
while (missionGO == 1):
wlan = getAccessPoints()
x = numberOfAccessPoints(wlan)
print x
return
def stopButtonClick(self):
missionGO = 0
return
def stop(event):
missionGO = 0
# Finds all wireless AP
def getAccessPoints():
X = subprocess.check_output("netsh wlan show network mode=Bssid",
shell=True)
return X
def numberOfAccessPoints(file):
count = 0
words = file.split()
for line in words:
if re.match('SSID', line):
count = count + 1
return count
#Main
root = Tk()
app = App(root)
root.mainloop()
Tkinter is single threaded. That means that while you are in the while loop inside startButtonClick, no other events are processed. The stop button won't call its command until the startButtonClick function finishes
You need to remember that your program is already running a global infinite loop: the event loop. There's no reason to put another infinite loop inside it. When you want something to run forever, the trick is to put one iteration on the event loop, then when it runs it puts another iteration on the event loop.
The other key to this is to make sure that one iteration of the loop is fast -- it needs to be well under a second (more like under 100ms) or the UI will become laggy.
The logic looks something like this:
def startButtonClick(self):
self.missionGO = 1
self._do_one_iteration()
def _do_one_iteration(self):
if self.missionGO == 1:
wlan = getAccessPoints()
x = numberOfAccessPoints(wlan)
print x
# this adds another iteration to the event loop
self.after(10, self._do_one_iteration)
def stopButtonClick(self):
self.missionGO = 0
I think the main thread is hanging in the while loop of the start button click. Since it's busy it won't even notice the stop button has been pressed.
I can't tell you exactly why your stop button doesn't work, but I think I got the idea of your programm. My suggestion is to establish two threads. The first thread for the UI, and the second for constantly checking wireless networks with given interval (your current code checks ASAP - bad practice, you should pause within the loop.
Since I have not dealt with multithreading in Tkinter, I can only provide you with entry points:
threading Module
time.sleep for updating the nearby networks every second or similar
Is there a way to request a function to be called on Tkinter mainloop from a thread which is not the mainloop?
Tkinter: invoke event in main loop
Good luck!

Categories

Resources