简体   繁体   English

使用 grid_forget() 后帧大小不再显示 - Python tkinter

[英]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).但是,当您再次按下第一个按钮(在之前按下两个按钮之后)时,框架就会出现,就像从代码中删除了grid_propagate一样(框架与其中的 label 一样大)。 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.通过简单地使用grid_remove()而不是使用grid_forget()它正在工作。 But I still hope someone can answer my question because it really should work with grid_forget() as well I think.但我仍然希望有人能回答我的问题,因为我认为它确实应该与grid_forget()一起使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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