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.
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.
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
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
Then we gonna define our widgets
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
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
Now lets add a user to the db by writing a function to invoke the db.py
Then lets write a function to select an item once clicked on the listbox
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??
Finally, lets populate the list and start our window GUI
Find the full code on github at https://github.com/akmukiibi/py-tkinter-pirple.
Top comments (0)