In this tutorial, we will learn how to create a simple GUI calculator using Python. We will be using the tkinter library, which is a built-in library in Python for creating graphical user interfaces.
Step 1: Import the necessary libraries
First, we need to import the tkinter library and the messagebox module that we will use to display error messages.
from tkinter import *
from tkinter import messagebox
Step 2: Create the main window
Next, we will create the main window for our calculator using the Tk class from tkinter.
# Create the main window
root = Tk()
root.title("Simple Calculator")
Step 3: Create the display field
Now, we will create a text box to display the numbers and the result of the calculations.
display = Entry(root, width=20, borderwidth=5)
display.grid(row=0, column=0, columnspan=4)
Step 4: Define the button functions
Next, we will define the functions for the different calculator operations such as addition, subtraction, multiplication, and division.
def button_click(number):
current = display.get()
display.delete(0, END)
display.insert(0, str(current) + str(number)
def button_clear():
display.delete(0, END)
def button_add():
first_number = display.get()
global f_num
f_num = int(first_number)
display.delete(0, END)
def button_equal():
second_number = display.get()
display.delete(0, END)
result = f_num + int(second_number)
display.insert(0, result)
Step 5: Create the calculator buttons
Now, we will create the buttons for the numbers, operators, and the equals sign.
# Create the number buttons
button_1 = Button(root, text='1', command=lambda: button_click(1))
button_2 = Button(root, text='2', command=lambda: button_click(2))
button_3 = Button(root, text='3', command=lambda: button_click(3))
button_4 = Button(root, text='4', command=lambda: button_click(4))
button_5 = Button(root, text='5', command=lambda: button_click(5))
button_6 = Button(root, text='6', command=lambda: button_click(6))
button_7 = Button(root, text='7', command=lambda: button_click(7))
button_8 = Button(root, text='8', command=lambda: button_click(8))
button_9 = Button(root, text='9', command=lambda: button_click(9))
button_0 = Button(root, text='0', command=lambda: button_click(0))
# Create the operator buttons
button_add = Button(root, text='+', command=button_add)
button_equal = Button(root, text='=', command=button_equal)
button_clear = Button(root, text='C', command=button_clear)
Step 6: Display the buttons on the window
Finally, we will use the grid method to display the buttons on the calculator window.
# Display the number buttons
button_1.grid(row=1, column=0)
button_2.grid(row=1, column=1)
button_3.grid(row=1, column=2)
button_4.grid(row=2, column=0)
button_5.grid(row=2, column=1)
button_6.grid(row=2, column=2)
button_7.grid(row=3, column=0)
button_8.grid(row=3, column=1)
button_9.grid(row=3, column=2)
button_0.grid(row=4, column=0)
# Display the operator buttons
button_add.grid(row=1, column=3)
button_equal.grid(row=4, column=2)
button_clear.grid(row=4, column=1)
Step 7: Run the main loop
Finally, we will run the main loop for the calculator window.
root.mainloop()
That’s it! You have now created a simple GUI calculator in Python using tkinter. Feel free to play around with the code and add more functionality to the calculator, such as additional operators or advanced calculations.
Really good tutorial, has helped improve my Python knowledge 😊
This is what i was looking for. Thanks to tutor.
Can someone send me the code
There is problem
Text is not defined
bro thank you very much. you made my day. keep up
I can create a calculator without gui.
Creating a gui is way harder, its different.
Can anyone explain to me how he navigates the code by highlighting a single character then just skimming it? I'm a bit new to coding and am still learning much. – Thanks!
Are u German? I think i saw a "ö" or is this also on english keyboards .-.
when you put root = tk.Tk() it worked perfectly for you but for me it's says AttributeError: module 'tkinter' has no attribute 'tk'
How do you do the copy paste trick??? damn. It seems like it's a shortcut key or somethin' it was so quick ahahahhah
When i click the number are error, can you repair my code?
thank you very much
Thank you sir ❤
5:20
Thanks for beautiful guidens
Minus botton not show
do you need pycharm for this, or can you use the normal python application?
Tysm❤❤😊
How can I add percentage button in this program
everything worked but the buttons only work individually instead of 1+2 it would just say the number by itself when clicked instead of holding memory. any tips or advice