Python and Sqlite with Tkinter: records into listbox [part 1]

Spread the love

Making database, working visually

Let’s start a tutorial to make a very simple sqlite database to be ‘manipulated’ by Python with the complicity of a GUI, graphoc user interface, realized with the builtin module tkinter.

The code

In this first part, we will create a GUI to create databases. We will also have a list of the db created in the window that we are going to create. You will learn to:

  • create a database with sqlite
  • create a window with tkinter
  • populate the window with widgets:
    • labels
    • entry
    • buttons
    • listbox
  • add a command to a button to make the computer act on the database
  • insert values (populate) a listbox with data from the folder
  • get values from an entry and insert them in the listbox
  • create a class
  • access to a method or attribute of the class
  • and other little skills

Create a database

To create a database with sqlite3, after you imported it

you do it this way

Now the database is created, go to look in the folder where you are coding.

It is a good behaviour to close the database when you do not use it, like this:

Let’s make a function to do that

The code below shows how to create a db. It will be called when you will press a button. In the GUI we will build, in fact, there will be an entry where the user will write the name of the database and when he presses the button it sends all the window object (window) to this function below so that we can take the name of the database with window.e.get(), where e is the name of the entry. I will give more explanations after the following code:

The above funtion gets from the user (in db) the text with the name of the database from an entry (called e) that will be in the GUI (in the following code below). In practice window is the istance of the GUI class, e is the entry in that GUI and get() is the method to get the text in e. Now that we have it in db, we can check if the user has used the .db extension in the name (in window.e). If not, the code adds the “.db” extension at the end.

After this initial checks, the database is created (conn = lite.connect(db)). lite is the module sqlite3 (we gave it that name in the import line, take a look at the 1st line).

P.S. :With the try except block we test a block of code for errors. If all goes right it creates the db and finally (at the end) it closes it, adds the name of the db in lb (the listbox that we will add) and wil delete the name the user has input in the entry (window.db.set).

Now let’s see the code for the GUI window tha will make everything clearer.

In this class, that I rewrote differently from the one you see in the video, creates a label, an entry, a button, a listbox. I created a method for each widget to make more readable the code.

When the user hits the button you see the command is a lambda function (an anonymous function) that runs the code in the create(db) funtion that we’ve seen previously.

The whole code

Now we can take a look at the whole code.

Live coding to make a db with sqlite and Python with tkinter

This video explains the code above to make a database with sqlite3. It lasts 20 minutes.

In the next post we will see how to create a table with the tkinter GUI.