Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Implementing the Model

00:00 Implementing the Calculator’s Model and Controller. In the MVC pattern, the model is the layer of code that takes care of business logic. In the calculator app, the business logic is all about basic math calculations, so your model will evaluate the math expression that your users introduce in the calculator’s GUI.

00:21 The calculator’s model also needs to handle errors. To this end, you’ll define a global constant.

00:34 The error message constant is the message that your user will see on the calculator’s display if they introduce an invalid expression. With this change made, you are now ready to code the app’s model, which will be a single function.

00:47 In evaluateExpression(), you’ll use eval() to evaluate a math expression that comes as a string.

00:59 If the evaluation is successful, then you’ll return result. Otherwise, you return the predefined error message. But note that this function is not perfect.

01:08 It has a couple of important issues.

01:13 The try-except block doesn’t catch a specific exception, so it’s using a practice that’s discouraged in Python. The function uses eval() which can lead to some serious security issues.

01:25 You are free to rework this function to make it more reliable and secure, but in this tutorial, you’ll use the function as is to keep the focus on implementing the GUI.

01:36 The controller class will connect the view to the model that you’ve just coded. You’ll use the controller class to make the calculator perform actions in response to the user’s events. It needs to perform three main tasks: Access the GUI’s public interface, handle the creation of math expressions, and connect all the buttons’ .clicked signals with the appropriate slots.

02:00 To perform all these actions, you’ll code a new PyCalc class. First, you import partial from functools, as you’ll use this function to connect signals with methods that need to take extra arguments.

02:27 Inside PyCalc, you define the class initializer, which takes two arguments, the app’s model and its view. Then you store these arguments in appropriate instance attributes.

02:38 Finally, you call connectSignalsAndSlots() to make all the required connections of signals and slots.

02:50 In calculateResult you use evaluate_expression() to evaluate the math expression that the user has just typed into the calculator’s display.

02:58 Then you call setDisplayText() on the calculator’s view to update the display text with the computation result.

03:20 As its name suggests, the buildExpression() method takes care of building the target math expression. To do this, the method concatenates the initial display value with every new value the user enters on the calculator’s keyboard.

03:35 Finally, the connectSignalsAndSlots() method connects all the buttons click signals with the appropriate slots method in the controller class.

03:47 If the key symbol isn’t ‘C’ or ‘Enter’, then the button click action is connected to call to partial, extending the expression with the key symbol.

04:09 The equals button is connected to calculateResult as is pressing return on the keyboard.

04:22 Finally, the ‘C’ button is connected to clearDisplay. And that’s it, your controller class is ready. But for all this code to work as a calculator, you’ll need to update the app’s main function.

04:39 You create a new instance of PyCalc, the model argument to the PyCalc class constructor holds a reference to the evaluate expression function while the view argument holds a reference to the PyCalc window object.

04:53 Now your PyQt calculator application is ready to run. Run the calculator from the command line, and you should be able to perform calculations as seen. Enter a valid math expression with a mouse.

05:08 Press Enter or click the equal sign to compute and show the expression result on the calculator’s display.

05:27 You can also see if you enter an invalid expression, the error is displayed. And that’s it. You’ve developed your first fully functional GUI desktop application using Python and PyQt.

05:44 In the next section of the course, you’ll take a look at some additional tools you can use to aid your development using PyQt.

Become a Member to join the conversation.