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! |