我不确定为什么这行不通。我必须缺少一些东西,因为我可以使单个框架扩展以适合画布,但是不能使框架内部扩展以适合画布。
我已经为此战斗了好几个小时,感觉好像很明显。
我希望NoteFrame
内的标签可以扩展以填充框架,而NoteFrame可以扩展以填充画布。
以下是再现行为代码的最小代码:
import tkinter as tk
class NoteFrame(tk.Frame):
def __init__(self, container):
super().__init__(container)
self.config(background='white')
for i in range(20):
tk.Label(self, text='test').grid(row=i, column=0, sticky='nsew')
class GCMain(tk.Tk):
def __init__(self):
super().__init__()
self.columnconfigure(0, weight=1)
self.rowconfigure(0, weight=1)
self.main_canvas = tk.Canvas(self, background="bisque")
self.main_canvas.columnconfigure(0, weight=1)
self.main_canvas.rowconfigure(0, weight=1)
self.frame_n_canvas = tk.Frame(self.main_canvas)
self.frame_n_canvas.grid(row=0, column=0, sticky='nsew') # tried with and without this
self.frame_n_canvas.columnconfigure(0, weight=1)
self.main_canvas.config(scrollregion=self.main_canvas.bbox("all"), highlightthickness=0)
scrollbar = tk.Scrollbar(self, orient='vertical')
scrollbar.grid(row=0, column=1, sticky='nsw')
scrollbar.config(command=self.main_canvas.yview)
self.main_canvas.config(yscrollcommand=scrollbar.set)
self.main_canvas.grid(row=0, column=0, sticky='nsew')
self.main_canvas.create_window(0, 0, window=self.frame_n_canvas, anchor='nw')
self.main_canvas.bind("<Configure>", self.update_scrollregion)
nf = NoteFrame(self.frame_n_canvas)
nf.grid(row=0, column=0, sticky='nsew') # tried with and without this
def on_resize(self, event):
w, h = event.width, event.height
self.main_canvas.configure(width=w, height=h)
self.frame_n_canvas(width=w, height=h) # tried with and without this
def update_scrollregion(self, event):
self.main_canvas.configure(scrollregion=self.main_canvas.bbox("all"))
if __name__ == '__main__':
GCMain().mainloop()
答案 0 :(得分:1)
评论:动态解决方案
允许您的Label
使用sticky='ew'
来.grid_columnconfigure(0, weight=1)
。
class NoteFrame(tk.Frame):
def __init__(self, container):
super().__init__(container)
self.grid_columnconfigure(0, weight=1)
您需要iid
对象的项目Canvas(..., window=
。
不需要参数width=200, height=200
!
self.main_canvas.fram_n_canvas_iid =\
self.main_canvas.create_window(0, 0, window=self.frame_n_canvas, anchor='nw')
绑定到"<Configure>
。
self.main_canvas.bind("<Configure>", self.canvas_configure)
调整.frame_n_canvas
宽度的大小与.main_canvas
宽度同步。
def canvas_configure(self, event):
canvas = event.widget
canvas.itemconfigure(canvas.fram_n_canvas_iid, width=canvas.winfo_width())
问题:画布中的框架不会扩展为适合画布
如果将self.frame_n_canvas
放在expand
中,则小部件(这里是框架.create_window(...
)失去了Canvas
的功能。
.grid(...
这没用,因为您稍后将与Frame
相同的.create_window(...
放置:
self.frame_n_canvas.grid(row=0, column=0, sticky='nsew') # tried with and without this
...
self.main_canvas.create_window(0, 0, window=self.frame_n_canvas, anchor='nw')
要给您的NoteFrame
扩展位置,请使用参数width=200, height=200
。
self.main_canvas.create_window(0, 0, window=self.frame_n_canvas, anchor='nw',
width=200, height=200)
要使您的Label
成为sticky='ew'
,您必须执行.grid_columnconfigure(...
:
class NoteFrame(tk.Frame):
def __init__(self, container):
super().__init__(container)
self.grid_columnconfigure(0, weight=1)