tkinter, creating frames with button and delay
so i am trying to create multiple frames with a button but with delays between them, so i press the button --> create frame1 --> wait 5 second --> create frame2 and so on. But the problem is that when i press the button it takes 5 seconds and displays the frame's at the same time. hopefully someone has any idea how to solve this.
import tkinter as tk
import time
class lingo(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
self.grid_rowconfigure(0,minsize=1080)
self.grid_columnconfigure(0,minsize=1810)
m = tk.Frame(self)
m.grid(row=0, column=0,sticky="nsew")
h1 = tk.Frame(m)
h1.grid(row=0, column=0)
e1 = tk.Entry(h1)
e1.grid(row=0, column=0)
def get_entry():
entry1 = e1.get()
print(entry1)
print(list(entry1))
def test1():
r1l1t = tk.Label(r1l1, text=a1, font=("",40), fg="white")
r1l1t.grid()
r1l1t['bg'] = r1l1['bg']
r1l2t = tk.Label(r1l2, text=a2, font=("",40), fg="white")
r1l2t.grid()
r1l2t['bg'] = r1l2['bg']
if len(entry1) < 19:
print("te kort woord")
if len(entry1) > 19:
print("te lang woord")
if len(entry1) == 19:
a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19 = list(entry1)
test1()
m.grid_rowconfigure(0, minsize=100)
m.grid_rowconfigure(1, minsize=100)
m.grid_columnconfigure(0, minsize=1810)
b1 = tk.Button(h1, bg="blue", text="enter", command=get_entry)
b1.grid(row=0, column=1)
row1 = tk.Frame(m, bg="grey")
row1.grid(row=1, column=0, sticky="nsew")
for c in range(0, 38):
if c % 2 == 0:
row1.grid_columnconfigure(c, minsize=5)
else:
row1.grid_columnconfigure(c, minsize=90)
row1.grid_rowconfigure(0, minsize=5)
row1.grid_rowconfigure(1, minsize=90)
row1.grid_rowconfigure(2, minsize=5)
r1l1 = tk.Frame(row1, bg="blue")
r1l1.grid(row=1, column=1, sticky="nsew")
r1l1.grid_rowconfigure(0, minsize=90)
r1l1.grid_columnconfigure(0, minsize=90)
# r1l1t = tk.Label(r1l1, text=".....", font=("",40), fg="white")
# r1l1t.grid()
# r1l1t['bg'] = r1l1['bg']
r1l2 = tk.Frame(row1, bg="blue")
r1l2.grid(row=1, column=3, sticky="nsew")
r1l2.grid_rowconfigure(0, minsize=90)
r1l2.grid_columnconfigure(0, minsize=90)
# r1l2t = tk.Label(r1l2, text=".....", font=("",40), fg="white")
# r1l2t.grid()
# r1l2t['bg'] = r1l2['bg']
r1l3 = tk.Frame(row1, bg="blue")
r1l3.grid(row=1, column=5, sticky="nsew")
r1l3.grid_rowconfigure(0, minsize=90)
r1l3.grid_columnconfigure(0, minsize=90)
r1l3t = tk.Label(r1l3, text=".....", font=("",40), fg="white")
r1l3t.grid()
r1l3t['bg'] = r1l3['bg']
r1l4 = tk.Frame(row1, bg="blue")
r1l4.grid(row=1, column=7, sticky="nsew")
r1l4.grid_rowconfigure(0, minsize=90)
r1l4.grid_columnconfigure(0, minsize=90)
r1l4t = tk.Label(r1l4, text=".....", font=("",40), fg="white")
r1l4t.grid()
r1l4t['bg'] = r1l4['bg']
r1l5 = tk.Frame(row1, bg="blue")
r1l5.grid(row=1, column=9, sticky="nsew")
r1l5.grid_rowconfigure(0, minsize=90)
r1l5.grid_columnconfigure(0, minsize=90)
r1l5t = tk.Label(r1l5, text=".....", font=("",40), fg="white")
r1l5t.grid()
r1l5t['bg'] = r1l5['bg']
r1l6 = tk.Frame(row1, bg="blue")
r1l6.grid(row=1, column=11, sticky="nsew")
r1l6.grid_rowconfigure(0, minsize=90)
r1l6.grid_columnconfigure(0, minsize=90)
r1l6t = tk.Label(r1l6, text=".....", font=("",40), fg="white")
r1l6t.grid()
r1l6t['bg'] = r1l6['bg']
r1l7 = tk.Frame(row1, bg="blue")
r1l7.grid(row=1, column=13, sticky="nsew")
r1l7.grid_rowconfigure(0, minsize=90)
r1l7.grid_columnconfigure(0, minsize=90)
r1l7t = tk.Label(r1l7, text=".....", font=("",40), fg="white")
r1l7t.grid()
r1l7t['bg'] = r1l7['bg']
r1l8 = tk.Frame(row1, bg="blue")
r1l8.grid(row=1, column=15, sticky="nsew")
r1l8.grid_rowconfigure(0, minsize=90)
r1l8.grid_columnconfigure(0, minsize=90)
r1l8t = tk.Label(r1l8, text=".....", font=("",40), fg="white")
r1l8t.grid()
r1l8t['bg'] = r1l8['bg']
r1l9 = tk.Frame(row1, bg="blue")
r1l9.grid(row=1, column=17, sticky="nsew")
r1l9.grid_rowconfigure(0, minsize=90)
r1l9.grid_columnconfigure(0, minsize=90)
r1l9t = tk.Label(r1l9, text=".....", font=("",40), fg="white")
r1l9t.grid()
r1l9t['bg'] = r1l9['bg']
r1l10 = tk.Frame(row1, bg="blue")
r1l10.grid(row=1, column=19, sticky="nsew")
r1l10.grid_rowconfigure(0, minsize=90)
r1l10.grid_columnconfigure(0, minsize=90)
r1l10t = tk.Label(r1l10, text=".....", font=("",40), fg="white")
r1l10t.grid()
r1l10t['bg'] = r1l10['bg']
r1l11 = tk.Frame(row1, bg="blue")
r1l11.grid(row=1, column=21, sticky="nsew")
r1l11.grid_rowconfigure(0, minsize=90)
r1l11.grid_columnconfigure(0, minsize=90)
r1l11t = tk.Label(r1l11, text=".....", font=("",40), fg="white")
r1l11t.grid()
r1l11t['bg'] = r1l11['bg']
r1l12 = tk.Frame(row1, bg="blue")
r1l12.grid(row=1, column=23, sticky="nsew")
r1l12.grid_rowconfigure(0, minsize=90)
r1l12.grid_columnconfigure(0, minsize=90)
r1l12t = tk.Label(r1l12, text=".....", font=("",40), fg="white")
r1l12t.grid()
r1l12t['bg'] = r1l12['bg']
r1l13 = tk.Frame(row1, bg="blue")
r1l13.grid(row=1, column=25, sticky="nsew")
r1l13.grid_rowconfigure(0, minsize=90)
r1l13.grid_columnconfigure(0, minsize=90)
r1l13t = tk.Label(r1l13, text=".....", font=("",40), fg="white")
r1l13t.grid()
r1l13t['bg'] = r1l13['bg']
r1l14 = tk.Frame(row1, bg="blue")
r1l14.grid(row=1, column=27, sticky="nsew")
r1l14.grid_rowconfigure(0, minsize=90)
r1l14.grid_columnconfigure(0, minsize=90)
r1l14t = tk.Label(r1l14, text=".....", font=("",40), fg="white")
r1l14t.grid()
r1l14t['bg'] = r1l14['bg']
r1l15 = tk.Frame(row1, bg="blue")
r1l15.grid(row=1, column=29, sticky="nsew")
r1l15.grid_rowconfigure(0, minsize=90)
r1l15.grid_columnconfigure(0, minsize=90)
r1l15t = tk.Label(r1l15, text=".....", font=("",40), fg="white")
r1l15t.grid()
r1l15t['bg'] = r1l15['bg']
r1l16 = tk.Frame(row1, bg="blue")
r1l16.grid(row=1, column=31, sticky="nsew")
r1l16.grid_rowconfigure(0, minsize=90)
r1l16.grid_columnconfigure(0, minsize=90)
r1l16t = tk.Label(r1l16, text=".....", font=("",40), fg="white")
r1l16t.grid()
r1l16t['bg'] = r1l16['bg']
r1l17 = tk.Frame(row1, bg="blue")
r1l17.grid(row=1, column=33, sticky="nsew")
r1l17.grid_rowconfigure(0, minsize=90)
r1l17.grid_columnconfigure(0, minsize=90)
r1l17t = tk.Label(r1l17, text=".....", font=("",40), fg="white")
r1l17t.grid()
r1l17t['bg'] = r1l17['bg']
r1l18 = tk.Frame(row1, bg="blue")
r1l18.grid(row=1, column=35, sticky="nsew")
r1l18.grid_rowconfigure(0, minsize=90)
r1l18.grid_columnconfigure(0, minsize=90)
r1l18t = tk.Label(r1l18, text=".....", font=("",40), fg="white")
r1l18t.grid()
r1l18t['bg'] = r1l18['bg']
r1l19 = tk.Frame(row1, bg="blue")
r1l19.grid(row=1, column=37, sticky="nsew")
r1l19.grid_rowconfigure(0, minsize=90)
r1l19.grid_columnconfigure(0, minsize=90)
r1l19t = tk.Label(r1l19, text=".....", font=("",40), fg="white")
r1l19t.grid()
r1l19t['bg'] = r1l19['bg']
app = lingo()
app.wm_geometry("1810x1080")
app.resizable(False, False)
app.title("pls work")
app.mainloop()
so i added the whole code this time, and tried to replace r1l2t.grid() with r1l2t.after(1000, r1l2t.grid()) but with no change....
python tkinter frame
|
show 1 more comment
so i am trying to create multiple frames with a button but with delays between them, so i press the button --> create frame1 --> wait 5 second --> create frame2 and so on. But the problem is that when i press the button it takes 5 seconds and displays the frame's at the same time. hopefully someone has any idea how to solve this.
import tkinter as tk
import time
class lingo(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
self.grid_rowconfigure(0,minsize=1080)
self.grid_columnconfigure(0,minsize=1810)
m = tk.Frame(self)
m.grid(row=0, column=0,sticky="nsew")
h1 = tk.Frame(m)
h1.grid(row=0, column=0)
e1 = tk.Entry(h1)
e1.grid(row=0, column=0)
def get_entry():
entry1 = e1.get()
print(entry1)
print(list(entry1))
def test1():
r1l1t = tk.Label(r1l1, text=a1, font=("",40), fg="white")
r1l1t.grid()
r1l1t['bg'] = r1l1['bg']
r1l2t = tk.Label(r1l2, text=a2, font=("",40), fg="white")
r1l2t.grid()
r1l2t['bg'] = r1l2['bg']
if len(entry1) < 19:
print("te kort woord")
if len(entry1) > 19:
print("te lang woord")
if len(entry1) == 19:
a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19 = list(entry1)
test1()
m.grid_rowconfigure(0, minsize=100)
m.grid_rowconfigure(1, minsize=100)
m.grid_columnconfigure(0, minsize=1810)
b1 = tk.Button(h1, bg="blue", text="enter", command=get_entry)
b1.grid(row=0, column=1)
row1 = tk.Frame(m, bg="grey")
row1.grid(row=1, column=0, sticky="nsew")
for c in range(0, 38):
if c % 2 == 0:
row1.grid_columnconfigure(c, minsize=5)
else:
row1.grid_columnconfigure(c, minsize=90)
row1.grid_rowconfigure(0, minsize=5)
row1.grid_rowconfigure(1, minsize=90)
row1.grid_rowconfigure(2, minsize=5)
r1l1 = tk.Frame(row1, bg="blue")
r1l1.grid(row=1, column=1, sticky="nsew")
r1l1.grid_rowconfigure(0, minsize=90)
r1l1.grid_columnconfigure(0, minsize=90)
# r1l1t = tk.Label(r1l1, text=".....", font=("",40), fg="white")
# r1l1t.grid()
# r1l1t['bg'] = r1l1['bg']
r1l2 = tk.Frame(row1, bg="blue")
r1l2.grid(row=1, column=3, sticky="nsew")
r1l2.grid_rowconfigure(0, minsize=90)
r1l2.grid_columnconfigure(0, minsize=90)
# r1l2t = tk.Label(r1l2, text=".....", font=("",40), fg="white")
# r1l2t.grid()
# r1l2t['bg'] = r1l2['bg']
r1l3 = tk.Frame(row1, bg="blue")
r1l3.grid(row=1, column=5, sticky="nsew")
r1l3.grid_rowconfigure(0, minsize=90)
r1l3.grid_columnconfigure(0, minsize=90)
r1l3t = tk.Label(r1l3, text=".....", font=("",40), fg="white")
r1l3t.grid()
r1l3t['bg'] = r1l3['bg']
r1l4 = tk.Frame(row1, bg="blue")
r1l4.grid(row=1, column=7, sticky="nsew")
r1l4.grid_rowconfigure(0, minsize=90)
r1l4.grid_columnconfigure(0, minsize=90)
r1l4t = tk.Label(r1l4, text=".....", font=("",40), fg="white")
r1l4t.grid()
r1l4t['bg'] = r1l4['bg']
r1l5 = tk.Frame(row1, bg="blue")
r1l5.grid(row=1, column=9, sticky="nsew")
r1l5.grid_rowconfigure(0, minsize=90)
r1l5.grid_columnconfigure(0, minsize=90)
r1l5t = tk.Label(r1l5, text=".....", font=("",40), fg="white")
r1l5t.grid()
r1l5t['bg'] = r1l5['bg']
r1l6 = tk.Frame(row1, bg="blue")
r1l6.grid(row=1, column=11, sticky="nsew")
r1l6.grid_rowconfigure(0, minsize=90)
r1l6.grid_columnconfigure(0, minsize=90)
r1l6t = tk.Label(r1l6, text=".....", font=("",40), fg="white")
r1l6t.grid()
r1l6t['bg'] = r1l6['bg']
r1l7 = tk.Frame(row1, bg="blue")
r1l7.grid(row=1, column=13, sticky="nsew")
r1l7.grid_rowconfigure(0, minsize=90)
r1l7.grid_columnconfigure(0, minsize=90)
r1l7t = tk.Label(r1l7, text=".....", font=("",40), fg="white")
r1l7t.grid()
r1l7t['bg'] = r1l7['bg']
r1l8 = tk.Frame(row1, bg="blue")
r1l8.grid(row=1, column=15, sticky="nsew")
r1l8.grid_rowconfigure(0, minsize=90)
r1l8.grid_columnconfigure(0, minsize=90)
r1l8t = tk.Label(r1l8, text=".....", font=("",40), fg="white")
r1l8t.grid()
r1l8t['bg'] = r1l8['bg']
r1l9 = tk.Frame(row1, bg="blue")
r1l9.grid(row=1, column=17, sticky="nsew")
r1l9.grid_rowconfigure(0, minsize=90)
r1l9.grid_columnconfigure(0, minsize=90)
r1l9t = tk.Label(r1l9, text=".....", font=("",40), fg="white")
r1l9t.grid()
r1l9t['bg'] = r1l9['bg']
r1l10 = tk.Frame(row1, bg="blue")
r1l10.grid(row=1, column=19, sticky="nsew")
r1l10.grid_rowconfigure(0, minsize=90)
r1l10.grid_columnconfigure(0, minsize=90)
r1l10t = tk.Label(r1l10, text=".....", font=("",40), fg="white")
r1l10t.grid()
r1l10t['bg'] = r1l10['bg']
r1l11 = tk.Frame(row1, bg="blue")
r1l11.grid(row=1, column=21, sticky="nsew")
r1l11.grid_rowconfigure(0, minsize=90)
r1l11.grid_columnconfigure(0, minsize=90)
r1l11t = tk.Label(r1l11, text=".....", font=("",40), fg="white")
r1l11t.grid()
r1l11t['bg'] = r1l11['bg']
r1l12 = tk.Frame(row1, bg="blue")
r1l12.grid(row=1, column=23, sticky="nsew")
r1l12.grid_rowconfigure(0, minsize=90)
r1l12.grid_columnconfigure(0, minsize=90)
r1l12t = tk.Label(r1l12, text=".....", font=("",40), fg="white")
r1l12t.grid()
r1l12t['bg'] = r1l12['bg']
r1l13 = tk.Frame(row1, bg="blue")
r1l13.grid(row=1, column=25, sticky="nsew")
r1l13.grid_rowconfigure(0, minsize=90)
r1l13.grid_columnconfigure(0, minsize=90)
r1l13t = tk.Label(r1l13, text=".....", font=("",40), fg="white")
r1l13t.grid()
r1l13t['bg'] = r1l13['bg']
r1l14 = tk.Frame(row1, bg="blue")
r1l14.grid(row=1, column=27, sticky="nsew")
r1l14.grid_rowconfigure(0, minsize=90)
r1l14.grid_columnconfigure(0, minsize=90)
r1l14t = tk.Label(r1l14, text=".....", font=("",40), fg="white")
r1l14t.grid()
r1l14t['bg'] = r1l14['bg']
r1l15 = tk.Frame(row1, bg="blue")
r1l15.grid(row=1, column=29, sticky="nsew")
r1l15.grid_rowconfigure(0, minsize=90)
r1l15.grid_columnconfigure(0, minsize=90)
r1l15t = tk.Label(r1l15, text=".....", font=("",40), fg="white")
r1l15t.grid()
r1l15t['bg'] = r1l15['bg']
r1l16 = tk.Frame(row1, bg="blue")
r1l16.grid(row=1, column=31, sticky="nsew")
r1l16.grid_rowconfigure(0, minsize=90)
r1l16.grid_columnconfigure(0, minsize=90)
r1l16t = tk.Label(r1l16, text=".....", font=("",40), fg="white")
r1l16t.grid()
r1l16t['bg'] = r1l16['bg']
r1l17 = tk.Frame(row1, bg="blue")
r1l17.grid(row=1, column=33, sticky="nsew")
r1l17.grid_rowconfigure(0, minsize=90)
r1l17.grid_columnconfigure(0, minsize=90)
r1l17t = tk.Label(r1l17, text=".....", font=("",40), fg="white")
r1l17t.grid()
r1l17t['bg'] = r1l17['bg']
r1l18 = tk.Frame(row1, bg="blue")
r1l18.grid(row=1, column=35, sticky="nsew")
r1l18.grid_rowconfigure(0, minsize=90)
r1l18.grid_columnconfigure(0, minsize=90)
r1l18t = tk.Label(r1l18, text=".....", font=("",40), fg="white")
r1l18t.grid()
r1l18t['bg'] = r1l18['bg']
r1l19 = tk.Frame(row1, bg="blue")
r1l19.grid(row=1, column=37, sticky="nsew")
r1l19.grid_rowconfigure(0, minsize=90)
r1l19.grid_columnconfigure(0, minsize=90)
r1l19t = tk.Label(r1l19, text=".....", font=("",40), fg="white")
r1l19t.grid()
r1l19t['bg'] = r1l19['bg']
app = lingo()
app.wm_geometry("1810x1080")
app.resizable(False, False)
app.title("pls work")
app.mainloop()
so i added the whole code this time, and tried to replace r1l2t.grid() with r1l2t.after(1000, r1l2t.grid()) but with no change....
python tkinter frame
1
Actually it is not enough. I think somethingr1l1.update_idletasks()
might help you but without a Minimal, Complete, and Verifiable example it is just a long shot..
– Goyo
Nov 23 '18 at 12:13
1
It is usually not recommended to usetime.sleep
along with tkinter because it suspends tkinter's mainloop and therefore freezes the GUI. To execute a command with a delay, you can use theafter
command provided by tkinter instead.
– j_4321
Nov 23 '18 at 19:52
@Goyo i tried it to use update_idletask but with no luck, and i added the whole code
– istvan kolkert
Nov 26 '18 at 8:32
@j_4321 what do you mean by time.sleep suspends tkinter? and how should is use the after command in my code? because r1l2t.after(1000, r1l2t.grid()) doesn't change anything :(
– istvan kolkert
Nov 26 '18 at 8:34
It is better you you make tour Minimal, Complete, and Verifiable example actually minimal.
– Goyo
Nov 26 '18 at 9:25
|
show 1 more comment
so i am trying to create multiple frames with a button but with delays between them, so i press the button --> create frame1 --> wait 5 second --> create frame2 and so on. But the problem is that when i press the button it takes 5 seconds and displays the frame's at the same time. hopefully someone has any idea how to solve this.
import tkinter as tk
import time
class lingo(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
self.grid_rowconfigure(0,minsize=1080)
self.grid_columnconfigure(0,minsize=1810)
m = tk.Frame(self)
m.grid(row=0, column=0,sticky="nsew")
h1 = tk.Frame(m)
h1.grid(row=0, column=0)
e1 = tk.Entry(h1)
e1.grid(row=0, column=0)
def get_entry():
entry1 = e1.get()
print(entry1)
print(list(entry1))
def test1():
r1l1t = tk.Label(r1l1, text=a1, font=("",40), fg="white")
r1l1t.grid()
r1l1t['bg'] = r1l1['bg']
r1l2t = tk.Label(r1l2, text=a2, font=("",40), fg="white")
r1l2t.grid()
r1l2t['bg'] = r1l2['bg']
if len(entry1) < 19:
print("te kort woord")
if len(entry1) > 19:
print("te lang woord")
if len(entry1) == 19:
a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19 = list(entry1)
test1()
m.grid_rowconfigure(0, minsize=100)
m.grid_rowconfigure(1, minsize=100)
m.grid_columnconfigure(0, minsize=1810)
b1 = tk.Button(h1, bg="blue", text="enter", command=get_entry)
b1.grid(row=0, column=1)
row1 = tk.Frame(m, bg="grey")
row1.grid(row=1, column=0, sticky="nsew")
for c in range(0, 38):
if c % 2 == 0:
row1.grid_columnconfigure(c, minsize=5)
else:
row1.grid_columnconfigure(c, minsize=90)
row1.grid_rowconfigure(0, minsize=5)
row1.grid_rowconfigure(1, minsize=90)
row1.grid_rowconfigure(2, minsize=5)
r1l1 = tk.Frame(row1, bg="blue")
r1l1.grid(row=1, column=1, sticky="nsew")
r1l1.grid_rowconfigure(0, minsize=90)
r1l1.grid_columnconfigure(0, minsize=90)
# r1l1t = tk.Label(r1l1, text=".....", font=("",40), fg="white")
# r1l1t.grid()
# r1l1t['bg'] = r1l1['bg']
r1l2 = tk.Frame(row1, bg="blue")
r1l2.grid(row=1, column=3, sticky="nsew")
r1l2.grid_rowconfigure(0, minsize=90)
r1l2.grid_columnconfigure(0, minsize=90)
# r1l2t = tk.Label(r1l2, text=".....", font=("",40), fg="white")
# r1l2t.grid()
# r1l2t['bg'] = r1l2['bg']
r1l3 = tk.Frame(row1, bg="blue")
r1l3.grid(row=1, column=5, sticky="nsew")
r1l3.grid_rowconfigure(0, minsize=90)
r1l3.grid_columnconfigure(0, minsize=90)
r1l3t = tk.Label(r1l3, text=".....", font=("",40), fg="white")
r1l3t.grid()
r1l3t['bg'] = r1l3['bg']
r1l4 = tk.Frame(row1, bg="blue")
r1l4.grid(row=1, column=7, sticky="nsew")
r1l4.grid_rowconfigure(0, minsize=90)
r1l4.grid_columnconfigure(0, minsize=90)
r1l4t = tk.Label(r1l4, text=".....", font=("",40), fg="white")
r1l4t.grid()
r1l4t['bg'] = r1l4['bg']
r1l5 = tk.Frame(row1, bg="blue")
r1l5.grid(row=1, column=9, sticky="nsew")
r1l5.grid_rowconfigure(0, minsize=90)
r1l5.grid_columnconfigure(0, minsize=90)
r1l5t = tk.Label(r1l5, text=".....", font=("",40), fg="white")
r1l5t.grid()
r1l5t['bg'] = r1l5['bg']
r1l6 = tk.Frame(row1, bg="blue")
r1l6.grid(row=1, column=11, sticky="nsew")
r1l6.grid_rowconfigure(0, minsize=90)
r1l6.grid_columnconfigure(0, minsize=90)
r1l6t = tk.Label(r1l6, text=".....", font=("",40), fg="white")
r1l6t.grid()
r1l6t['bg'] = r1l6['bg']
r1l7 = tk.Frame(row1, bg="blue")
r1l7.grid(row=1, column=13, sticky="nsew")
r1l7.grid_rowconfigure(0, minsize=90)
r1l7.grid_columnconfigure(0, minsize=90)
r1l7t = tk.Label(r1l7, text=".....", font=("",40), fg="white")
r1l7t.grid()
r1l7t['bg'] = r1l7['bg']
r1l8 = tk.Frame(row1, bg="blue")
r1l8.grid(row=1, column=15, sticky="nsew")
r1l8.grid_rowconfigure(0, minsize=90)
r1l8.grid_columnconfigure(0, minsize=90)
r1l8t = tk.Label(r1l8, text=".....", font=("",40), fg="white")
r1l8t.grid()
r1l8t['bg'] = r1l8['bg']
r1l9 = tk.Frame(row1, bg="blue")
r1l9.grid(row=1, column=17, sticky="nsew")
r1l9.grid_rowconfigure(0, minsize=90)
r1l9.grid_columnconfigure(0, minsize=90)
r1l9t = tk.Label(r1l9, text=".....", font=("",40), fg="white")
r1l9t.grid()
r1l9t['bg'] = r1l9['bg']
r1l10 = tk.Frame(row1, bg="blue")
r1l10.grid(row=1, column=19, sticky="nsew")
r1l10.grid_rowconfigure(0, minsize=90)
r1l10.grid_columnconfigure(0, minsize=90)
r1l10t = tk.Label(r1l10, text=".....", font=("",40), fg="white")
r1l10t.grid()
r1l10t['bg'] = r1l10['bg']
r1l11 = tk.Frame(row1, bg="blue")
r1l11.grid(row=1, column=21, sticky="nsew")
r1l11.grid_rowconfigure(0, minsize=90)
r1l11.grid_columnconfigure(0, minsize=90)
r1l11t = tk.Label(r1l11, text=".....", font=("",40), fg="white")
r1l11t.grid()
r1l11t['bg'] = r1l11['bg']
r1l12 = tk.Frame(row1, bg="blue")
r1l12.grid(row=1, column=23, sticky="nsew")
r1l12.grid_rowconfigure(0, minsize=90)
r1l12.grid_columnconfigure(0, minsize=90)
r1l12t = tk.Label(r1l12, text=".....", font=("",40), fg="white")
r1l12t.grid()
r1l12t['bg'] = r1l12['bg']
r1l13 = tk.Frame(row1, bg="blue")
r1l13.grid(row=1, column=25, sticky="nsew")
r1l13.grid_rowconfigure(0, minsize=90)
r1l13.grid_columnconfigure(0, minsize=90)
r1l13t = tk.Label(r1l13, text=".....", font=("",40), fg="white")
r1l13t.grid()
r1l13t['bg'] = r1l13['bg']
r1l14 = tk.Frame(row1, bg="blue")
r1l14.grid(row=1, column=27, sticky="nsew")
r1l14.grid_rowconfigure(0, minsize=90)
r1l14.grid_columnconfigure(0, minsize=90)
r1l14t = tk.Label(r1l14, text=".....", font=("",40), fg="white")
r1l14t.grid()
r1l14t['bg'] = r1l14['bg']
r1l15 = tk.Frame(row1, bg="blue")
r1l15.grid(row=1, column=29, sticky="nsew")
r1l15.grid_rowconfigure(0, minsize=90)
r1l15.grid_columnconfigure(0, minsize=90)
r1l15t = tk.Label(r1l15, text=".....", font=("",40), fg="white")
r1l15t.grid()
r1l15t['bg'] = r1l15['bg']
r1l16 = tk.Frame(row1, bg="blue")
r1l16.grid(row=1, column=31, sticky="nsew")
r1l16.grid_rowconfigure(0, minsize=90)
r1l16.grid_columnconfigure(0, minsize=90)
r1l16t = tk.Label(r1l16, text=".....", font=("",40), fg="white")
r1l16t.grid()
r1l16t['bg'] = r1l16['bg']
r1l17 = tk.Frame(row1, bg="blue")
r1l17.grid(row=1, column=33, sticky="nsew")
r1l17.grid_rowconfigure(0, minsize=90)
r1l17.grid_columnconfigure(0, minsize=90)
r1l17t = tk.Label(r1l17, text=".....", font=("",40), fg="white")
r1l17t.grid()
r1l17t['bg'] = r1l17['bg']
r1l18 = tk.Frame(row1, bg="blue")
r1l18.grid(row=1, column=35, sticky="nsew")
r1l18.grid_rowconfigure(0, minsize=90)
r1l18.grid_columnconfigure(0, minsize=90)
r1l18t = tk.Label(r1l18, text=".....", font=("",40), fg="white")
r1l18t.grid()
r1l18t['bg'] = r1l18['bg']
r1l19 = tk.Frame(row1, bg="blue")
r1l19.grid(row=1, column=37, sticky="nsew")
r1l19.grid_rowconfigure(0, minsize=90)
r1l19.grid_columnconfigure(0, minsize=90)
r1l19t = tk.Label(r1l19, text=".....", font=("",40), fg="white")
r1l19t.grid()
r1l19t['bg'] = r1l19['bg']
app = lingo()
app.wm_geometry("1810x1080")
app.resizable(False, False)
app.title("pls work")
app.mainloop()
so i added the whole code this time, and tried to replace r1l2t.grid() with r1l2t.after(1000, r1l2t.grid()) but with no change....
python tkinter frame
so i am trying to create multiple frames with a button but with delays between them, so i press the button --> create frame1 --> wait 5 second --> create frame2 and so on. But the problem is that when i press the button it takes 5 seconds and displays the frame's at the same time. hopefully someone has any idea how to solve this.
import tkinter as tk
import time
class lingo(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
self.grid_rowconfigure(0,minsize=1080)
self.grid_columnconfigure(0,minsize=1810)
m = tk.Frame(self)
m.grid(row=0, column=0,sticky="nsew")
h1 = tk.Frame(m)
h1.grid(row=0, column=0)
e1 = tk.Entry(h1)
e1.grid(row=0, column=0)
def get_entry():
entry1 = e1.get()
print(entry1)
print(list(entry1))
def test1():
r1l1t = tk.Label(r1l1, text=a1, font=("",40), fg="white")
r1l1t.grid()
r1l1t['bg'] = r1l1['bg']
r1l2t = tk.Label(r1l2, text=a2, font=("",40), fg="white")
r1l2t.grid()
r1l2t['bg'] = r1l2['bg']
if len(entry1) < 19:
print("te kort woord")
if len(entry1) > 19:
print("te lang woord")
if len(entry1) == 19:
a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19 = list(entry1)
test1()
m.grid_rowconfigure(0, minsize=100)
m.grid_rowconfigure(1, minsize=100)
m.grid_columnconfigure(0, minsize=1810)
b1 = tk.Button(h1, bg="blue", text="enter", command=get_entry)
b1.grid(row=0, column=1)
row1 = tk.Frame(m, bg="grey")
row1.grid(row=1, column=0, sticky="nsew")
for c in range(0, 38):
if c % 2 == 0:
row1.grid_columnconfigure(c, minsize=5)
else:
row1.grid_columnconfigure(c, minsize=90)
row1.grid_rowconfigure(0, minsize=5)
row1.grid_rowconfigure(1, minsize=90)
row1.grid_rowconfigure(2, minsize=5)
r1l1 = tk.Frame(row1, bg="blue")
r1l1.grid(row=1, column=1, sticky="nsew")
r1l1.grid_rowconfigure(0, minsize=90)
r1l1.grid_columnconfigure(0, minsize=90)
# r1l1t = tk.Label(r1l1, text=".....", font=("",40), fg="white")
# r1l1t.grid()
# r1l1t['bg'] = r1l1['bg']
r1l2 = tk.Frame(row1, bg="blue")
r1l2.grid(row=1, column=3, sticky="nsew")
r1l2.grid_rowconfigure(0, minsize=90)
r1l2.grid_columnconfigure(0, minsize=90)
# r1l2t = tk.Label(r1l2, text=".....", font=("",40), fg="white")
# r1l2t.grid()
# r1l2t['bg'] = r1l2['bg']
r1l3 = tk.Frame(row1, bg="blue")
r1l3.grid(row=1, column=5, sticky="nsew")
r1l3.grid_rowconfigure(0, minsize=90)
r1l3.grid_columnconfigure(0, minsize=90)
r1l3t = tk.Label(r1l3, text=".....", font=("",40), fg="white")
r1l3t.grid()
r1l3t['bg'] = r1l3['bg']
r1l4 = tk.Frame(row1, bg="blue")
r1l4.grid(row=1, column=7, sticky="nsew")
r1l4.grid_rowconfigure(0, minsize=90)
r1l4.grid_columnconfigure(0, minsize=90)
r1l4t = tk.Label(r1l4, text=".....", font=("",40), fg="white")
r1l4t.grid()
r1l4t['bg'] = r1l4['bg']
r1l5 = tk.Frame(row1, bg="blue")
r1l5.grid(row=1, column=9, sticky="nsew")
r1l5.grid_rowconfigure(0, minsize=90)
r1l5.grid_columnconfigure(0, minsize=90)
r1l5t = tk.Label(r1l5, text=".....", font=("",40), fg="white")
r1l5t.grid()
r1l5t['bg'] = r1l5['bg']
r1l6 = tk.Frame(row1, bg="blue")
r1l6.grid(row=1, column=11, sticky="nsew")
r1l6.grid_rowconfigure(0, minsize=90)
r1l6.grid_columnconfigure(0, minsize=90)
r1l6t = tk.Label(r1l6, text=".....", font=("",40), fg="white")
r1l6t.grid()
r1l6t['bg'] = r1l6['bg']
r1l7 = tk.Frame(row1, bg="blue")
r1l7.grid(row=1, column=13, sticky="nsew")
r1l7.grid_rowconfigure(0, minsize=90)
r1l7.grid_columnconfigure(0, minsize=90)
r1l7t = tk.Label(r1l7, text=".....", font=("",40), fg="white")
r1l7t.grid()
r1l7t['bg'] = r1l7['bg']
r1l8 = tk.Frame(row1, bg="blue")
r1l8.grid(row=1, column=15, sticky="nsew")
r1l8.grid_rowconfigure(0, minsize=90)
r1l8.grid_columnconfigure(0, minsize=90)
r1l8t = tk.Label(r1l8, text=".....", font=("",40), fg="white")
r1l8t.grid()
r1l8t['bg'] = r1l8['bg']
r1l9 = tk.Frame(row1, bg="blue")
r1l9.grid(row=1, column=17, sticky="nsew")
r1l9.grid_rowconfigure(0, minsize=90)
r1l9.grid_columnconfigure(0, minsize=90)
r1l9t = tk.Label(r1l9, text=".....", font=("",40), fg="white")
r1l9t.grid()
r1l9t['bg'] = r1l9['bg']
r1l10 = tk.Frame(row1, bg="blue")
r1l10.grid(row=1, column=19, sticky="nsew")
r1l10.grid_rowconfigure(0, minsize=90)
r1l10.grid_columnconfigure(0, minsize=90)
r1l10t = tk.Label(r1l10, text=".....", font=("",40), fg="white")
r1l10t.grid()
r1l10t['bg'] = r1l10['bg']
r1l11 = tk.Frame(row1, bg="blue")
r1l11.grid(row=1, column=21, sticky="nsew")
r1l11.grid_rowconfigure(0, minsize=90)
r1l11.grid_columnconfigure(0, minsize=90)
r1l11t = tk.Label(r1l11, text=".....", font=("",40), fg="white")
r1l11t.grid()
r1l11t['bg'] = r1l11['bg']
r1l12 = tk.Frame(row1, bg="blue")
r1l12.grid(row=1, column=23, sticky="nsew")
r1l12.grid_rowconfigure(0, minsize=90)
r1l12.grid_columnconfigure(0, minsize=90)
r1l12t = tk.Label(r1l12, text=".....", font=("",40), fg="white")
r1l12t.grid()
r1l12t['bg'] = r1l12['bg']
r1l13 = tk.Frame(row1, bg="blue")
r1l13.grid(row=1, column=25, sticky="nsew")
r1l13.grid_rowconfigure(0, minsize=90)
r1l13.grid_columnconfigure(0, minsize=90)
r1l13t = tk.Label(r1l13, text=".....", font=("",40), fg="white")
r1l13t.grid()
r1l13t['bg'] = r1l13['bg']
r1l14 = tk.Frame(row1, bg="blue")
r1l14.grid(row=1, column=27, sticky="nsew")
r1l14.grid_rowconfigure(0, minsize=90)
r1l14.grid_columnconfigure(0, minsize=90)
r1l14t = tk.Label(r1l14, text=".....", font=("",40), fg="white")
r1l14t.grid()
r1l14t['bg'] = r1l14['bg']
r1l15 = tk.Frame(row1, bg="blue")
r1l15.grid(row=1, column=29, sticky="nsew")
r1l15.grid_rowconfigure(0, minsize=90)
r1l15.grid_columnconfigure(0, minsize=90)
r1l15t = tk.Label(r1l15, text=".....", font=("",40), fg="white")
r1l15t.grid()
r1l15t['bg'] = r1l15['bg']
r1l16 = tk.Frame(row1, bg="blue")
r1l16.grid(row=1, column=31, sticky="nsew")
r1l16.grid_rowconfigure(0, minsize=90)
r1l16.grid_columnconfigure(0, minsize=90)
r1l16t = tk.Label(r1l16, text=".....", font=("",40), fg="white")
r1l16t.grid()
r1l16t['bg'] = r1l16['bg']
r1l17 = tk.Frame(row1, bg="blue")
r1l17.grid(row=1, column=33, sticky="nsew")
r1l17.grid_rowconfigure(0, minsize=90)
r1l17.grid_columnconfigure(0, minsize=90)
r1l17t = tk.Label(r1l17, text=".....", font=("",40), fg="white")
r1l17t.grid()
r1l17t['bg'] = r1l17['bg']
r1l18 = tk.Frame(row1, bg="blue")
r1l18.grid(row=1, column=35, sticky="nsew")
r1l18.grid_rowconfigure(0, minsize=90)
r1l18.grid_columnconfigure(0, minsize=90)
r1l18t = tk.Label(r1l18, text=".....", font=("",40), fg="white")
r1l18t.grid()
r1l18t['bg'] = r1l18['bg']
r1l19 = tk.Frame(row1, bg="blue")
r1l19.grid(row=1, column=37, sticky="nsew")
r1l19.grid_rowconfigure(0, minsize=90)
r1l19.grid_columnconfigure(0, minsize=90)
r1l19t = tk.Label(r1l19, text=".....", font=("",40), fg="white")
r1l19t.grid()
r1l19t['bg'] = r1l19['bg']
app = lingo()
app.wm_geometry("1810x1080")
app.resizable(False, False)
app.title("pls work")
app.mainloop()
so i added the whole code this time, and tried to replace r1l2t.grid() with r1l2t.after(1000, r1l2t.grid()) but with no change....
python tkinter frame
python tkinter frame
edited Nov 26 '18 at 8:29
istvan kolkert
asked Nov 23 '18 at 12:07
istvan kolkertistvan kolkert
82
82
1
Actually it is not enough. I think somethingr1l1.update_idletasks()
might help you but without a Minimal, Complete, and Verifiable example it is just a long shot..
– Goyo
Nov 23 '18 at 12:13
1
It is usually not recommended to usetime.sleep
along with tkinter because it suspends tkinter's mainloop and therefore freezes the GUI. To execute a command with a delay, you can use theafter
command provided by tkinter instead.
– j_4321
Nov 23 '18 at 19:52
@Goyo i tried it to use update_idletask but with no luck, and i added the whole code
– istvan kolkert
Nov 26 '18 at 8:32
@j_4321 what do you mean by time.sleep suspends tkinter? and how should is use the after command in my code? because r1l2t.after(1000, r1l2t.grid()) doesn't change anything :(
– istvan kolkert
Nov 26 '18 at 8:34
It is better you you make tour Minimal, Complete, and Verifiable example actually minimal.
– Goyo
Nov 26 '18 at 9:25
|
show 1 more comment
1
Actually it is not enough. I think somethingr1l1.update_idletasks()
might help you but without a Minimal, Complete, and Verifiable example it is just a long shot..
– Goyo
Nov 23 '18 at 12:13
1
It is usually not recommended to usetime.sleep
along with tkinter because it suspends tkinter's mainloop and therefore freezes the GUI. To execute a command with a delay, you can use theafter
command provided by tkinter instead.
– j_4321
Nov 23 '18 at 19:52
@Goyo i tried it to use update_idletask but with no luck, and i added the whole code
– istvan kolkert
Nov 26 '18 at 8:32
@j_4321 what do you mean by time.sleep suspends tkinter? and how should is use the after command in my code? because r1l2t.after(1000, r1l2t.grid()) doesn't change anything :(
– istvan kolkert
Nov 26 '18 at 8:34
It is better you you make tour Minimal, Complete, and Verifiable example actually minimal.
– Goyo
Nov 26 '18 at 9:25
1
1
Actually it is not enough. I think something
r1l1.update_idletasks()
might help you but without a Minimal, Complete, and Verifiable example it is just a long shot..– Goyo
Nov 23 '18 at 12:13
Actually it is not enough. I think something
r1l1.update_idletasks()
might help you but without a Minimal, Complete, and Verifiable example it is just a long shot..– Goyo
Nov 23 '18 at 12:13
1
1
It is usually not recommended to use
time.sleep
along with tkinter because it suspends tkinter's mainloop and therefore freezes the GUI. To execute a command with a delay, you can use the after
command provided by tkinter instead.– j_4321
Nov 23 '18 at 19:52
It is usually not recommended to use
time.sleep
along with tkinter because it suspends tkinter's mainloop and therefore freezes the GUI. To execute a command with a delay, you can use the after
command provided by tkinter instead.– j_4321
Nov 23 '18 at 19:52
@Goyo i tried it to use update_idletask but with no luck, and i added the whole code
– istvan kolkert
Nov 26 '18 at 8:32
@Goyo i tried it to use update_idletask but with no luck, and i added the whole code
– istvan kolkert
Nov 26 '18 at 8:32
@j_4321 what do you mean by time.sleep suspends tkinter? and how should is use the after command in my code? because r1l2t.after(1000, r1l2t.grid()) doesn't change anything :(
– istvan kolkert
Nov 26 '18 at 8:34
@j_4321 what do you mean by time.sleep suspends tkinter? and how should is use the after command in my code? because r1l2t.after(1000, r1l2t.grid()) doesn't change anything :(
– istvan kolkert
Nov 26 '18 at 8:34
It is better you you make tour Minimal, Complete, and Verifiable example actually minimal.
– Goyo
Nov 26 '18 at 9:25
It is better you you make tour Minimal, Complete, and Verifiable example actually minimal.
– Goyo
Nov 26 '18 at 9:25
|
show 1 more comment
1 Answer
1
active
oldest
votes
Once you have launched Tkinter's mainloop, the GUI waits for events (keystrokes, mouse clicks, ...) and reacts immediately when one occurs (visual effects on the clicked button, ...). However, when you use time.sleep(5)
, you freeze this mainloop, so the GUI does not react when events occur but process them all after the end of the 5 seconds. In addition, the GUI does not update before the freezing so your first frame is displayed only at the end. You can force the GUI to update with the update_idletasks()
method.
Tkinter provides a method to execute commands with a delay without freezing the mainloop: app.after(delay , function, *args)
will execute function(*args)
after the given delay (in ms). The mistake you made is that you called r1l2t.after(1000, r1l2t.grid())
instead of r1l2t.after(1000, r1l2t.grid)
. Below is a small example creating two labels on a button press with a 5s delay between each:
import tkinter as tk
app = tk.Tk()
def display():
l1 = tk.Label(app, text='First label')
l2 = tk.Label(app, text='Second label')
l1.grid()
app.after(5000, l2.grid)
tk.Button(app, text='Display', command=display).grid()
app.mainloop()
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53446450%2ftkinter-creating-frames-with-button-and-delay%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Once you have launched Tkinter's mainloop, the GUI waits for events (keystrokes, mouse clicks, ...) and reacts immediately when one occurs (visual effects on the clicked button, ...). However, when you use time.sleep(5)
, you freeze this mainloop, so the GUI does not react when events occur but process them all after the end of the 5 seconds. In addition, the GUI does not update before the freezing so your first frame is displayed only at the end. You can force the GUI to update with the update_idletasks()
method.
Tkinter provides a method to execute commands with a delay without freezing the mainloop: app.after(delay , function, *args)
will execute function(*args)
after the given delay (in ms). The mistake you made is that you called r1l2t.after(1000, r1l2t.grid())
instead of r1l2t.after(1000, r1l2t.grid)
. Below is a small example creating two labels on a button press with a 5s delay between each:
import tkinter as tk
app = tk.Tk()
def display():
l1 = tk.Label(app, text='First label')
l2 = tk.Label(app, text='Second label')
l1.grid()
app.after(5000, l2.grid)
tk.Button(app, text='Display', command=display).grid()
app.mainloop()
add a comment |
Once you have launched Tkinter's mainloop, the GUI waits for events (keystrokes, mouse clicks, ...) and reacts immediately when one occurs (visual effects on the clicked button, ...). However, when you use time.sleep(5)
, you freeze this mainloop, so the GUI does not react when events occur but process them all after the end of the 5 seconds. In addition, the GUI does not update before the freezing so your first frame is displayed only at the end. You can force the GUI to update with the update_idletasks()
method.
Tkinter provides a method to execute commands with a delay without freezing the mainloop: app.after(delay , function, *args)
will execute function(*args)
after the given delay (in ms). The mistake you made is that you called r1l2t.after(1000, r1l2t.grid())
instead of r1l2t.after(1000, r1l2t.grid)
. Below is a small example creating two labels on a button press with a 5s delay between each:
import tkinter as tk
app = tk.Tk()
def display():
l1 = tk.Label(app, text='First label')
l2 = tk.Label(app, text='Second label')
l1.grid()
app.after(5000, l2.grid)
tk.Button(app, text='Display', command=display).grid()
app.mainloop()
add a comment |
Once you have launched Tkinter's mainloop, the GUI waits for events (keystrokes, mouse clicks, ...) and reacts immediately when one occurs (visual effects on the clicked button, ...). However, when you use time.sleep(5)
, you freeze this mainloop, so the GUI does not react when events occur but process them all after the end of the 5 seconds. In addition, the GUI does not update before the freezing so your first frame is displayed only at the end. You can force the GUI to update with the update_idletasks()
method.
Tkinter provides a method to execute commands with a delay without freezing the mainloop: app.after(delay , function, *args)
will execute function(*args)
after the given delay (in ms). The mistake you made is that you called r1l2t.after(1000, r1l2t.grid())
instead of r1l2t.after(1000, r1l2t.grid)
. Below is a small example creating two labels on a button press with a 5s delay between each:
import tkinter as tk
app = tk.Tk()
def display():
l1 = tk.Label(app, text='First label')
l2 = tk.Label(app, text='Second label')
l1.grid()
app.after(5000, l2.grid)
tk.Button(app, text='Display', command=display).grid()
app.mainloop()
Once you have launched Tkinter's mainloop, the GUI waits for events (keystrokes, mouse clicks, ...) and reacts immediately when one occurs (visual effects on the clicked button, ...). However, when you use time.sleep(5)
, you freeze this mainloop, so the GUI does not react when events occur but process them all after the end of the 5 seconds. In addition, the GUI does not update before the freezing so your first frame is displayed only at the end. You can force the GUI to update with the update_idletasks()
method.
Tkinter provides a method to execute commands with a delay without freezing the mainloop: app.after(delay , function, *args)
will execute function(*args)
after the given delay (in ms). The mistake you made is that you called r1l2t.after(1000, r1l2t.grid())
instead of r1l2t.after(1000, r1l2t.grid)
. Below is a small example creating two labels on a button press with a 5s delay between each:
import tkinter as tk
app = tk.Tk()
def display():
l1 = tk.Label(app, text='First label')
l2 = tk.Label(app, text='Second label')
l1.grid()
app.after(5000, l2.grid)
tk.Button(app, text='Display', command=display).grid()
app.mainloop()
answered Nov 26 '18 at 10:37
j_4321j_4321
6,10321530
6,10321530
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53446450%2ftkinter-creating-frames-with-button-and-delay%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Actually it is not enough. I think something
r1l1.update_idletasks()
might help you but without a Minimal, Complete, and Verifiable example it is just a long shot..– Goyo
Nov 23 '18 at 12:13
1
It is usually not recommended to use
time.sleep
along with tkinter because it suspends tkinter's mainloop and therefore freezes the GUI. To execute a command with a delay, you can use theafter
command provided by tkinter instead.– j_4321
Nov 23 '18 at 19:52
@Goyo i tried it to use update_idletask but with no luck, and i added the whole code
– istvan kolkert
Nov 26 '18 at 8:32
@j_4321 what do you mean by time.sleep suspends tkinter? and how should is use the after command in my code? because r1l2t.after(1000, r1l2t.grid()) doesn't change anything :(
– istvan kolkert
Nov 26 '18 at 8:34
It is better you you make tour Minimal, Complete, and Verifiable example actually minimal.
– Goyo
Nov 26 '18 at 9:25