user6729368
user6729368

Reputation:

how to set fixed frame sizes in python tkinter

say i have a tkinter window with two separate frames that i want to fill with info, my only problem is that the frame size sets itself dynamcally. my windows size is 500 by 500 and i want the top frame to go from 0,0 to 250,250. and the bottom from 0,250 to 500,500. I think you might get the idea at this point

here is my code so far:

def main_game(username,password,new_u,old_u):
    Tk_window_center(500,500)
    root.resizable(width=False, height=False)
    ##
    if new_u =="":
        pass
    else:
        new_u.pack_forget()
    if old_u =="":
        pass
    else:
        old_u.pack_forget()
    ##
    output_window_frame=Frame(root, bg="black")
    user_terminal_frame=Frame(root)
    ##
    usr_output=Label(output_window_frame)
    user_inp=Entry(user_terminal_frame)
    user_term=Label(user_terminal_frame)
    ##
    output_window_frame.grid(row=0,column=0,rowspan=3,columnspan=3, sticky="NSEW")

    user_terminal_frame.grid(row=4,column=0,rowspan=3,columnspan=3, sticky="NSEW")
    user_inp.pack(side="top", fill="x")
    user_term.pack(side="bottom", fill="x") 

p.s. new_u and old_u are previous frames that activate on startup

Upvotes: 0

Views: 8754

Answers (1)

Bryan Oakley
Bryan Oakley

Reputation: 386315

Since you are using grid, you need to give each row the same non-zero weight so that each row expands to fill half of its parent. You are putting one window in row 4 which is OK, but since you only have two items in the window it's misleading so I recommend using rows 0 and 1.

root.grid_rowconfigure(0, weight=1)
root.grid_rowconfigure(4, weight=1)

output_window_frame.grid(row=0,column=0,rowspan=3,columnspan=3, sticky="NSEW")

user_terminal_frame.grid(row=1,column=0,rowspan=3,columnspan=3, sticky="NSEW")

Of course, if you put other things in in rows 1, 2, or 3 then your frames won't each be 250 pixels tall.

There are other ways to accomplish the same thing. You could use pack, and set the pack options fill to "both" and expand to True. Or, you could use place and put the top window at the x/y coordinate of 0,0 with a relative width of 1.0 and a relative height of .5, and then put the other at 250,250 again with a relative width of 1.0 and a relative height of .5

Upvotes: 0

Related Questions