Search code examples
pythontkintertkinter-layout

Different shapes of windows in 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?


Solution

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

    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.

    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

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

    What is the simplest way to create a shaped window in 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/