(Last Updated On: November 20, 2018)

Episode 2 in our PyQt5 Beginner tutorial series


This is a continuation of our previous PyQt5 beginner tutorial. In order to gain the most benefit out of this tutorial, it’s necessary to have first read that tutorial.

In the second episode of this PyQt5 beginner tutorial series, we’ll go through some of the sample code that comes with PyQt5. I will cover part 3 of the address book tutorial.

The code



I’ll start with class Addressbook which begins at line 33. Most of the code was explained in part one of this series. I will only cover what was not.

Lines 38-39: We declare two variables oldName and oldAddress for future use.

Lines 42-43: We declare a text edit box and set it’s readonly property to true. When we click on it, we will not be able to enter any text into it. Not yet anyway.

Lines 49-58: We add some buttons to our window. In line 50, the show() method means that addButton is visible. submitButton and cancelButton are created but they are made invisible.

nextButton and previousButton show up in our window, but they are disabled so the user can’t interact with them yet.

Lines 60-64: We add click handlers for our widgets.

The rest of this class should be easy to follow for anyone who read the previous tutorial.

PyQt5 beginner tutorial image 1

When we first run the program, the only button we can interact with is the “Add” button. The handler for this is found on line 87.

Line 88-89: Remember our two variables from lines 38-39. Here we assign them the values contained in the variables nameLine and addressText.

Lines 91-96: We clear the textboxes nameLine and addressText. We make them editable by setting setReadOnly(false), on them. We then set the focus i.e. where our cursor points to nameLine.

Lines 98-102: We disable the Add, Previous and Next buttons. We also make the Submit and Cancel buttons visible.

PyQt5 beginner tutorial image 2

The handler code for the cancel button begins on line 137.

Lines 138-139: Do you remember our two variables from lines 88-89? Here we set nameLine and addressText to the values these variables contain.

Lines 141-143: However if these variables don’t contain any data yet, we set these textboxes to contain no text in them.

Lines 149-151: self.contacts is a dict that contains our address book entries. We get the size of this dict and store it in a variable called number. If there are at least 2 entries in this variable we enable the next and previous buttons.

The handler code for the submit button begins on line 104.

Lines 105-106: The variables name and address contain the values contained in the respective textfields.

Lines 108-110: if any of these variables is blank, we show an error message.

Lines 113-120: One property of dicts is that they must contain unique key values. If the key value(text contained in the variable name) isn’t contained in our dict, we add it to our list and show a “success” messagebox.

If the key already exists in our dict, we show an error message.

The class that handles our dict begins on line 5. This class creates a sorted dict for us. It is called in line 37 of our code.

Lines 7-11: We initialise some internal variables. _idx is our dictionary index. _nr_items contains the size of our dictionary.

Lines 16-24: This sorts through our list. If the value of _idx is greater than _nr_items an exception is raised. Else, we step forward in our dict. This function returns a key-value pair and then increments the index value, _idx by 1

For more information on dicts in Python see this page.

Lines 157-158: This function handles code called when we press the next button. The variable name contains the value of the name textbox. The iter() function assigns our variable it  the contents of  our dictionary. It returns the keys in no particular order.

Lines 160-168: We have a try-catch exception block. In our try block on line 162 we assign the key-value pair of the next item in our dict to this_name and _.

We loop through the values contained in our dict. If our current name value equals the content of our name textbox then we set next_name, next_address to the value of the next entry in the dict.

Lines 170-171: We assign the value of next_name, next_address to nameLine and addressText.

The handler for the previous button begins at line 173.

Line 176: We initialise prev_name, prev_address to None

Lines 177-182: We loop through our dict and store the values in this_name, this_address. If this_name equals the value contained in nameLine, we exit our loop. If not we assign prev_name, prev_address to the values we get.

In other words, we want to assign any other value apart from what is contained in nameLine to prev_name, prev_address.

Lines 189-190: The pass statement does nothing. It’s only there to ensure syntactic correctness. So if prev_name is blank, we set it’s value to an empty string.



Hope you enjoyed reading this PyQt5 beginner tutorial.
** Update, March 26th 2018: Click here to recieve your PyQt5 book **


fadawar · January 28, 2014 at 12:18 pm

Thanks for the article!

    admin · January 30, 2014 at 9:38 am

    You’re welcome 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *

Reload Image
Visit Us On Twitter