在 tkinter 中,我正在尝试制作一个包含小部件的可滚动画布,以便我的固定大小的窗口可以滚动以使用所有小部件。但是,当尝试通过从 Canvas 获取边界框大小来设置滚动条大小时,None会返回。通过将它们的父级分配为画布并调用grid()每个小部件,我的小部件被添加到画布中。在创建和布置小部件后,我尝试获取边界框大小。# Create vertical scrollbarself.scrollbar = Scrollbar(self.master, orient = VERTICAL)# Pack on the right side and fill on the Y-axisself.scrollbar.pack(side = RIGHT, fill = Y)# Create container canvas, set Y-axis scroll command to scrollbar valueself.mainsection = Canvas(self.master, bg = colors["lightgray"], yscrollcommand = self.scrollbar.set)# Pack on the left side, center, fill and expand on both axesself.mainsection.pack(side = LEFT, anchor = CENTER, fill = BOTH, expand = True)# Configure the scrollbar to scroll the canvas.self.scrollbar.config(command = self.mainsection.yview)# Widget definitions go here.self.printsectionlabel = Label(self.mainsection, text = "Print Bills")self.printsectionlabel.grid(row = 0)# More widget definitions here...# Run after all widget definitions# Creates disabled scrollbarself.mainsection.configure(scrollregion = self.mainsection.bbox(ALL))# Prints "None"print(self.mainsection.bbox(ALL))print(self.mainsection.bbox(ALL))应该打印出有关画布边界框的某种信息;但是,它返回None.
1 回答

茅侃侃
TA贡献1842条经验 获得超21个赞
该bbox
方法将仅返回画布项目的边界框。如果您使用 向画布添加标签grid
,则它不是画布项目。您必须使用其中一种方法(create_line
、create_window
等)将对象添加到画布。
(请注意,bbox
将返回它会显示 (0, 0, 0, 0) 直到添加到画布的任何内容在屏幕上实际可见。您需要在调用 update 或等待诸如<Configure>
事件之类的东西后重置滚动区域。 )
添加回答
举报
0/150
提交
取消