Title: Make a shaped analog clock in tkinter and Python
This is actually pretty easy if you know the tricks.
First, set the parts of the window that you want to be transparent to some color that you're not using. In this example, I set the Canvas widget's background color to pink.
Also set the Canvas widget's highlightthickness to 0. If you don't do this, the program displays a strange border.
self.canvas = tk.Canvas(self.window, width=CLOCK_WID, height=CLOCK_HGT,
bg='pink', highlightthickness=0)
Second, set the window's transparent color to that background color.
self.window.wm_attributes('-transparentcolor', 'pink')
Finally, set the window's overrideredirect value to True and give the window zero-width border and highlight thickness.
self.window.overrideredirect(True)
self.window.configure(borderwidth=0, highlightthickness=0)
Now when you run the program, only the non-pink areas show up.
Conclusion
This technique lets you make a shaped window, at least in Windows 11. I don't know if it works without modification in macOS and Linux.
Unfortunately, because you've removed the window's decorations, there's no way for the user to move it. It also doesn't appear in the taskbar. You can still close it by pressing Alt_F4, but first you need to find it. One way I've found to do that is to minimize all windows and then restore one window. The clock also un-minimizes at that point. If the window you restored covers the clock, minimize it and then you should be able to find the clock.
In my next post, I'll show how you can let the user move the clock. Until then, download the example to experiment with it and to see additional details.
|