Update a width and height frame in tkinter

Viewed 23

I am trying to make a tkinter interface with frames that are added and deleted on the same window. But when a frame is larger than the other it enlarges the window and does not become small again for the other frame. What bothers me for aesthetics, is there a way to reduce it?

When I return to frame 1, I have the impression that the line ((frame1.configure(width = 150, height =150)) is not taken into account

Thx for help

import tkinter as tk

def clear_windgets(frame):
    for widget in frame.winfo_children():
        widget.destroy()

def load_frame1():
    #clear other windget
    clear_windgets(frame2)
    
    #configure windget
    frame1.configure(width = 150, height =150)
    frame1.update
    #frame1.pack_propagate(0) have try but don"t works
    frame1.tkraise()

    tk.Label(frame1, text = "Frame1", bg = "#358c77", fg = "white", font = ("TkMenuFont", 14) ).pack()

    #button widget
    switch_button = tk.Button(frame1, text = "go frame 2", font = ("TkHeadingFont", 20), bg = "#358c77", fg = "white",cursor = "hand2", activebackground = "#badee2", activeforeground = "black", command = lambda:load_frame2())
    switch_button.pack(pady = 20)

    #width, height for frame and root
    print(f"--" * 20)
    root.update()
    width = root.winfo_width()
    height = root.winfo_height()
    print("LargeurROOT : ", width)
    print("HauteurROOT: ", height)

    width_frame1 = frame1.winfo_width()
    height_frame1 = frame1.winfo_height()
    print("Largeurframe1 : ", width_frame1)
    print("Hauteurframe1: ", height_frame1)

def load_frame2():
    #clear other windget
    clear_windgets(frame1)

    
    #configure windget
    frame2.tkraise()

    tk.Label(frame2, text = "Frame2", bg = "#358c77", fg = "white", font = ("TkMenuFont", 14), pady = 10 ).pack()
    tk.Label(frame2, text = "Frame2", bg = "#358c77", fg = "white", font = ("TkMenuFont", 14), pady = 10 ).pack()
    tk.Label(frame2, text = "Frame2", bg = "#358c77", fg = "white", font = ("TkMenuFont", 14), pady = 10 ).pack()
    tk.Label(frame2, text = "Frame2", bg = "#358c77", fg = "white", font = ("TkMenuFont", 14), pady = 10 ).pack()

    #button widget
    switch_button = tk.Button(frame2, text = "Back to frame 1", font = ("TkHeadingFont", 20), bg = "#358c77", fg = "white",cursor = "hand2", activebackground = "#badee2", activeforeground = "black", command = lambda:load_frame1())
    switch_button.pack(pady = 20)


    #width, height for frame and root
    print(f"--" * 20)
    root.update()
    width = root.winfo_width()
    height = root.winfo_height()
    print("LargeurROOT : ", width)
    print("HauteurROOT: ", height)

    width_frame2 = frame2.winfo_width()
    height_frame2 = frame2.winfo_height()
    print("Largeurframe2 : ", width_frame2)
    print("Hauteurframe2: ", height_frame2)
    

#initiation app
root = tk.Tk()
root.eval("tk::PlaceWindow . center")


#create a frame widget
frame1 = tk.Frame(root, bg = "#358c77")
frame2 = tk.Frame(root, bg = "#358c77")
frame3 = tk.Frame(root, bg = "#358c77")

for frame in (frame1, frame2, frame3):
    frame.grid(row = 0, column = 0, sticky = "nesw")

#run app
load_frame1()
root.mainloop()
0 Answers
Related