tkinter 中不同形状的窗口 [英] Different shapes of windows in tkinter

查看:75
本文介绍了tkinter 中不同形状的窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通常,当您使用 Tk() 在 tkinter 中创建窗口时,窗口的标准形状将是正方形.有什么办法可以让形状变成别的吗?

normally when you make a window in tkinter using Tk(), the standard shape of the window would be a square. Is there any way we can make the shape anything else?

推荐答案

我自己对 Python 中的自定义 GUI 也比较陌生.几个小时以来,我一直在互联网上搜索类似问题的答案.与 Tkinter 一样棒的是,在我看来,它的功能仅限于标准外观的矩形 GUI 对象.一方面,这极大地简化了 Python 中的编码.另一方面,应用程序通常可以从定制的 GUI 中受益.(想象一个由 Python 代码驱动的 RPG 游戏界面中使用的古代卷轴图像/形状的对话窗口.)在 Tk 中使用透明等技巧可能会奏效.但是,该透明窗口中的所有小部件也将变为透明.因此,正确处理会很棘手.但是,众所周知,更高级的编程语言确实允许您使用任意窗口、框架、按钮形状等,这些可能适用于上述此类游戏界面.

I'm also relatively new to custom GUIs in Python myself. I've been searching the Internet for an answer to a similar question for several hours now. As awesome as Tkinter is, it appears to me that its capabilities are restricted to standard-looking, rectangular GUI objects. On the one hand, this greatly simplifies coding in Python. On the other, an application could often benefit from a customized GUI. (Imagine a dialog window in the image/shape of an ancient scroll used in an RPG game interface driven by Python code.) Using tricks like transparency might work in Tk. But, all widgets in that transparent window will also become transparent. So, it would be tricky to get right. However, it is common knowledge that more advanced programming languages do allow you to utilize arbitrary window, frame, button shapes, etc., that might be appropriate for such a game interface as mentioned above.

然后我找到了 wxPython.https://wxpython.org/ 根据我目前所读到的内容,该库确实有更深入的访问权限到操作系统内置的更抽象的类,您可以从中构建任意形状或任何图像的框架或任何 GUI 对象.主要的缺点是您必须使用类似于 C++ 的 OOP 语法从头开始构建这些对象.这需要比 Tkinter 更陡峭的学习曲线.但是,我认为这是值得的,我很快就会开始学习过程.

Then I found wxPython. https://wxpython.org/ From what I've read about it so far, that library does have deeper access to the more abstract classes built-in to the OS from which you can build a frame, or any GUI object, of any arbitrary shape or from any image. The major downside is that you have to build these objects from scratch using OOP syntax similar to C++. That requires a steeper learning curve than what is needed for Tkinter. But, I think it's worth the effort and I will start that learning process shortly.

这是来自 Python Programming Studio 的示例.不幸的是,代码清单似乎没有在该页面上正确呈现;(至少在我的浏览器上.)但它确实表明可以使用 wxPython 实现自定义形状的窗口:

Here is an example from Python Programming Studio. Unfortunately, the code listing does not appear to be rendered properly on that page; (at least on my browser.) But it does show that it is possible to implement a custom-shaped window using wxPython:

https://www.pythonstudio.us/wxpython/how-do-i-make-a-nonrectangular-frame.html

幸运的是,这里有一个 wxPython 线程,其中包含更清晰的编码示例:

Fortunately, there is a wxPython thread here with much clearer coding examples:

最简单的方法是什么在 wxPython 中创建一个异形窗口?

wxPyWiki(和其他地方)上还有其他几个教程,它们至少可以让您了解 wxPython 可以做什么:

There are several other tutorials at the wxPyWiki, (and other places), that will at least give you an idea of what wxPython can do:

https://wiki.wxpython.org/

这篇关于tkinter 中不同形状的窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆