如何使用Tkinter Pack几何图形管理器重叠小部件?

时间:2020-03-06 14:31:00  来源:igfitidea点击:

我想在窗口中放置一个带有图像的Canvas,然后在其顶部打包小部件,因此Canvas可以用作背景。

" pack"管理器可能有两种状态:一种状态用于一组小部件,另一种状态用于另一组小部件?

解决方案

特定问题的答案是否定的。我们不能具有两种状态,否则不能在同一父级中以两种不同的方式使用包。

但是,我认为我们想要完成的事情很简单。使用画布的内置功能创建一个图像项目,该图像项目是画布的一部分,然后将东西包装到画布中,就好像它是框架一样。

我们可以通过创建带有图像的标签小部件,然后将其他小部件包装到标签中来完成类似的操作。

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

我相信Bryan的答案可能是最好的一般解决方案。但是,我们可能还需要查看位置几何图形管理器。位置几何图形管理器使我们可以指定小部件的确切大小和位置……这可能很快就很繁琐,但可以完成工作。

并非没有交换窗口小部件树的情况,我认为使用Tk不能做到这一点。其他工具箱可以更优雅地完成此操作。

  • COM / VB / MFC可以使用ActiveX控件执行此操作-我们可以在同一区域中隐藏/显示多个ActiveX控件。任何一个容器都可以通过改变孩子的位置来做到这一点。如果我们正在执行Windows特定程序,则可以通过这种方式完成。
  • QT还可以让我们以类似的方式执行此操作。
  • GTK有点难。

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

如果可以加载其他扩展名,请查看Tkzinc。在网站上,

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.

我目前在tcl项目上使用它,并对结果感到非常满意。提供了tcl,perl和python的扩展。