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)
my_paned_window.add(frame2)
my_paned_window.pack(fill=tk.BOTH)
root.mainloop()
在上面的代碼中,我不希望frame1在拖動時擴展得太多。我怎樣才能為此設定一個限制?
uj5u.com熱心網友回復:
沒有直接的方法可以做到這一點。你可以通過為frame2設定minsize來實作這個目標。
像這樣:
...
my_paned_window.add(frame1)
my_paned_window.add(frame2, minsize=550)
...
另一種方法是,當窗扇的位置大于最大寬度時,回傳 "break",這樣它就不能再被移動。
最小的例子:
import tkinter as tk
class PanedWindow(tk.PanedWindow)。
def __init__(self, *args, **kwargs)。
super(PanedWindow, self).__init__(*args, **kwargs)
self.max_width = {}。
self.bind("<B1-Motion>"/span>, self.check_width)
self.bind("<ButtonRelease-1>", self.set_width)
def add(self, child, max_width=None, *args) 。
super(PanedWindow, self).add(child, *args)
self.max_width[child] = max_width
def check_width(self, event)。
for widget, width in self.max_width.items() 。
if width and widget.winfo_width() >= width:
自身.paneconfig(widget, width=width)
return "break"
def set_width(self, event)。
for widget, width in self.max_width.items() 。
if width and widget.winfo_width() >= width:
self.paneconfig(widget, width=width-1)
root = tk.Tk()
my_paned_window = PanedWindow(master=root, sashwidth=5)
max_width = 500'red' )
frame2 = tk.Frame(master=my_paned_window, bg='blue')
frame3 = tk.Frame(master=my_paned_window, bg="green")
tk.Label(master=frame1, text='frame1' ).pack()
tk.Label(master=frame2, text='frame2').pack()
my_paned_window.add(frame1, max_width=500)
my_paned_window.add(frame2)
my_paned_window.pack(fill=tk.BOTH, expand=True)
root.mainloop()
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/330592.html
標籤:
上一篇:如何在Tkinter中用回呼函式改變window.mainloop之外的標簽文本?
下一篇:如何獲得小部件的尺寸?
