DEV Community

loading...
Cover image for Have dinner with Python's Tkinter Library tonight

Have dinner with Python's Tkinter Library tonight

akmukiibi profile image Vin Mukiibi ・3 min read

Is there a more concise language than Python? Of course not, you and I know that. Lets explore Python's Tkinter library for developing wonderful and good looking Graphical User interfaces. It has beautiful documentation at the link below
https://docs.python.org/3/library/tk.html . The docs shall tell you the story, as for us, lets dive right in with a practical introduction to tkinter.
This tutorial assumes that you know Python basics, already have a cool Python IDE (I personally prefer JetBrain's PyCharm) and a heart for learning python, especially Tkinter.

The plan
Our plan is to have you build your home town's people directory, and that shall be our deliverable. A user should be able to add a person onto the directory, update any person, remove a person and clear the directory at a go. We all know the proverbial CRUD introduction. Lets get our feet wet.

Algorithm
All programs follow a procedure, you should have it in your mind even before you write a single line of code. As they normally say, coding is 90% thinking, 5% writing code, and 5% sipping on your cup of tea/coffee (whatever you prefer, I prefer tea).
This is what we want
Alt Text

So we gonna first define our UI i.e. the input elements, buttons etc (VIEW), then we gonna think about how to store our data (MODEL), then we gonna put our logic into the UI to send our data to the database (CONTROLLER). We have talked too much, lets start, follow the comments.

Lets first import our tkinter library and a few other dependencies
Alt Text

Alt Text

Then we gonna define our widgets
Alt Text
Feel free to define the last widget which is for the contact

Now this is where it gets interesting, we define our listbox where we shall view our people, then attach a scrollbar to it vertically

Alt Text

Then we gonna put our buttons
Alt Text

That is all for the UI, concerning the database, we shall store our data in an sqlite database whose code have already written, download it for free at https://github.com/akmukiibi/py-tkinter-pirple/blob/master/db.py JUST MAKE SURE ITS IN THE SAME DIRECTORY AS YOUR MAIN FILE.

Lets move onto the logic i.e. controller bit
We need to write a few functions that gonna invoke our db api after collecting user input or user events on our buttons.

Lets fetch all our people in the directory and put them in the listbox

Alt Text

Now lets add a user to the db by writing a function to invoke the db.py

Alt Text

Then lets write a function to select an item once clicked on the listbox
Alt Text

Lets update and delete a person from the list, feel free to write the function that clears the inputs on your own. We learn by doing, right??

Alt Text

Finally, lets populate the list and start our window GUI

Alt Text

Bingo

Find the full code on github at https://github.com/akmukiibi/py-tkinter-pirple.

Discussion (0)

pic
Editor guide