In this lesson we want to learn How to Build Calculator in Python TKinter, Here is an example of a simple calculator built using the Tkinter library in Python:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
import tkinter as tk import re class Calculator(tk.Tk): def __init__(self): super().__init__() self.title("Calculator") self.geometry("200x200") self.result = tk.Entry(self) self.result.grid(row=0, column=0, columnspan=4, padx=10, pady=10, ipadx=10, ipady=10) button_list = [ "7", "8", "9", "+", "4", "5", "6", "-", "1", "2", "3", "*", ".", "0", "C", "/" ] row = 1 col = 0 for button in button_list: self.button = tk.Button(self, text=button, width=5, height=2, command=lambda btn=button: self.button_click(btn)) self.button.grid(row=row, column=col, padx=10, pady=10) col += 1 if col > 3: col = 0 row += 1 self.equal = tk.Button(self, text="=", width=5, height=2, command=self.calculate) self.equal.grid(row=5, column=3) def button_click(self, button): if button == "C": self.result.delete(0, tk.END) else: self.result.insert(tk.END, button) def calculate(self): try: input_str = re.sub(r'[^0-9\+\-\*\/\.]', '', self.result.get()) self.result.delete(0, tk.END) self.result.insert(tk.END, eval(input_str)) except: self.result.delete(0, tk.END) self.result.insert(tk.END, "Error") calculator = Calculator() calculator.mainloop() |
This code creates a simple calculator using the Tkinter library in Python.
The Calculator
class is a subclass of tk.Tk
, which is the main window class of Tkinter. The __init__
method sets the title of the window to “Calculator” and sets its size to 200×200 pixels.
A text entry widget is created and added to the window using the tk.Entry
class. This widget will be used to display the results of calculations.
The button_list
variable is a list of strings that represent the buttons on the calculator. These buttons are then created and added to the window using a loop. Each button is created using the tk.Button
class and is given a label, a width, a height, and a command to execute when clicked. The button_click
method is used as the command for all buttons except the “=” button, which uses the calculate
method as its command.
The button_click
method is used to handle button clicks. If the button clicked is the “C” button, it clears the text in the result widget. If it’s any other button, it appends the button’s label to the text in the result widget.
The calculate
method is used to evaluate the mathematical expression entered in the result widget. It uses the re.sub()
function to remove any non-numeric or non-mathematical characters from the input, so that the eval()
function only receives a well-formed mathematical expression. It then uses the eval()
function to evaluate the expression and display the result in the result widget. If the input is not a valid mathematical expression or if any other error occurs, it displays “Error” in the result widget.
Finally, an instance of the Calculator
class is created and mainloop()
method is called on it, which starts the event loop and makes the calculator window visible on the screen.
Run the complete code and this will be the result.
