Search code examples
pythontkinteruser-inputwait

python: how to wait in a for loop for user inputs in popup windows


I wanted to login to a webpage three times as follows.

  1. Push the "button1"
  2. Login window pops up then enter ID & PW and push the "Enter" button
  3. do something
  4. repeat #2 & 3 three times.

I am stuck at making the code wait until a user pushes the "Enter" button.

I'm pretty new to python. Any comments will be greatly helpful.

Main window popup login window

from tkinter import *
from tkinter import ttk  
import time

root = Tk()
root.geometry('400x500')


# functions ----------------------------------
def popup():
    global pop
    pop= Toplevel(root)
    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',width=10, font=20)
    btn1.grid(row=1, column=3, rowspan=2, sticky=N+E+W+S, padx=10, pady=10) 


def  button_():      
    for i in [1,2,3]: 
        popup() 
        

bt1=Button(root, text='button1', command=button_)
bt1.pack(pady=10)


root.mainloop()

==================================================

Thanks to you all. I finally got what I wanted. I take the Sujay logic and hope this help for those who may have the same problem.

import tkinter as tk
from tkinter import ttk  
import time


# functions ----------------------------------

# 0 I don't understand this part but it still works without this.
# Force you to pass in the master argument when creating widgets:
# tk._support_default_root = False
#----------------------------------------------------------------

def popup():
    global pop   
    pop = tk.Tk()       # 1. <-- pop= Toplevel(root)
    pop.title('Login')
    pop.geometry('450x100')

    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',width=10, font=20)
    btn1.grid(row=1, column=3, rowspan=2, sticky=N+E+W+S, padx=10, pady=10) 
    

    # 2.---------------------------------------------
    # Wait until there is only 1 window left open
    pop.mainloop(1)
    #---------------------------------------------
    
def  button_():
    for i in range(3):
        popup()

root = tk.Tk()
root.geometry('400x500')

bt1=tk.Button(root, text='button1', command=button_)
bt1.pack(pady=10)



# Wait until all windows have been closed
root.mainloop()

As you can see, code #1, #2 are what solve my problem. but I still don't understand why #1 works and what does #0 means.


Solution

  • What you can do is check if the popup window has been opened 3 times.

    from tkinter import *
    from tkinter import ttk  
    import time
    
    root = 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=1
    def  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()