如何使用 Tkinter 包几何管理器重叠小部件?
我想在窗口中放置一个带有图像的 Canvas
,然后我想在其上打包 小部件,以便 Canvas 充当背景。
pack
管理器是否可以有两种状态:一种用于一组小部件,另一种用于另一组?
I want to put a Canvas
with an image in my window, and then I want to pack widgets on top of it, so the Canvas acts as a background.
Is it possible to have two states for the pack
manager: one for one set of widgets and another for another set?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
你的具体问题的答案是否定的。 您不能有两种状态,或者以其他方式在同一父级中使用两种不同的方式打包。
然而,我认为你想要完成的事情很简单。 使用画布的内置功能创建作为画布一部分的图像项,然后将内容打包到画布中,就像它是框架一样。
您可以通过创建带有图像的标签小部件来完成类似的事情,然后将其他小部件打包到标签中。
使用画布的一个优点是您可以轻松地平铺图像以使用重复的背景图像填充整个画布,这样随着窗口的增长,图像将继续填充窗口(当然您可以只使用足够大的原始图像.. .)
The answer to your specific question is no. You can't have two states or otherwise use pack two different ways in the same parent.
However, what I think you want to accomplish is simple. Use the built-in features of the canvas to create an image item that is part of the canvas, then pack things into the canvas as if it were a frame.
You can accomplish a similar thing by creating a label widget with an image, then pack your other widgets into the label.
One advantage to using a canvas is you can easily tile an image to fill the whole canvas with a repeating background image so as the window grows the image will continue to fill the window (of course you can just use a sufficiently large original image...)
如果可以接受加载附加扩展,请查看 Tkzinc。 从网站上看,
我目前正在 tcl 项目中使用它,对结果非常满意。 提供 tcl、perl 和 python 扩展。
If it is acceptable to load an additional extension, take a look at Tkzinc. From the web site,
I'm currently using it on a tcl project and am quite pleased with the results. Extensions for tcl, perl, and python are available.
我相信布莱恩的答案可能是最好的通用解决方案。 但是,您可能还想查看地点几何管理器。 地点几何管理器允许您指定小部件的确切大小和位置...这可能很快就会变得乏味,但可以完成工作。
I believe that Bryan's answer is probably the best general solution. However, you may also want to look at the place geometry manager. The place geometry manager lets you specify the exact size and position of the widget... which can get tedious quickly, but will get the job done.
不是没有交换部件树进出,我认为这不能用 Tk 干净地完成。 其他工具包可以更优雅地做到这一点。
Not without swapping widget trees in and out, which I don't think can be done cleanly with Tk. Other toolkits can do this a little more elegantly.