elnino
elnino

Reputation: 173

Entry box inside toplevel window in tkinter

I have written a code to show Entry widget inside top-level window in tkinter but it is not showing anything when I run it. Below is the code from where I am calling the top-level window:

#file: app.py

# enter new racer
btnNewRacer = Button(app, text = "Enter New Racer", style = 'W.TButton', command = EnterRacer)
btnNewRacer.grid(row = 0, column = 0, pady = 50, padx = 50)

And this is the code where I have written the code for Entry widget:

#file: new_racer.py

def EnterRacer(): 
      
    # Toplevel object which will  
    # be treated as a new window 
    racerWindow = Toplevel() 
    racerWindow['background']='#2A3132'
  
    # sets the title of the 
    # Toplevel widget 
    racerWindow.title("Enter New Racer") 
  
    # sets the geometry of toplevel 
    racerWindow.geometry("700x500") 
  
    # A Label widget to show in toplevel 
    Label(racerWindow, text ="Enter new racer window").pack()
    Label(racerWindow, text="First Name").pack()
    Label(racerWindow, text="Last Name").pack().grid(row=5)

    entry_1 = Entry(racerWindow)
    entry_1.pack()
    entry_1.grid(row=5)

When I run the app.py and I click on the "Enter New Racer" button I don't see any entry widget. Can anyone please help? Thanks.

Upvotes: 0

Views: 630

Answers (1)

Delrius Euphoria
Delrius Euphoria

Reputation: 15088

See the issue here is that, you cannot do a combination of .pack() and .grid(). You will have to use one of them only.

You can change your function to this:

For .pack()

    # Toplevel object which will  
    # be treated as a new window 
    racerWindow = Toplevel() 
    racerWindow['background']='#2A3132'

    # sets the title of the 
    # Toplevel widget 
    racerWindow.title("Enter New Racer") 

    # sets the geometry of toplevel 
    racerWindow.geometry("700x500") 

    # A Label widget to show in toplevel 
    Label(racerWindow, text ="Enter new racer window").pack()
    Label(racerWindow, text="First Name").pack()
    Label(racerWindow, text="Last Name").pack()

    entry_1 = Entry(racerWindow)
    entry_1.pack()

For .grid()

def EnterRacer():
    # Toplevel object which will  
    # be treated as a new window 
    racerWindow = Toplevel() 
    racerWindow['background']='#2A3132'

    # sets the title of the 
    # Toplevel widget 
    racerWindow.title("Enter New Racer") 

    # sets the geometry of toplevel 
    racerWindow.geometry("700x500") 

    # A Label widget to show in toplevel 
    Label(racerWindow, text ="Enter new racer window").grid(row=0,column=0)
    Label(racerWindow, text="First Name").grid(row=0,column=1)
    Label(racerWindow, text="Last Name").grid(row=0,column=2)

    entry_1 = Entry(racerWindow)
    entry_1.grid(row=1,column=0,columnspan=3,sticky=E+W)

This is just one way of grid-ing things, you can use youre own way by changing the row and column arguments. Let me know if any doubts :D

Cheers

Upvotes: 1

Related Questions