简体   繁体   中英

Frame-size does not show up anymore after using grid_forget() - Python tkinter

I was testing a method to hide and show frames by pressing a button (the buttons will be a menu bar later). So I created this code:

import tkinter as tk
from tkinter import *


win = Tk()

win.geometry("275x350")
win.title("MyProgram")
win.configure(background="white")

frame1 = Frame(win, width=200, height=200)
frame1.grid_propagate(False)
frame2 = Frame(win, width=200, height=200)
frame2.grid_propagate(False)
Label(frame1, text="Hello world", font="20").grid()
Label(frame2, text="Zweiter frame", font="20").grid()

def runGrid(run, destroy):
    run.grid(row=1, column=2)
    destroy.grid_forget()


def run2nd(run, destroy):
    run.grid(row=1, column=2)
    destroy.grid_forget()

Button(win, text="Run", command=lambda:runGrid(frame1, frame2)).grid(row=0, column=0)
Button(win, text="Run 2nd", command=lambda:run2nd(frame2, frame1)).grid(row=0, column=1)


win.mainloop()

So here is the problem... After pressing the first button, the first frame comes up in his wanted size. After pressing the 2nd button, the 2nd frame comes up in his wanted size. But when you press the first button again (after pressing both buttons once before) then the frame is just showing up like grid_propagate was removed from the code (the frame is just as big as the label in it). Can someone explain me the fault in my code which is causing the problem?

Thanks a lot;)

I found an other way to get what I want:

By simply using grid_remove() instead of using grid_forget() it is working. But I still hope someone can answer my question because it really should work with grid_forget() as well I think.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM