I have spent the last 2 weeks learning Python for backend development purposes. One of the obstacles I have faced is making changes to some code and not being able to actually see something visually changing. This is also after spending the 2 months prior to learning Python on learning JavaScript and React.js and getting used to seeing the changes made to the code be reflected on the actual page. I was able to adapt, but it was tough making the adjustment. All of this is to say, this is why I decided to take a look at a library that helps make a GUI for Python.
What is a GUI?
The average person probably does not realize that they interact with a bunch of different GUIs throughout their entire day. GUI stands for graphical user interface. So when you are really interacting with anything digital, you are interacting with a GUI. These are how users interact with technology and get them to do what they want. If you are here though, you probably already know this. This is what a developer needs to make as friendly as possible for their users to interact with the software they made.
Finally, What is Tkinter?
Tkinter is a library for Python. What is this library useful for? Well the answer is making GUIs! Two great things about Tkinter is that it is beginner friendly and compatible with Windows, macOS, and Linux.
How Does it Work?
You start by importing the modules. After that, you create the main window for the GUI. Choose which elements (widgets) you want incorporated in the GUI. Finally you will run the mainloop method to start up the GUI. To get a basic GUI up and running with this library, all you would need is about 5-6 lines of code. Obviously the more advanced you make it and more modifications you want to make, it will add to it. If you were to code this on your own, then it would take significantly more work and time.
Let's Take a Look at Some Examples
Here you can see we didn't do much besides create a simple GUI that really isn't very interactive. This is only with four lines of code though. At the top, you can see the import. Second line is creating the GUI window. Third line is creating the title for the window. The last line is what will make the GUI run.
You can see this one is very similar to the last one. All we did was add "Hello" in big font. We did this by adding the label widget as the new fourth line of code and modified some attributes like the font.
Here we were able to add a menu widget. Almost everything after the fourth line of code is new and is what is making this new aspect of our GUI. The fifth line is making our menu widget by making menu = the Menu class. The sixth line is make another Menu instance. The seventh line is adding an item (New) to the second Menu instance. Then finally, the eighth line is running a function that adds the label "file" and our second instance to our first Menu instance to create that drop down.
Why Should You Care?
The whole point of this was to show you that Tkinter may seem advance but in all honesty it isn't that bad at all after you look into some documentation on its widgets and functions. We were able to make a GUI with a file menu option in 10 lines of code. That is pretty amazing! Once you are ready to make your own project, you will most likely need to make a GUI so that users can properly interact with your software. Even if you later find a library you like more to help you make a GUI or you decide to write your own code to make your own, you can temporarily set this one up so you can actually see some changes you make to your code. So even if it isn't Tkinter, go out and find one you like and learn it!
Top comments (0)