我不知道这为什么行不通。我一定错过了什么,因为我可以使一个单一的框架扩大,以适应画布,但我不能使一个框架内的框架扩大,以适应画布。你知道吗
我已经为此奋斗了好几个小时,我觉得这是显而易见的。你知道吗
我希望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()
允许你的
Label
成为sticky='ew'
使用.grid_columnconfigure(0, weight=1)
。你知道吗您需要
Canvas(..., window=
对象的iid
项。不需要参数
width=200, height=200
!你知道吗绑定到已知的
"<Configure>
。你知道吗调整
.frame_n_canvas
宽度与.main_canvas
宽度同步。你知道吗一个小部件,这里是框架
self.frame_n_canvas
,如果与.create_window(...
一起放在Canvas
中,就失去了expand
的功能。你知道吗这个
.grid(...
是无用的,因为您稍后将相同的Frame
与.create_window(...
放在一起:要给您的
NoteFrame
扩展位置,请使用参数width=200, height=200
。你知道吗要使您的
Label
成为sticky='ew'
,您必须执行.grid_columnconfigure(...
:结果:
![enter image description here](https://i.stack.imgur.com/GCI5s.png)
相关问题 更多 >
编程相关推荐