Have dinner with Python's Tkinter Library tonight
Tags: python, tkinter, introduction, pirple
Published: 2020-10-05 18:39:19

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](https://dev-to-uploads.s3.amazonaws.com/i/zyg7jtb90lzxsprvoem6.PNG)

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](https://dev-to-uploads.s3.amazonaws.com/i/y8r4p7zr13nekpz3k0bs.PNG)

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/rdnw4uotfefcmlh3td79.PNG)

Then we gonna define our widgets
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/km7xxxfvqe12jdi1vlgb.PNG)
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](https://dev-to-uploads.s3.amazonaws.com/i/zd7zccn9z5txu49t4gxe.PNG)

Then we gonna put our buttons
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/mnp3y6fnja2cd6xqn87o.PNG)

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](https://dev-to-uploads.s3.amazonaws.com/i/gvc9i1fgyvic7h43joc1.PNG)

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

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/o0v7xlmcvdm6cblqrhvh.PNG)

Then lets write a function to select an item once  clicked on the listbox
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/v4cv0npaavofdxjifxpw.PNG)

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](https://dev-to-uploads.s3.amazonaws.com/i/r3cl84rzxu9icssz6ho3.PNG)

Finally, lets populate the list and start our window GUI

 ![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/l0709ptrqpzce5yzi53t.PNG)

Bingo

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