【发布时间】:2019-01-17 08:44:45
【问题描述】:
在 tkinter 中,我正在尝试制作一个包含小部件的可滚动画布,以便我的固定大小的窗口可以滚动以使用所有小部件。但是,当尝试通过从 Canvas 获取边界框大小来设置滚动条大小时,会返回 None。
通过将它们的父级分配为画布并在每个上调用grid(),我的小部件被添加到画布中。在创建和布置小部件后,我尝试获取边界框大小。
# Create vertical scrollbar
self.scrollbar = Scrollbar(self.master, orient = VERTICAL)
# Pack on the right side and fill on the Y-axis
self.scrollbar.pack(side = RIGHT, fill = Y)
# Create container canvas, set Y-axis scroll command to scrollbar value
self.mainsection = Canvas(self.master, bg = colors["lightgray"], yscrollcommand = self.scrollbar.set)
# Pack on the left side, center, fill and expand on both axes
self.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 scrollbar
self.mainsection.configure(scrollregion = self.mainsection.bbox(ALL))
# Prints "None"
print(self.mainsection.bbox(ALL))
print(self.mainsection.bbox(ALL)) 应该打印出一些关于画布边界框的信息;但是,它返回None。
【问题讨论】:
-
您的画布上什么也没有。子小部件不是内容,只有各种
.create_XXXX()方法生成对.bbox()可见的项目。 -
将
Label(self.mainsection,更改为myFrame = Frame(...) Label(myFrame,...) mainsection.create_window(0, 0, window=myFrame)
标签: python tkinter tkinter-canvas