How to make code run after a tkinter window pop up
Question:
My code should create a window and then count from 1 to 10 but, it
makes a window and only counts from 1 to 10 when you close the window. how do I make it count when the window pops up, not when it closes?
from tkinter import Tk, mainloop
import time
I = 1
window = Tk()
window.title("Game")
window.configure(width=500, height=300)
window.configure(bg='blue')
window.geometry("+" + str(I * 5) + "+0")
window.mainloop()
while I < 10:
print(I)
print("Hi")
I += 1
time.sleep(0.2)
Answers:
The while
loop in your code only runs after the window.mainloop()
(after it has ended).
This code seems to work:
from tkinter import Tk
I = 1
window = Tk()
window.title("Game")
window.configure(width=500, height=300)
window.configure(bg='blue')
window.geometry("+" + str(I * 5) + "+0")
def loop():
global I
print(I)
print("Hi")
I += 1
window.after(200, loop)
loop()
window.mainloop()
The window.after()
method is used to run some kind of loops even when the mainloop is running.
What mainloop
does is start using the main thread for GUI events and nothing else; that’s why your counting code starts only after closing the window. If you want to execute code while the GUI is running, you can use the after
method:
from tkinter import Tk, mainloop
import time
I = 1
# Wrap the code in a standalone method
def count():
# In order to access a global variable from a method
global I
while I < 10:
print(I)
print("Hi")
I += 1
time.sleep(0.2)
window = Tk()
window.title("Game")
window.configure(width=500, height=300)
window.configure(bg='blue')
window.geometry("+" + str(I * 5) + "+0")
# In order to have the window rendered already
window.update()
# Execute code on the main thread
window.after(0, count)
window.mainloop()
My code should create a window and then count from 1 to 10 but, it
makes a window and only counts from 1 to 10 when you close the window. how do I make it count when the window pops up, not when it closes?
from tkinter import Tk, mainloop
import time
I = 1
window = Tk()
window.title("Game")
window.configure(width=500, height=300)
window.configure(bg='blue')
window.geometry("+" + str(I * 5) + "+0")
window.mainloop()
while I < 10:
print(I)
print("Hi")
I += 1
time.sleep(0.2)
The while
loop in your code only runs after the window.mainloop()
(after it has ended).
This code seems to work:
from tkinter import Tk
I = 1
window = Tk()
window.title("Game")
window.configure(width=500, height=300)
window.configure(bg='blue')
window.geometry("+" + str(I * 5) + "+0")
def loop():
global I
print(I)
print("Hi")
I += 1
window.after(200, loop)
loop()
window.mainloop()
The window.after()
method is used to run some kind of loops even when the mainloop is running.
What mainloop
does is start using the main thread for GUI events and nothing else; that’s why your counting code starts only after closing the window. If you want to execute code while the GUI is running, you can use the after
method:
from tkinter import Tk, mainloop
import time
I = 1
# Wrap the code in a standalone method
def count():
# In order to access a global variable from a method
global I
while I < 10:
print(I)
print("Hi")
I += 1
time.sleep(0.2)
window = Tk()
window.title("Game")
window.configure(width=500, height=300)
window.configure(bg='blue')
window.geometry("+" + str(I * 5) + "+0")
# In order to have the window rendered already
window.update()
# Execute code on the main thread
window.after(0, count)
window.mainloop()