尽管内部存在小部件,画布 bbox 方法返回 None

编程入门 行业动态 更新时间:2024-10-26 17:20:58
本文介绍了尽管内部存在小部件,画布 bbox 方法返回 None的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

在 tkinter 中,我正在尝试制作一个包含小部件的可滚动画布,以便我的固定大小窗口可以滚动以使用所有小部件.但是,当尝试通过从 Canvas 获取边界框大小来设置滚动条大小时,会返回 None.

In tkinter, I'm trying to make a scrollable canvas that contains widgets so my fixed-size window can scroll to use all the widgets. However, when attempting to set the scrollbar size by getting the bounding box size from the Canvas, None is returned.

我的小部件通过将它们的父级指定为画布并在每个小部件上调用 grid() 来添加到画布中.在创建和布置小部件后,我尝试获取边界框大小.

My widgets are being added to the canvas by assigning their parent as the canvas and calling grid() on each. I try to get the bounding box size after creating and laying out the widgets.

# 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.

print(self.mainsection.bbox(ALL)) should print out some sort of information about the bounding box of the canvas; however, it returns None.

推荐答案

bbox 方法将仅返回画布项目的边界框.如果您使用 grid 向画布添加标签,则它不是画布项目.您必须使用其中一种方法(create_linecreate_window 等)将对象添加到画布.

The bbox method will return a bounding box only for canvas items. If you add a label to the canvas with grid, it's not a canvas items. You must use one of the methods (create_line, create_window, etc) to add an object to the canvas.

(注意 bbox 将返回它会显示 (0, 0, 0, 0) 直到添加到画布的任何内容在屏幕上实际可见.您需要在之后重置滚动区域调用更新,或等待类似 事件.)

(Note that bbox will return it will show (0, 0, 0, 0) until anything added to the canvas is actually visible on the screen. You need to reset the scroll region either after calling update, or waiting for something like a <Configure> event.)

这篇关于尽管内部存在小部件,画布 bbox 方法返回 None的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-04-30 06:16:36,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1390571.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:画布   部件   方法   bbox

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!