Home Python Course Menu Book

Python - The Add New Record Button


We need to do quite a lot with the button that adds a new record. Here's what we need to do:

  1. Check for blank text boxes
  2. Display an error if any are blank
  3. If all filled in, set a flag
  4. Check the image_selected flag
  5. If True:
    1. Copy the image to new home:
    2. Get text from text boxes
    3. Get photo file name
    4. Call an insert_record function that we'll set up
  6. If False:
    1. Display an error message

We can check for blank text boxes, first. Set up a function called add_new_record in your code. We'll need access to some of those global variables we set. Add these three to your function:

global blank_textboxes_tab_two
global file_new_home
global file_to_copy

We'll also need an internal variable to keep a count of the empty text boxes. So add this below the three other variables:

blank_textbox_count = 0

Your new function will look like this:

A Python function

You should have a faint underline for global blank_textboxes_tab_two. This is because we haven't added it outside the function. Add this line under your other variables:

blank_textboxes_tab_two = True

Here's your coding window:

Setting up a Python boolean variable

Now we can check the text boxes.

We can increment the blank_textbox_count variable. If it's greater than 0 then it means one of the text boxes is blank. Add this if statement:

if fNameTabTwo.get() is "":

blank_textbox_count = blank_textbox_count + 1

We used String Vars to set up text variables for our Entry text boxes. One of these was called fNameTabTwo. We can use get() to get the text that was entered. You only need to say is "" to check for blank boxes. The pair of doubles quotes with no spaces between them is seen as a blank string by Python.

If there is a blank string, we add 1 to the blank_textbox_count variable.

Add similar if statements to check the other two entry text boxes:

if famTabTwo.get() is "":

blank_textbox_count = blank_textbox_count + 1

if jobTabTwo.get() is "":

blank_textbox_count = blank_textbox_count + 1

The only change is to which StringVar we're using, famTabTwo and jobTabTwo this time.

Your function should look like this in PyCharm:

Python error cheecking on Tkinter text boxes

We can now check the blank_textbox_count variable to see what's inside it:

if blank_textbox_count > 0:


elif blank_textbox_count is 0:


So if blank_textbox_count is greater than 0 then it means that a text box wasn't filled in. If blank_textbox_count is 0 then we can do the main code.

The code for the first part of the if statement is this:

blank_textboxes_tab_two = True
messagebox.showinfo("Database Error", "Blank Text boxes")

We set the blank_textboxes_tab_two flag to True if there are any blank text boxes. Then we display a message.

The first line of the main code can set the blank_textboxes_tab_two to False, meaning everything is OK.

Add this to your code, then:

if blank_textbox_count > 0:

blank_textboxes_tab_two = True
messagebox.showinfo("Database Error", "Blank Text boxes")

elif blank_textbox_count is 0:

blank_textboxes_tab_two = False

It will look like this in PyCharm:

Checking for blank text boxes in Python

There are a few things we want to do if the blank text box count is zero:

  1. Check if the image_selected flag is True
  2. Try to copy the chosen image file over to the photos directory
  3. Get the text box data
  4. Get the image name
  5. Insert the details into the database

We can use an if statement to check that an image has been selected. If not, display a message:

if image_selected:




To copy an image, we can use the shutil library. This is short for Shell Utility and can be used to copy files, amongst lots other things.

shutil.copy(file_to_copy, file_new_home)

In between the round brackets of copy, you need the location of the file you're trying to copy, and you need where you're want to copy it to.

Both of these are stored in our variables file_to_copy and file_new_home.

But we can wrap this in a try … except block:


shutil.copy(file_to_copy, file_new_home)

except shutil.SameFileError:


Notice that the exception is SameFileError. This will prevent our program from crashing if the chosen file already exists in our photos directory. If it does, we use the keyword pass. This just means don't do anything.

Add a message box to the else part of the if statement:

messagebox.showinfo("File Error", "Please select an image")

Your code will then look like this in PyCharm:

Using shutil in Python

We can now get the text box data and the image name. We'll pass these over to a new function. Let's call the new function insert_into_database. Add the following line:

insert_into_database(fNameTabTwo.get(), famTabTwo.get(), jobTabTwo.get(), image_file_name)

Add it just below pass, but on the same indent as the except part:


shutil.copy(file_to_copy, file_new_home)

except shutil.SameFileError:


insert_into_database(fNameTabTwo.get(), famTabTwo.get(), jobTabTwo.get(), image_file_name)

You'll have a red underline for insert_into_database. That's because we haven't yet added this function. In PyCharm, your code should be this:

Setting up a Python function to insert text box data into a database

But the new function will be passed four things: the data from the three text boxes plus the image file name.

In the next lesson, we'll set up that insert_into_database function. You'll learn how to write SQL to insert a record into a database.