python: how to wait in a for loop for user inputs in popup windows [duplicate]
What you can do is check if the popup window has been opened 3 times.
from tkinter import *from tkinter import ttk import timeroot = Tk()root.geometry('400x500')# functions ----------------------------------def popup(): global pop pop= Toplevel(root) pop.focus_force() pop.grab_set() pop.title('Login') pop.geometry('450x100') global me fr1=Frame(pop) fr1.pack(pady=5) lbl1=Label(fr1, text='ID :', font=20) lbl1.grid(row=1, column=1,pady=7) e1 = Entry(fr1, width=15,font=(25)) e1.grid(row=1, column=2) lbl2=Label(fr1, text='PW :', font=20) lbl2.grid(row=2, column=1) e2 = Entry(fr1, width=15,font=(25)) e2.grid(row=2, column=2) btn1 = Button(fr1, text='Enter',command=button_,width=10, font=20) btn1.grid(row=1, column=3, rowspan=2, sticky=N+E+W+S, padx=10, pady=10) done=1def button_(): global done if done==3: pop.destroy() else: done+=1 pop.destroy() popup() bt1=Button(root, text='button1', command=popup)bt1.pack(pady=10)root.mainloop()
Try something like this:
import tkinter as tkfrom tkinter import ttk import time# Force you to pass in the master argument when creating widgets:tk._support_default_root = Falsedef popup(): global pop_window pop_window = tk.Tk() pop_window.geometry("200x100") entry = tk.Entry(pop_window) entry.pack() button = tk.Button(pop_window, text="Enter", command=pop_window.destroy) button.pack() # Wait until there is only 1 window left open pop_window.mainloop(1)def button_(): global done if done: done = False for i in range(3): popup() done = Truedone = Trueroot = tk.Tk()root.geometry("200x200")button = tk.Button(root, text="Click me", command=button_)button.pack()# Close the program when the main window is destroyed:# Instead here you can call a function that sets `done` to `True`,# Closes the main window as well as the `pop_window` if it existsroot.protocol("WM_DELETE_WINDOW", exit)# Wait until all windows have been closedroot.mainloop()
It uses multiple instances of Tk()
and waits until there is only 1 window left open before continuing with the for
loop. That is done using: pop_window.mainloop(1)
It uses multiple instances of Tk()
so you need to be careful when creating StringVar
/IntVar
/.../PhotoImage
s. As long as you always pass in the master
argument when creating those variables, you shouldn't encounter any problems.