Using Tkinter has actually been a good way into implementing a GUI within the KaiPass app. Though it does have its own limitations, for a noob like me, it’s great!

ʕっ•ᴥ•ʔっ❤︎

I thought this post was kind of funny, but it’s also true. Even though Tkinter is simple, it’s a fantastic tool for creating something simple or even more complex. The only real issue is that I have no idea how to pronounce it. I usually just call it “Tinkerbell” in my head.

Is Tkinter on your device?

The most important thing is to ensure that Tkinter is installed. Usually when downloading the python package, Tkinter is installed with it. To test it, first you need to import tkinter, then use the tk._test() function. Like the following: 

If the output would be the Tkinter window pop up, then that means it works! If not, you need to install it mate. There would be a resource at the bottom to help installing the library, depending on what device you have. 

How the start

Lets import Tkinter into the top of the page using  import tkinter as tk. We use Tkinter because it is a library of preexisting functions, just like other libraries you might use in any project. These libraries make programming easier by allowing us to reuse multiple functions that are already written for us, saving time, improving code quality, and even sparing our fingers from too much typing.

The Tkinter library offers a ton of functions for creating graphical user interfaces (GUIs). It simplifies designing and managing elements like windows, buttons, text boxes, and other components you might need.

We will also be importing a ‘messagebox’ from the Tkinter library. We do this by typing ‘from tkinter import messagebox’. The messagebox function would be able to give your user some feedback after they have completed a task. Think of it as that error message you get on windows. Like this: 

Great editing skills I know. ᕦʕ •ᴥ•ʔᕤ

This is a good way to inform the user if an error has occurred and how to fix it/why, and or a confirmation if something is completed.  It is also helpful for you as the developer, making it easier to test and pinpoint the errors or successes in your application.

Initialising it (S cause I’m bri’ish)

Next we need to initialise the Tkinter application. Initializing in simple words just means ‘to set’ something (variable, object or system) with an initial value or state before using it. I know that isn’t really in ‘simple words’ but it does explain what it kind of means.

The way that this is done this through the following code:

if __name__ == “__main__”: 

app = tk.Tk() 

app.mainloop()

There are actually a few different ways that this could be done, but this one does make sense within my pea sized brain. Now let’s understand what each line actually means.

 if __name__ == “__main__”: makes sure that the code inside it only runs when the script is executed directly (Ctrl+ K + S on vs code), not when you it in another program.

app = tk.Tk() Just create the main application window using Tkinter

app.mainloop() Keeps the app running and open(through that event loop) and  handling user actions like clicks or typing until you close it.

Now let’s run!

Basic customisation

One thing cool about Tkinter is that you can customise the GUI to your heart’s desire, but for now we shall just cover the basics. 

For this we will make the windows a fixed size through the command of ‘.geometry(“length x width”)’ and to change the background we can use ‘.config (bg= ‘colour’)’. 

It’s not really necessary to design the GUI right away, as it’s better to start with a clear plan for how you want it to look.Though it is still helpful to understand the basics early on.

One cool feature of adding colours in Tkinter is that you’re not limited to using color codes. Tkinter includes its own color chart, which is handy if you’re unsure about the color scheme you want to use.

We should also add a title to make it look ‘professional looking’ , we do this by implementing the .title(“title”)

Now the final result would look like this:

For the buttons and input boxes, I’d probably advise you to do it while you’re doing the functions, or not. You can do whatever you want.

Not sure what the next post would be but, let’s code ‘em all! ʕ -ᴥ•ʔ♡

Resources:

+

Leave a comment