Create a Kivy Application
00:01 Creating a Kivy Application. One of the best ways to learn a new skill is by creating something useful. With that in mind, you’ll use Kivy to build a calculator that supports the following operations: addition, subtraction, multiplication, and division.
00:18 For this application, you’ll need a series of buttons in a layout. You’ll also need a box along the top of your app to display the equations and their results. On-screen, you can see a sketch of the calculator.
00:33 Now that you have a goal for the UI, you can go ahead and write the code.
00:58
First, you create a list of operators and a couple of handy values, last_was_operator
and last_button
, that you’ll be using later on.
01:11
Next, you create a top-level layout, main_layout
, and add a read-only TextInput
widget to it.
01:35 Here, you create a nested list of lists containing most of your buttons for the calculator.
01:55
In this line, you start a for
loop over those buttons. For each nested list, you’ll do the following: Create a BoxLayout
with horizontal orientation, start another for
loop over the items in the nested list, create the buttons for the row,
02:27
bind them to an event handler, and add the buttons to the horizontal BoxLayout
that was just created. Finally, you add this layout to main_layout
.
02:46
As a last part of the layout, you create the equals_button
, bind it to an event, and add it to main_layout
.
03:14
As you’ve already seen, most of the widgets in the application call .on_button_press()
, so that’s what you’ll be coding next. The method definition takes the instance
argument so you can access which widget called the function.
03:32
The values of the solution and the button text
are extracted to variables. Here, you check to see which button was pressed. If the user pressed C
, then you’ll clear the solution.
03:53
If they pressed any other key, then the program moves onto the else
statement. This checks if the solution has any preexisting value. And here you check if the last button that was pressed was an operator button.
04:13
If it was, then solution
won’t be updated. This is to prevent the user from having two operators in a row. For instance, 1 */
is not a valid statement.
04:27
Here, you check to see if the first character is an operator. If it is, then solution
won’t be updated, since the first value can’t be an operator value.
04:43
If none of the previous conditions are met, then solution
is updated with the new_text
. last_button
is set to the label of the last button pressed, and last_was_operator
is set to True
or False
depending on whether or not it was an operator character.
05:08
The last piece of code to write is .on_solution()
. Here, you get the current text from solution
and use Python’s built-in eval()
to execute it. If the user created a formula such as 1+2
, then eval()
will run the code and return the result. Finally, you set the result as a new value for the solution
widget.
05:42
Note that eval()
is somewhat dangerous because it can run arbitrary code. Most developers avoid using it because of that fact. However, since you’re only allowing integers, operators, and the period as input to eval()
, it should be safe to use in this context.
06:01
The last piece of code needed for the application will already be familiar to you: creating the app from the class that you’ve written and then invoking the .run()
method.
06:13 When you run the code on a desktop computer, your application should look has seen on-screen.
06:31 Now that you have a complete application, it’s time to package it for deployment, and that’s what you’ll be looking at in the next section of the course.
Become a Member to join the conversation.