如何使用 Tkinter 包几何管理器重叠小部件?

发布于 2024-07-06 00:11:03 字数 142 浏览 4 评论 0原文

我想在窗口中放置一个带有图像的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

窝囊感情。 2024-07-13 00:11:03

你的具体问题的答案是否定的。 您不能有两种状态,或者以其他方式在同一父级中使用两种不同的方式打包。

然而,我认为你想要完成的事情很简单。 使用画布的内置功能创建作为画布一部分的图像项,然后将内容打包到画布中,就像它是框架一样。

您可以通过创建带有图像的标签小部件来完成类似的事情,然后将其他小部件打包到标签中。

使用画布的一个优点是您可以轻松地平铺图像以使用重复的背景图像填充整个画布,这样随着窗口的增长,图像将继续填充窗口(当然您可以只使用足够大的原始图像.. .)

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

穿透光 2024-07-13 00:11:03

...结果是行不通的,因为我想向其中添加标签和更多画布,但我找不到任何方法使它们的背景透明

如果可以接受加载附加扩展,请查看 Tkzinc。 从网站上看,

Tkzinc(历史上称为 Zinc)小部件与 Tk Canvas 非常相似,因为它们都支持结构化图形。 与 Canvas 一样,Tkzinc 实现了用于显示图形实体的项目。 可以操纵这些项目,并且可以将绑定与它们关联以实现交互行为。 但与 Canvas 不同的是,Tkzinc 可以在层次结构中构建项目,支持缩放和旋转,可以为项目层次结构的子树设置剪裁,支持多轮廓曲线。 它还借助 OpenGL 提供高级渲染,例如颜色渐变、抗锯齿、透明度和三角形项目。

我目前正在 tcl 项目中使用它,对结果非常满意。 提供 tcl、perl 和 python 扩展。

... turned out to be unworkable because I wanted to add labels and more canvases to it, but I can't find any way to make their backgrounds transparent

If it is acceptable to load an additional extension, take a look at Tkzinc. From the web site,

Tkzinc (historically called Zinc) widget is very similar to the Tk Canvas in that they both support structured graphics. Like the Canvas, Tkzinc implements items used to display graphical entities. Those items can be manipulated and bindings can be associated with them to implement interaction behaviors. But unlike the Canvas, Tkzinc can structure the items in a hierarchy, has support for scaling and rotation, clipping can be set for sub-trees of the item hierarchy, supports muti-contour curves. It also provides advanced rendering with the help of OpenGL, such as color gradient, antialiasing, transparencies and a triangles item.

I'm currently using it on a tcl project and am quite pleased with the results. Extensions for tcl, perl, and python are available.

浅浅 2024-07-13 00:11:03

我相信布莱恩的答案可能是最好的通用解决方案。 但是,您可能还想查看地点几何管理器。 地点几何管理器允许您指定小部件的确切大小和位置...这可能很快就会变得乏味,但可以完成工作。

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.

苹果你个爱泡泡 2024-07-13 00:11:03

不是没有交换部件树进出,我认为这不能用 Tk 干净地完成。 其他工具包可以更优雅地做到这一点。

  • COM/VB/MFC 可以使用 ActiveX 控件来完成此操作 - 您可以在同一区域中隐藏/显示多个 ActiveX 控件。 任何容器都可以让你通过改变孩子的位置来做到这一点。 如果您正在编写特定于 Windows 的程序,您也许可以通过这种方式完成它。
  • QT 也将允许您以类似的方式执行此操作。
  • GTK 稍微难一些。

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.

  • COM/VB/MFC can do this with an ActiveX control - you can hide/show multiple ActiveX controls in the same region. Any of the containers will let you do this by changing the child around. If you're doing a windows-specific program you may be able to accomplish it this way.
  • QT will also let you do this in a similar manner.
  • GTK is slightly harder.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文