You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何为Tkinter PanedWindow设置拖拽最大限制?

Limit Drag Expansion for Frame in Tkinter PanedWindow

Got it! To stop frame1 from being dragged to an overly large size (and even set a minimum size if you want), you can use the paneconfigure() method built into the PanedWindow widget. This lets you define hard size limits for individual panes.

Here's how to modify your code:

First, after adding frame1 to the paned window, call paneconfigure() to set its maximum (and optionally minimum) width:

import tkinter as tk
root = tk.Tk()
my_paned_window = tk.PanedWindow(master=root)
frame1 = tk.Frame(master=my_paned_window, bg='snow')
frame2 = tk.Frame(master=my_paned_window)
tk.Label(master=frame1, text='frame1').pack()
tk.Label(master=frame2, text='frame2').pack()

my_paned_window.add(frame1)
# Set drag limits for frame1: min width 100, max width 300
my_paned_window.paneconfigure(frame1, minsize=100, maxsize=300)
my_paned_window.add(frame2)

my_paned_window.pack(fill=tk.BOTH, expand=True)  # Added expand=True for better window resizing
root.mainloop()

Key details:

  • maxsize=300 ensures frame1 can't be dragged wider than 300 pixels. Adjust this number to fit your needs.
  • minsize=100 stops frame1 from being shrunk smaller than 100 pixels (remove this if you don't need a minimum size).
  • If your PanedWindow uses vertical orientation (orient=tk.VERTICAL), these values will control the height of the frame instead of width.

内容的提问来源于stack exchange,提问作者Jothin kumar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.30 12:54:11