如何为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=300ensuresframe1can't be dragged wider than 300 pixels. Adjust this number to fit your needs.minsize=100stopsframe1from being shrunk smaller than 100 pixels (remove this if you don't need a minimum size).- If your
PanedWindowuses vertical orientation (orient=tk.VERTICAL), these values will control the height of the frame instead of width.
内容的提问来源于stack exchange,提问作者Jothin kumar
相关产品推荐
相关产品推荐

