Creating a Beautiful Python Desktop Application (customtkinter)

Why a Python desktop application?

If, like me, you decide to take a look at Python for the first time after several attempts to learn C++/C#, then most likely the first project will be a desktop application. Departing from the topic, I will say that the craving for the study of these languages ​​​​was hopelessly lost in view of the classical teaching in the spirit of “just to pass” and countless monotonous and monotonous lectures. As I said above, although at the initial level, I still touched on the development of applications for Windows and therefore I wanted to look at the principles of python first from here (and not jump into django and other powerful frameworks). I must warn you – this article does not provide code excerpts and is more of an expression of my emotions for this project.

What application to create?

I did not doubt this for a second and immediately chose – TIMER! I like to fall asleep to the sounds of films from various resources, but the fact that after they ended the computer worked for an extra 6 hours definitely did not suit me. Of course, I know about the existence of various kinds of applications with similar functionality (for example, SMtimer), but firstly, its UX / UI is just hello to zero, and secondly, I still wanted my own.

SMtimer itself

SMtimer itself

Tasks set – time to act

After watching one and a half guides from youtube, I got inspired and wrote the first prototype using the standard tkinter library. It looked very doubtful from the point of view of the same UX / UI, but it was already something

The first version of my long-suffering project

The first version of my long-suffering project

Here everyone can notice and say “but this is even worse than SMtimer” and they will be right, in general, such thoughts visited me and I decided to turn to the search for something that could make this an adequate application that I can use without a twinge of conscience.

CustomTkinter or how I tried to learn Zen

After a short search, I found it – CustomTkinter. He promised to literally make an application out of my prototype, which seemed to have been originally in Windows 10. You can see the exampls directly below:

Dark theme "example" by CustomTkinter

Dark theme “example” by CustomTkinter

Accordingly light theme "example" by CustomTkinter

Respectively light theme “example” from CustomTkinter

In addition to this, the library promised minimal code changes to transfer my “prototype” to this beauty, but I still had a long way to go in understanding how it works here…

Lots of problems and lack of understanding

This is how I would describe my path in the knowledge of this library. Literally trying to interact with it, I understood how Python data types are arranged (yes, it may seem ridiculous, but when you first meet NoneType you are a little surprised), I understood how to interact with class objects and actually divide the project code into functional blocks (which before that seemed to me something then strange). But in order to understand how sad everything was in my knowledge – here is the reference that I washed down out of hopelessness (at that time I wanted to get at least this):

As you can see, even the color is dull filled in Paint

As you can see, even the color is dull filled in Paint

The problem was a whole mountain – I absolutely could not figure out how to communicate with CusomTkinter – the creation and positioning of its objects, even now they seem quite logical and understandable, but then they were akin to Chinese characters for me. After several evenings spent smoking manuals, I was able to assemble the 3rd version (its first reincarnation):

Albeit working on my knees, but a WORKING version

Albeit working on my knees, but a WORKING version

Having received this, I was already glad that I did not give up and started fixing the location of all objects on the screen. However, it should be mentioned about the “little problem” – there is no SpinBox in CustomTkinter, which means that you had to enter data either from the keyboard, or look for how to make this same SpinBox yourself. The choice fell on the second option, on a site dedicated to ctk itself, the author made a FloatSpinBox, having finalized it, I could already get what I wanted.

Actually FloatSpinBox

Actually FloatSpinBox

Its main jamb was the lack of reaction to the mouse wheel – for me it was critical – pressing 32 times to set the minutes did not appeal to me and I decided to finish it myself:

  1. Added min and max value – now I could use this class for both minutes and hours (at first I just spammed several classes where I manually set limits for each of them)

  2. The mousewheel response now spanned the entire SpinBox, which was great. If you just hang the handler on self.bind(“MouseWheel”, self.on_mouse_wheel)” then the operation will only be on the entry borders and button elements, which is just awful.

  3. And of course, I implemented scrolling in the opposite direction. It’s so strange to do something that is usually already implemented for you.

    Well, I played a little with the themes of the customtkinter itself:

The birth of the first adequate prototype

The birth of the first adequate prototype

Topics I found on github

Topics I found on github

Finally what I wanted to get

Now I decided to draw the output of the time before triggering. I worked on different versions of this timer and came to the conclusion that I wrote down the TimeBox. The main difference is that there are no buttons. The conclusion, of course, became much more fun.

Pre-release version of my timer

Pre-release version of my timer

After I had seen enough of custom themes from github, I decided that I definitely didn’t need such horror and returned to the origins – the standard green theme. Having defeated the cyclops effect (even 3x), I added theme switching and corrected the application logic – reboot. shutdown, sleep and blocking work with a bang and two types of timer conditions – after a period of time and At a specific time. I was completely satisfied and literally happy that now I don’t have to go to the task scheduler every day and set the shutdown time manually! But then I saw something that would later transfer me from Notepad ++ (yes, yes, that’s where I wrote my project up to this point). It was the pywinstyles library, which promised transparency effects like from win7 but in the style of a dozen. Exactly what is needed.

Sample Customtkinter but with Pywinstyles

Sample Customtkinter but with Pywinstyles

It seemed to me just the perfect addition to my program. You run over the film and do not interfere and you know how much time is left – beauty. But the complete lack of documentation and intelligible samples forced us to reconsider the prospects of the project. I wrote to the author of pywinstyles and with his description, I was able to get it all to work together:

Final versions with pywinstyles

Final versions with pywinstyles

If there are other weird people here who decide to touch pywinstyles, be prepared for a lot of problems. For example, after changing the theme to a transparent one, there is no way to return it back. Only restart. The main_frame from ctk has a whitish background, which makes the program “shine” on light backgrounds.

conclusions

I got what I wanted and a bunch of needed experience on top. In the future I want to implement an automatic stop of the timer. If the movie is paused, then the timer itself stops. I don’t know how to implement this when watching a movie from a browser, but if you have ideas, I’ll be happy to read it.

You can touch the version without pywinstyles here

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *