Estimated reading time: 3 minutes
Let’s make the introductions 🙂
Tkinter is a package that allows a programmer to build a GUI interface, which then can be opened on a computer screen by a user. There are many different types of GUI apps, but examples include a calculator or a text editor that opens when you click it.
Tkinter would be the most commonly used GUI package in Python, due to its simplicity, but PySimpleGUI, PYQt or PySide are other alternatives. Ensure you research these before using to make sure they suitable for your needs.
Why use Tkinter?
- Relatively simple and easy to learn, upskilling is quick.
- A great introduction to the concepts and ideas for building GUI apps, you will get a good grounding in the techniques and approaches needed.
- Very well documented, so a programmer should be able to find the answer to anything specific they need to understand.
Now we are introduced, let’s see how to utilise it:
Install Python as usual, and make sure that Tkinter is working and you have the correct version. Note that import tkinter is for version 3.x, before that use import Tkinter
Please note that you will see in places when using Python code, that capitalization is important. This will sometimes puzzle you as to why some of your code does not work, usually, the interpreter should flag it for you.
While it is correct to put a capital at the start of a line, the programming language will ignore written English convention. An example is as follows:
list = ['a','b','c'] Print(list) gives NameError: name 'Print' is not defined whereas list = ['a','b','c'] print(list) gives ['a', 'b', 'c'] No errors
When saving your python script DO NOT call it tkinter.py as I did, the import statement will not work. Call it something like tkinter_test.py, see red arrow below.
At the start of the video below the code will look like this. the first six lines are the creation of the Tkinter screen its size and any buttons that will appear on it.
Note that all code should appear betwee line two and line six, so that the screen output works and looks correctly.
Added to this code in the following video:
- Button – which will open our YouTube channel
- An image
- A clickable link – Which will bring you to our Home Page
A screenshot of the final output is as follows:
See a link to the Python documentation here Tkinter on python.org