Get started quickly and build a personalized Python GUI calculator in five minutes

Get started quickly and build a personalized Python GUI calculator in five minutes

1. Introduction

In this tutorial, you will learn how to use Tkinter in Python to make your own fully functional GUI calculator in just a few minutes.

When completing this tutorial, you will not need any additional libraries other than Tkinter which is usually installed with the Python standard library.

If you are using Linux, you may need to install it:

 $ pip install python-tk

Once everything is installed, let’s start writing our calculator code. By the end of the tutorial, we will have something like this:

picture

2. Use eval() to solve math problems

eval() is a built-in function in Python that parses the expression argument and evaluates it as a Python expression.

We will use the concept of eval() to solve mathematical expressions.

Usage example:

 >>> while True: ... expression = input('Enter equation: ') ... result = eval(expression) ... print(result) ... Enter equation: 2 + (9/9) *3 5.0 Enter equation: 12 /9 + (18 -2) % 5 2.333333333333333

With these 4 lines of code, we have made a command line calculator in Python. Now let’s use the same concept to make a calculator with a graphical interface.

This GUI calculator has three main parts:

  • The screen (frame) used to display the expression
  • Button to save the expression value
  • Building calculator logic

3. Make a frame for the calculator

 from tkinter import Tk, Entry, Button, StringVar class Calculator: def __init__(self, master): master.title('Simple Calculator') master.geometry('360x260+0+0') master.config(bg='#438') master.resizable(False, False) root = Tk() calculator = Calculator(root) root.mainloop()

Output:

picture

4. Add a screen to display the expression

 from tkinter import Tk, Entry, Button, StringVar class Calculator: def __init__(self, master): master.title('Simple Calculator') master.geometry('360x260+0+0') master.config(bg='#438') master.resizable(False, False) self.equation = StringVar() self.entry_value = '' Entry(width = 28,bg='lightblue', font = ('Times', 16), textvariable = self.equation).place(x=0,y=0) root = Tk() calculator = Calculator(root) root.mainloop()

Output:

picture

As shown above, we have completed the construction of the display screen, now we need to add a button for forming mathematical expressions.

5. Add buttons for forming mathematical expressions

These buttons are created in the same way, differing only in the values ​​they store and their positions. The buttons used to form mathematical expressions are:

  • Numbers from 0 to 9
  • Mathematical operators +, -, /, %
  • Decimal point
  • brackets()

We need to attach a command to each button so that when we click it, it will show up on the display. To do this, write a simple show() function to implement this functionality.

 from tkinter import Tk, Entry, Button, StringVar class Calculator: def __init__(self, master): master.title('Simple Calculator') master.geometry('360x260+0+0') master.config(bg='#438') master.resizable(False, False) self.equation = StringVar() self.entry_value = '' Entry(width = 28,bg='lightblue', font = ('Times', 16), textvariable = self.equation).place(x=0,y=0) Button(width=8, text = '(', relief ='flat', command=lambda:self.show('(')).place(x=0,y=50) Button(width=8, text = ')', relief ='flat', command=lambda:self.show(')')).place(x=90, y=50) Button(width=8, text = '%', relief ='flat', command=lambda:self.show('%')).place(x=180, y=50) Button(width=8, text = '1', relief ='flat', command=lambda:self.show(1)).place(x=0,y=90) Button(width=8, text = '2', relief ='flat', command=lambda:self.show(2)).place(x=90,y=90) Button(width=8, text = '3', relief ='flat', command=lambda:self.show(3)).place(x=180,y=90) Button(width=8, text = '4', relief ='flat', command=lambda:self.show(4)).place(x=0,y=130) Button(width=8, text = '5', relief ='flat', command=lambda:self.show(5)).place(x=90,y=130) Button(width=8, text = '6', relief ='flat', command=lambda:self.show(6)).place(x=180,y=130) Button(width=8, text = '7', relief ='flat', command=lambda:self.show(7)).place(x=0,y=170) Button(width=8, text = '8', relief ='flat', command=lambda:self.show(8)).place(x=180,y=170) Button(width=8, text = '9', relief ='flat', command=lambda:self.show(9)).place(x=90,y=170) Button(width=8, text = '0', relief ='flat', command=lambda:self.show(0)).place(x=0,y=210) Button(width=8, text = '.', relief ='flat', command=lambda:self.show('.')).place(x=90,y=210) Button(width=8, text = '+', relief ='flat', command=lambda:self.show('+')).place(x=270,y=90) Button(width=8, text = '-', relief ='flat', command=lambda:self.show('-')).place(x=270,y=130) Button(width=8, text = '/', relief ='flat', command=lambda:self.show('/')).place(x=270,y=170) Button(width=8, text = 'x', relief ='flat', command=lambda:self.show('*')).place(x=270,y=210) def show(self, value): self.entry_value +=str(value) self.equation.set(self.entry_value) root = Tk() calculator = Calculator(root) root.mainloop()

Output:

The output is a calculator with buttons, when you click any of the buttons, its value will be shown on the display.

Now the only two buttons left to complete our calculator are a reset button to clear the screen and an equal sign (=) button to calculate the expression and display the result on the screen.

6. Add reset and equal sign buttons to the calculator

 from tkinter import Tk, Entry, Button, StringVar class Calculator: def __init__(self, master): master.title('Simple Calculator') master.geometry('360x260+0+0') master.config(bg='#438') master.resizable(False, False) self.equation = StringVar() self.entry_value = '' Entry(width = 28,bg='lightblue', font = ('Times', 16), textvariable = self.equation).place(x=0,y=0) Button(width=8, text = '(', relief ='flat', command=lambda:self.show('(')).place(x=0,y=50) Button(width=8, text = ')', relief ='flat', command=lambda:self.show(')')).place(x=90, y=50) Button(width=8, text = '%', relief ='flat', command=lambda:self.show('%')).place(x=180, y=50) Button(width=8, text = '1', relief ='flat', command=lambda:self.show(1)).place(x=0,y=90) Button(width=8, text = '2', relief ='flat', command=lambda:self.show(2)).place(x=90,y=90) Button(width=8, text = '3', relief ='flat', command=lambda:self.show(3)).place(x=180,y=90) Button(width=8, text = '4', relief ='flat', command=lambda:self.show(4)).place(x=0,y=130) Button(width=8, text = '5', relief ='flat', command=lambda:self.show(5)).place(x=90,y=130) Button(width=8, text = '6', relief ='flat', command=lambda:self.show(6)).place(x=180,y=130) Button(width=8, text = '7', relief ='flat', command=lambda:self.show(7)).place(x=0,y=170) Button(width=8, text = '8', relief ='flat', command=lambda:self.show(8)).place(x=180,y=170) Button(width=8, text = '9', relief ='flat', command=lambda:self.show(9)).place(x=90,y=170) Button(width=8, text = '0', relief ='flat', command=lambda:self.show(0)).place(x=0,y=210) Button(width=8, text = '.', relief ='flat', command=lambda:self.show('.')).place(x=90,y=210) Button(width=8, text = '+', relief ='flat', command=lambda:self.show('+')).place(x=270,y=90) Button(width=8, text = '-', relief ='flat', command=lambda:self.show('-')).place(x=270,y=130) Button(width=8, text = '/', relief ='flat', command=lambda:self.show('/')).place(x=270,y=170) Button(width=8, text = 'x', relief ='flat', command=lambda:self.show('*')).place(x=270,y=210) Button(width=8, text = '=', bg='green', relief ='flat', command=self.solve).place(x=180, y=210) Button(width=8, text = 'AC', relief ='flat', command=self.clear).place(x=270,y=50) def show(self, value): self.entry_value +=str(value) self.equation.set(self.entry_value) def clear(self): self.entry_value = '' self.equation.set(self.entry_value) def solve(self): result = eval(self.entry_value) self.equation.set(result) root = Tk() calculator = Calculator(root) root.mainloop()

Output:

VII. Conclusion

In just five minutes, we successfully built a Python GUI calculator using the Tkinter library. This calculator can perform basic mathematical operations and provide users with a friendly interactive experience.

Building a GUI calculator is not only a fun project, it also demonstrates the power and flexibility of Python. I hope it helps you and inspires you to further explore and develop more interesting GUI applications!

<<:  How wireless technology will improve connectivity, efficiency and consumer experience in 2024

>>:  The relationship and difference between URL, URI and URN

Recommend

The Wireless Network Alliance praises Wi-Fi 6E, and the future is promising

After Wi-Fi 6, wireless networks have also ushere...

An article to help you understand HTML5 MathML

[[347913]] HTML5 can use MathML elements in docum...

Industrial Internet + 5G, we must plan carefully before taking action

In the previous article, "IoT operating syst...

What is the situation of my country’s Internet network security in 2017?

Recently, Yun Xiaochun, deputy director and chief...

ABI: Massive MIMO improves capacity and availability in 5G operator speed tests

[[425783]] For years, there has been a debate ove...

C++ Programming Practice: IP Hash Load Balancing Algorithm

Today we are going to learn about NGINX. Nginx is...

Future Development Path of Home Broadband Infrastructure

Part 01: Background China Mobile Group has furthe...

The secret to a fast-growing business: SD-WAN technology

According to the latest survey statistics from a ...

Can the Internet of Things drive the deployment of IPv6?

IPv6 has features that IPv4 lacks, which makes it...

Practice on optimizing VUA forwarding performance of vivo unified access gateway

VLB stands for vivo load balance. As the IDC traf...