当我在调整根窗口大小之前“忘记”或“添加”窗口小部件到 PanedWindow 窗口小部件时,此操作会更改根窗口大小。但是,在使用鼠标指针调整根窗口大小后,“忘记”或“添加”不会再更改窗口大小,只有 PanedWindow 的“子窗口”会更改其大小。
在任何根窗口调整大小之前,如何才能在这些操作期间保持根窗口大小稳定?
这是我的示例代码:
import tkinter as tk
from tkinter import ttk
state = "shown"
def hide_show(paned, canvas):
global state
if state=="shown":
paned.forget(canvas)
state = "hidden"
else:
paned_window.add(canvas, weight= 1)
state = "shown"
root = tk.Tk()
paned_window = ttk.PanedWindow(root, orient=tk.HORIZONTAL,)
button = ttk.Button (root, text="hide/show", command=lambda: hide_show(paned_window, canvas2))
paned_window.grid(row=0, sticky=(tk.W, tk.E, tk.S, tk.N))
button.grid (row=1, sticky=tk.W)
root.rowconfigure (0, weight=1)
root.rowconfigure (1, weight=0)
root.columnconfigure(0, weight=1)
canvas1 = tk.Canvas(paned_window, height=100, width=100, bg="red")
canvas1.grid()
paned_window.add(canvas1, weight= 1)
canvas2 = tk.Canvas(paned_window, height=100, width=100, bg="green")
canvas2.grid()
paned_window.add(canvas2, weight= 1)
root.mainloop()