Search code examples
pythontkintertkinter-layout

is it possible to override the size of a frame when something is inside?


I am trying to have two identically sized frames inside a grid, like this: enter image description here i have control over the frame's size when nothing is in it but when i add something into the frame, i lose control over the size itself and it adapts to the size of stuff placed in it. Any way to control the size of a frame when something is inside it? size of frame when something is inside: when nothing is in the frame

from tkinter import *
import random as rn

def value_change(choice):
    global user_choice
    user_choice=choice
    return(user_choice)

user_choice=4
hlavni=Tk()
jmeno=Label(hlavni,text='Kámen, nůžky,papír',font=("arial","20","bold"),justify='center')
jmeno.grid(row=0,column=0,columnspan=2)

hrac_name=Label(hlavni,text='Hráč',justify="left")
hrac_name.grid(row=1,column=0)
pc_name=Label(hlavni,text="Počítač")
pc_name.grid(row=1,column=1)




hrac_f=Frame(hlavni,relief="ridge",borderwidth=4,width=400,height=400,padx=10,pady=10)
hrac_f.grid(row=2,column=0,padx=10,pady=10)

""" stuff inside the frame
Volba=Label(hrac_f,text="Zadej svou volbu",font=("arial",10,"bold")).grid(row=0,column=0)
values=Label(hrac_f,text="Vyber: Kámen, Nůžky, Papír",font=("arial",10)).grid(row=1,column=0)

choice_f=Frame(hrac_f)
choice_f.grid(row=3,column=0)

stone=Button(choice_f,text="kámen",relief="ridge",command=lambda: value_change(0))
stone.grid(row=0,column=0)

scissors=Button(choice_f,text="nůžky",relief="ridge",command=lambda: value_change(1))
scissors.grid(row=0,column=1)

paper=Button(choice_f,text="papír",relief="ridge",command=lambda: value_change(2))
paper.grid(row=0,column=2) """


hlavni.mainloop()

Solution

  • By default, when adding widgets to a frame using .grid() or .pack(), the size of the frame will be adjusted to fit all the widgets.

    To change this default behavior, call .grid_propagate(0) or .pack_propagate(0) on the frame.

    For your case, as .grid() is used on those widgets inside hrac_f frame, then hrac_f.grid_propagate(0) should be called:

    ...
    hrac_f=Frame(hlavni,relief="ridge",borderwidth=4,width=400,height=400,padx=10,pady=10)
    hrac_f.grid(row=2,column=0,padx=10,pady=10)
    
    hrac_f.grid_propagate(0) # disable auto size adjustment when adding widgets using grid()
    
    """ stuff inside the frame """
    Volba=Label(hrac_f,text="Zadej svou volbu",font=("arial",10,"bold")).grid(row=0,column=0)
    values=Label(hrac_f,text="Vyber: Kámen, Nůžky, Papír",font=("arial",10)).grid(row=1,column=0)
    
    choice_f=Frame(hrac_f)
    choice_f.grid(row=3,column=0)
    
    stone=Button(choice_f,text="kámen",relief="ridge",command=lambda: value_change(0))
    stone.grid(row=0,column=0)
    
    scissors=Button(choice_f,text="nůžky",relief="ridge",command=lambda: value_change(1))
    scissors.grid(row=0,column=1)
    
    paper=Button(choice_f,text="papír",relief="ridge",command=lambda: value_change(2))
    paper.grid(row=0,column=2)
    
    hlavni.mainloop()
    

    Result:

    enter image description here