Add New Record Tab

Let's think what we need to do to add a new record to the database.

  1. We'd like to be able to select a new photo from anywhere on our computer. This photo then needs to be added to our photos directory. We need to extract just the name of the photo because we're not storing the whole file path in our database.
  2. We need to make sure that no text boxes are blank
  3. We need two Boolean flags: one to say that an image has been added, and one to say that there are no blank text boxes
  4. If everything is OK, then we can add the record to the database
  5. Finally, we can reload the records when the All Records tab is clicked on. Reloading will display the new record that we've just added

We'll start with number 1 on the list - adding a new image. We added a button on tab two. When this button is clicked, we'll first launch an open file dialog box. After an image is selected, we can grab the file name and the path to where it is. We can then use this information so that the file can be copied later. We'll set an image selected variable to True. We'll also insert the chosen image on to the label on tab two. Let's get started.

First, we'll need some variable set up at the top of the code. Add these variables just below the ones you already have (where your row_counter and num_of_rows variables are):

image_selected = False
image_file_name = None
file_to_copy = None
file_new_home = None

Your code in PyCharm will look something like this:

Python variables added to the code

Now we can set up a function that will be called when the Add Image button is clicked. Add this function to your code, along with the globals:

def select_image():

global image_selected
global image_file_name
global file_new_home
global file_to_copy

In a previous section, you learned how to launch an open file dialog box. We'll do the same here. Add this just below your global variables:

path_to_image = filedialog.askopenfilename(initialdir="/",

title="Open File",

filetypes=(("PNGs", "*.png"), ("GIFs", "*.gif"), ("All Files", "*.*")))

You've done this before, so we won't explain the code. But you could add more image types, if you wanted, such as JPEG. Here's what your function should look like so far:

Adding the Python code for a file dialog box

We can use an if statement to see if there's anything stored in the path_to_image variable. If there is (a file has been selected) then path_to_image can be tested for a True value:

if path_to_image:


You can use a method called basename to get just the name of your file. The basename method is part of the builtin os.path library. You use it like this:

if path_to_image:

image_file_name = os.path.basename(path_to_image)

After the equal sign we have os.path.basename. In between the round brackets of basename, we have our path_to_image variable. This will get you the name of the file from the long path that Python returns from askopenfilename. Add the basename line to your code.

For the file_new_home variable, we'll add the image file name to our photos directory. Here's the line to add:

file_new_home = db_config.PHOTO_DIRECTORY + image_file_name

To get the file name and the path, add this line:

file_to_copy = path_to_image

This will get you a path like this on Windows:


But we're just storing the path in a handy variable for use later.

Now that an image has been selected, we can set image_selected to True:

image_selected = True

One more thing we need to do is to load the selected image into the image label on tab two. Add a new function for this. Add the following:

def load_photo_tab_two(file_path):

image = image_path(file_path)
imgLabelTabTwo.image = image

We did this with an earlier function only now we're configuring label two. Call this function under your image_selected = True line:


We're passing over the full file path, here. This is not what we're going to be storing in the database. It's just for show, really, so that you can see the image on tab two. But your function should look like this:

Getting the file name and base path from a Tkinter dialog box

One thing we can do is to add a try … except part. If something goes wrong, you can catch an IOError.

An easy way to add a try … except block is through the menus or shortcuts. Highlight the entire if block:

A Python if statement highlighted

On the menu at the top of PyCharm, click Code. From the Code menu, select Surround With. (A shortcut is CTRL + ALT + T.) You should see the following menu appear:

Surround With shortcut menu in Pycharm

Select try / except from the menu. The code will then look like this:

Adding a try ... except part to the if statement

Amend the except line to this:

except IOError as err:

In place of the keyword pass, set the image_selected variable to False and display a message box:

image_selected = False
messagebox.showinfo("File Error", err)

Your code will then be this:

A try ... except block of Python code

Almost ready to try it out. Scroll down and locate your Add Image button:

buttonAddImage = tk.Button(tab2, text="Add Image")

Add a command parameter that calls your select_image function:

buttonAddImage = tk.Button(tab2, text="Add Image", command=select_image)

Try it out. Run your program. Click your Add Image button. Navigate to your photos directory and select a new image. When you click the Open button on the file open dialog box, you should see your selected image appear in the label on tab two:

An image inserted into a Tkinter label

OK, we're getting there. We've opened an image and inserted onto tab two. We've stored the name and path of this image. We've also set a flag called image_selected. (We can use this flag later to reload the database.) The next thing to do is to check for blank text boxes and add a new record to the database. We'll continue all this in the next lesson.

The Add New Record Button >