Simple GUI Calculator Using Tkinter
Graphical User Interface (GUI) development is a crucial component of contemporary software creation. The Python library Tkinter offers a simple interface for developing GUI programmes. In this article, Tkinter will be used to build a simple GUI calculator.
Tkinter provides a set of widgets that can be used to create the user interface of an application. These widgets include buttons, labels, text boxes, check boxes and more. We will use these widgets to create the calculator.
To start, we need to import the Tkinter library and create a window for the calculator. We can do this using the following code:
import tkinter as tk
# Create a window
window = tk.Tk()
window.title("Simple Calculator")
This code imports the Tkinter library and creates a window with the title "Simple Calculator".
Next, we need to create a text box where the user can enter the numbers to be calculated. We can do this using the Entry widget. We will also create a label to display the result of the calculation. We can do this using the Label widget. The code for this is as follows:
# Create an entry box for the numbers
entry_box = tk.Entry(window, width=40, borderwidth=5)
entry_box.grid(row=0, column=0, columnspan=3, padx=10, pady=10)
# Create a label for the result
result_label = tk.Label(window, text="", width=40, borderwidth=5)
result_label.grid(row=1, column=0, columnspan=3, padx=10, pady=10)
This code creates an entry box and a label and places them in the window using the 'grid' method. The 'grid' method allows us to specify the row and column where we want the widget to be placed.
Next, we need to create buttons for the calculator. We will create buttons for the digits 0-9, as well as buttons for addition, subtraction, multiplication, division, and clear. We can do this using the 'Button' widget. The code for this is as follows:
# Create buttons for the digits
button_1 = tk.Button(window, text="1", padx=40, pady=20, command=lambda: button_click(1))
button_2 = tk.Button(window, text="2", padx=40, pady=20, command=lambda: button_click(2))
button_3 = tk.Button(window, text="3", padx=40, pady=20, command=lambda: button_click(3))
button_4 = tk.Button(window, text="4", padx=40, pady=20, command=lambda: button_click(4))
button_5 = tk.Button(window, text="5", padx=40, pady=20, command=lambda: button_click(5))
button_6 = tk.Button(window, text="6", padx=40, pady=20, command=lambda: button_click(6))
button_7 = tk.Button(window, text="7", padx=40, pady=20, command=lambda: button_click(7))
button_8 = tk.Button(window, text="8", padx=40, pady=20, command=lambda: button_click(8))
button_9 = tk.Button(window, text="9", padx=40, pady=20, command=lambda: button_click(9))
button_0 = tk.Button(window, text="0", padx=40, pady=20, command=lambda: button_click(0))
# Create buttons for the operators
button_add = tk.Button(window, text="+", padx=39, pady=20, command=button_add)
button_subtract = tk.Button(window, text="-",padx=41, pady=20, command=button_subtract)
button_multiply = tk.Button(window, text="*", padx=40, pady=20, command=button_multiply)
button_divide = tk.Button(window, text="/", padx=41, pady=20, command=button_divide)
Create a button to clear the input:
button_clear = tk.Button(window, text="Clear", padx=79, pady=20, command=button_clear)
Place the buttons in the window using the grid method:
button_1.grid(row=2, column=0)
button_2.grid(row=2, column=1)
button_3.grid(row=2, column=2)
button_4.grid(row=3, column=0)
button_5.grid(row=3, column=1)
button_6.grid(row=3, column=2)
button_7.grid(row=4, column=0)
button_8.grid(row=4, column=1)
button_9.grid(row=4, column=2)
button_0.grid(row=5, column=0)
button_add.grid(row=2, column=3)
button_subtract.grid(row=3, column=3)
button_multiply.grid(row=4, column=3)
button_divide.grid(row=5, column=3)
button_clear.grid(row=5, column=1, columnspan=2)
This code creates buttons for the digits and operators and places them in the window using the `grid` method. The `command` parameter of each button specifies the function to be called when the button is clicked. We will define these functions next.
Next, we need to define the functions that will be called when the buttons are clicked. We will define functions for adding, subtracting, multiplying, and dividing the numbers entered by the user. We will also define a function to clear the input.
Function to handle button clicks
def button_click(number):
current = entry_box.get()
entry_box.delete(0, tk.END)
entry_box.insert(0, str(current) + str(number))
Function to handle the addition
def button_add():
first_number = entry_box.get()
global f_num
global math_operation
math_operation = "addition"
f_num = int(first_number)
entry_box.delete(0, tk.END)
Function to handle subtraction
def button_subtract():
first_number = entry_box.get()
global f_num
global math_operation
math_operation = "subtraction"
f_num = int(first_number)
entry_box.delete(0, tk.END)
Function to handle multiplication
def button_multiply():
first_number = entry_box.get()
global f_num
global math_operation
math_operation = "multiplication"
f_num = int(first_number)
entry_box.delete(0, tk.END)
Function to handle division
def button_divide():
first_number = entry_box.get()
global f_num
global math_operation
math_operation = "division"
f_num = int(first_number)
entry_box.delete(0, tk.END)
Function to handle clearing the input
def button_clear():
entry_box.delete(0, tk.END)
result_label.config(text="")
These functions handle button clicks and perform the appropriate calculations or actions. The `button_click` function is called when a digit button is clicked, and it adds the digit to the input box. The other functions are called when the corresponding operator button is clicked. They store the first number entered by the user and the math operation to be performed and clear the input box.
We need to define a function to perform the actual calculation and display the result in the result label. We will call this function button_equal.
# Function to handle equal button
def button_equal():
second_number = entry_box.get()
entry_box.delete(0, tk.END)
if math_operation == "addition":
result = f_num + int(second_number)
elif math_operation == "subtraction":
result = f_num - int(second_number)
elif math_operation == "multiplication":
result = f_num * int(second_number)
elif math_operation == "division":
result = f_num / int(second_number)
result_label.config(text="Result: " + str(result))
This function retrieves the second number entered by the user, clears the input box, and performs the appropriate calculation based on the math operation selected by the user. It then displays the result in the result label.
Finally, we need to create the input box and result label.
# Create the input box and result label
entry_box = tk.Entry(window, width=35, borderwidth=5)
entry_box.grid(row=0, column=0, columnspan=4, padx=10, pady=10)
result_label = tk.Label(window, text="")
result_label.grid(row=1, column=0, columnspan=4)
This code creates an entry box for the user to enter numbers and operators and a label to display the result.
Now that we have defined all the necessary functions and widgets, we can run the application by calling the mainloop method of the Tk object.
window.mainloop()
This code starts the event loop of the application and waits for user input.
When you run the application, you should see a window with a calculator interface. You can enter numbers and operators by clicking the corresponding buttons, and the result will be displayed in the result label when you click the equal button. You can also clear the input by clicking the clear button.
Advantages of Simple GUI Calculator using Tkinter:
- Easy to use: A simple GUI calculator using Tkinter is very user-friendly and easy to use, even for people who are not familiar with programming.
- Customizable: You can easily customize the look and feel of your calculator by changing the font, colour, and layout of the widgets.
- Interactive: The graphical user interface allows users to interact with the calculator by clicking on buttons and seeing the results immediately.
- Portable: Since Tkinter is a built-in module in Python, the calculator can be easily ported to any platform that supports Python.
- Good learning experience: Creating a simple GUI calculator using Tkinter is a great way to learn the basics of GUI programming in Python.
Disadvantages of Simple GUI Calculator using Tkinter:
- Limited functionality: A simple GUI calculator using Tkinter has limited functionality and can only perform basic arithmetic operations.
- Design limitations: The design of the calculator may be limited by the capabilities of Tkinter and may not allow for more complex layouts or widgets.
- Complex functions: Implementing more complex functions, such as trigonometric functions or logarithms, can be challenging with Tkinter.
- Code complexity: The code for a simple GUI calculator using Tkinter can become complex and difficult to maintain as the number of features and functionality increases.
- Not suitable for complex applications: While Tkinter is a good choice for simple applications, it may not be suitable for more complex applications that require more advanced features.
Overall, creating a simple GUI calculator using Tkinter is a good way to learn the basics of GUI programming in Python. Once you understand the principles involved, you can extend this code to create more complex applications with more advanced features.