Home > Enterprise >  Why does removing grid() make my program turn blank?
Why does removing grid() make my program turn blank?

Time:11-27

I'm using .place() in my tkinter program, so I wanted to remove references to grid(). So far my program works but for some reason there's a single .grid() line that makes my whole program turn blank if it's removed. This shouldn't happen, since I'm entirely using .place(). Here is that line:

    AllFrames.grid(row=0, column=0, sticky='nsew')

And here is my full code:

from tkinter import *
root = Tk()
root.title("Account Signup")
DarkBlue = "#2460A7"
LightBlue = "#B3C7D6"
root.geometry('350x230')

Menu = Frame()
loginPage = Frame()
registerPage = Frame()
for AllFrames in (Menu, loginPage, registerPage):
    AllFrames.grid(row=0, column=0, sticky='nsew')
    AllFrames.configure(bg=LightBlue)

def show_frame(frame):
    frame.tkraise()
root.grid_rowconfigure(0, weight=1)
root.grid_columnconfigure(0, weight=1)
show_frame(Menu)

# ============= Menu Page =========

menuTitle = Label(Menu, text="Menu", font=("Arial", 25), bg=LightBlue)
menuTitle.place(x=130, y=25)

loginButton1 = Button(Menu, width=25, text="Login", command=lambda: show_frame(loginPage))
loginButton1.place(x=85, y=85)

registerButton1 = Button(Menu, width=25, text="Register", command=lambda: show_frame(registerPage))
registerButton1.place(x=85, y=115)

# ======== Login Page ===========

loginUsernameL = Label(loginPage, text='Username').place(x=30, y=60)
loginUsernameE = Entry(loginPage).place(x=120, y=60)
loginPasswordL = Label(loginPage, text='Password').place(x=30, y=90)
loginPasswordE = Entry(loginPage).place(x=120, y=90)
backButton = Button(loginPage, text='Back', command=lambda: show_frame(Menu)).place(x=0, y=0)
loginButton = Button(loginPage, text='Login', width=20).place(x=100, y=150)

# ======== Register Page ===========


root.mainloop()

I've also noticed that changing anything in the parentheses causes the same result. For example, if I change sticky='nsew' to sticky='n' or row=0 to row=1 it will show a blank page. How do I remove .grid() from my program without it turning blank?

CodePudding user response:

The place() manager does not reserve any space, unless you tell it directly.

The grid(sticky='nsew') makes the widget expand to fill the entire available space, in this case the containing widget. The widgets inside all use place() which will not take any space. When you change to grid(sticky='n') you place the zero size widget at the top of the containing widget.

But, for your current problem you can assign a size to the widgets:

AllFrames.place(relwidth=1, relheight=1) # w/h relative to size of master

I would recommend using the grid() geometry manager if you are going to make any more complicated layouts.

For more info have a look at effbot on archive.org

  • Related