You should know the basics of the Tkinter package in Python before proceeding with this tutorial.
Basic Setup for the App
1. Importing the module
The first step for building the app is importing the module. While importing the Tkinter module, you’ll need to instantiate the ttk object.
2. Creating the Window Variable
To create a window, you need to create a window object using ttk. After creating a window object, you can assign a title and geometry to the window. The geometry will set the window’s height and width.
3. MainLoop
The mainloop() method runs the window in an infinite loop. It runs continuously unless the user closes the window manually.
Building the UI
The Tkinter package in Python has a lot of widgets that help make user-friendly designs. The widgets used in this project are button and text fields. For this project, you need the following buttons: 0-9 numbers, add, subtract, multiplication, division, clear, delete, calculate.
The button widget accepts many arguments: the window object, text displayed on the button, font style, etc. It also accepts a command argument that runs a function or method when clicking the button.
To align buttons in rows and columns for a user-friendly UI, make use of the grid attribute. The grid attribute accepts the row number and column number as arguments to align the buttons accordingly.
The Entry widget is the text box in the Python Tkinter package. Usually, the Entry field accepts many arguments—but one of the most crucial and required arguments is textvariable. The user can type in the Entry field because of this argument. In the above code, a variable named numericEq is assigned an attribute ttk.StringVar() to manage the Entry widget.
You can run the Python file using the python file_name.py command. The application looks like this:
Adding Functionality to Buttons:
As we mentioned earlier, the Button widget has a command attribute that accepts a function or method to be called. The methods passed when buttons are clicked use the lambda function.
1. Numeric and Arithmetic Buttons
The numeric buttons consist of values from 0-9 and, the arithmetic buttons are +, -, x, / for calculation purposes.
The addToEq() method gets called upon clicking the button. This method passes numbers or arithmetic operators depending on the button you click. After passing the value, the numbers or operators get stored in the calcValue variable. Before storing the value or operator in the calcValue variable, you must convert it to string type using the str() method.
2. Calculate Buttons
The button with the label cal calculates the entire string stored in the calcValue variable. The eval() function helps to perform arithmetic operations on the calcValue variable and return the total. After retrieving the value, set the total value in the numericEq variable. The numericEq variable displays this value in the Entry box.
3. Clear Button
The clear button clears the Entry box. Upon clicking the clear button, the clearInput() method is called. The variable calcValue is set to an empty string, and it’s stored in the Entry box.
Once you implement all these methods, the output of the code looks like this:
Projects are the Best Way to Improve Your Coding Skills
Now that you’ve learned how to build a simple calculator using GUI in Python Tkinter, it’s time for you to explore other Python packages. Python has a variety of packages that help you build any application you can dream up.
As you likely already know, projects are the best way to showcase your skills. Building projects will help you get a good grasp of the language and build your resume.