当PanedWindow忘记小部件时防止窗口调整大小

问题描述 投票:0回答:1

当我在调整根窗口大小之前“忘记”或“添加”一个小部件到 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()
python tkinter
1个回答
0
投票

我认为将

root.geometry()
设置为固定值会有所帮助,因为它将创建一个固定尺寸的窗口,防止不必要的调整大小。

© www.soinside.com 2019 - 2024. All rights reserved.