画布中的框架不会扩展为适合画布 [英] Frame in canvas will not expand to fit canvas
问题描述
我不确定为什么这行不通.我必须缺少一些东西,因为我可以使单个框架扩展以适合画布,但是不能在框架内部扩展以适合画布.
I am not sure why this will not work. I must be missing something because I can make a single frame expand to fit the canvas but I cannot make a frame inside a frame expand to fit the canvas.
我已经为此战斗了好几个小时,我觉得它很明显.
I have been fighting with this for hours and I feel like its something obvious.
我希望 NoteFrame
内部的标签可以扩展以填充框架,而NoteFrame可以扩展以填充画布.
I am expecting the label inside of the NoteFrame
to expand to fill the frame and that NoteFrame expand to fill the canvas.
以下是再现行为代码的最小代码:
Here is the minimum code to reproduce the behavior code:
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()
推荐答案
评论:动态解决方案
-
使用
.grid_columnconfigure(0,weight = 1)
允许您的Label
为sticky ='ew'
.>
Allow your
Label
to besticky='ew'
use.grid_columnconfigure(0, weight=1)
.
class NoteFrame(tk.Frame):
def __init__(self, container):
super().__init__(container)
self.grid_columnconfigure(0, weight=1)
您需要 Canvas(...,window =
对象的 iid
项目.
否参数 width = 200,height = 200
是必需的!
You need the item iid
of the Canvas(..., window=
object.
No parameter width=200, height=200
are needed!
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
width 保持同步.
Resize .frame_n_canvas
width in sync with .main_canvas
width.
def canvas_configure(self, event):
canvas = event.widget
canvas.itemconfigure(canvas.fram_n_canvas_iid, width=canvas.winfo_width())
问题:画布中的框架不会扩展为适合画布
Question: Frame in canvas will not expand to fit canvas
小部件,即您的Frame self.frame_n_canvas
,如果放置在 .create_window(...
画布
.
A widget, here your Frame self.frame_n_canvas
, looses the ability to expand
, if placed with .create_window(...
in a Canvas
.
这个 .grid(...
)是没有用的,因为稍后您将相同的 Frame
与 .create_window(...
:
This, .grid(...
, is useless, as you later place the same Frame
with .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
.
To give your NoteFrame
place to expand, use parameter 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(...
:
To allow your Label
to be sticky='ew'
, you have to do .grid_columnconfigure(...
:
class NoteFrame(tk.Frame):
def __init__(self, container):
super().__init__(container)
self.grid_columnconfigure(0, weight=1)
结果:
这篇关于画布中的框架不会扩展为适合画布的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!