It is a simple Python program to add two integers and display their sum. This program is only for learning Python Tkinter.
Here two text boxes will accept two numbers from user and display their sum in another text box.
Here is an example of a Python program that adds two numbers in Tkinter:
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 | import tkinter as tk def add(): num1 = float(num1_entry.get()) num2 = float(num2_entry.get()) result = num1 + num2 result_label.config(text=result) # create the main window root = tk.Tk() root.title("Add Two Numbers") # create the widgets num1_label = tk.Label(root, text="Number 1:") num1_entry = tk.Entry(root) num2_label = tk.Label(root, text="Number 2:") num2_entry = tk.Entry(root) add_button = tk.Button(root, text="Add", command=add) result_label = tk.Label(root, text="Result:") # layout the widgets num1_label.grid(row=0, column=0, sticky="e") num1_entry.grid(row=0, column=1) num2_label.grid(row=1, column=0, sticky="e") num2_entry.grid(row=1, column=1) add_button.grid(row=2, column=0, columnspan=2, pady=10) result_label.grid(row=3, column=0, columnspan=2) # run the main loop root.mainloop() |
Output:

This program creates a simple GUI with two entry fields for the numbers and a button to trigger the addition. When the button is clicked, the add
function is called, which gets the numbers from the entry fields, adds them, and displays the result in a label. The widgets are then laid out using the grid
geometry manager.
Here is a brief explanation of the code:
1 2 3 | import tkinter as tk |
This line imports the Tkinter module, which provides the interface to the Tk GUI toolkit. We import it under the alias tk
for convenience.
1 2 3 4 5 6 7 | def add(): num1 = float(num1_entry.get()) num2 = float(num2_entry.get()) result = num1 + num2 result_label.config(text=result) |
This is the add
function that is called when the “Add” button is clicked. It gets the values of the two entry fields using the get
method, converts them to floats, adds them, and updates the text of the result label with the result.
1 2 3 4 | root = tk.Tk() root.title("Add Two Numbers") |
This creates the main window and sets its title.
1 2 3 4 5 6 7 8 | num1_label = tk.Label(root, text="Number 1:") num1_entry = tk.Entry(root) num2_label = tk.Label(root, text="Number 2:") num2_entry = tk.Entry(root) add_button = tk.Button(root, text="Add", command=add) result_label = tk.Label(root, text="Result:") |
These lines create the widgets for the program. The labels and buttons are created with their respective constructors, and the entry fields are created with the Entry
constructor. The command
option of the button specifies the function to be called when the button is clicked.
1 2 3 4 5 6 7 8 | num1_label.grid(row=0, column=0, sticky="e") num1_entry.grid(row=0, column=1) num2_label.grid(row=1, column=0, sticky="e") num2_entry.grid(row=1, column=1) add_button.grid(row=2, column=0, columnspan=2, pady=10) result_label.grid(row=3, column=0, columnspan=2) |
These lines use the grid
geometry manager to lay out the widgets in a grid. The row
and column
options specify the row and column of the grid that the widget should be placed in, and the columnspan
option specifies how many columns the widget should span. The sticky
option specifies how the widget should be aligned within its cell, and the pady
option adds some padding to the bottom of the button.
1 2 3 | root.mainloop() |
This starts the main loop of the program, which listens for events such as button clicks and updates the GUI accordingly. This is what makes the GUI interactive.
I hope this helps to clarify things!
You also like this : Simple Calculator Example in Python 3 Tkinter
Alternative Example:
Interface:

Code Behind Interface(python v3.8):
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 | from tkinter import * # Function to add two numbers def addNumbers(): # Get values from Entry widgets, convert to integers, and calculate the sum result = int(entry1.get()) + int(entry2.get()) # Set the result in the StringVar myText.set(result) # Create the main Tkinter window master = Tk() # Create a StringVar to store the result myText = StringVar() # Create and place labels for input and result Label(master, text="First").grid(row=0, sticky=W) Label(master, text="Second").grid(row=1, sticky=W) Label(master, text="Result:").grid(row=3, sticky=W) # Create a Label to display the result using the StringVar result_label = Label(master, text="", textvariable=myText) result_label.grid(row=3, column=1, sticky=W) # Create Entry widgets for user input entry1 = Entry(master) entry2 = Entry(master) # Place Entry widgets in the grid entry1.grid(row=0, column=1) entry2.grid(row=1, column=1) # Create a Button to trigger the calculation calculate_button = Button(master, text="Calculate", command=addNumbers) calculate_button.grid(row=0, column=2, columnspan=2, rowspan=2, sticky=W+E+N+S, padx=5, pady=5) # Start the Tkinter event loop mainloop() |
This code creates a simple graphical user interface (GUI) in Python using the Tkinter library.
The GUI consists of two Entry widgets (e1
and e2
) that allow the user to input numbers, a Button widget (b
) that runs a function when clicked, and a Label widget (result
) that displays the result of the function.
When the Button is clicked, the addNumbers
function is called. This function gets the text from the Entry widgets, converts them to integers, and adds them together. The result is then displayed in the Label widget by setting the textvariable to the result.
Finally, the mainloop
function is called to start the Tkinter event loop, which handles all the events (like button clicks) and updates the GUI as necessary.
I get following error when I click Button for calculate
AttributeError: ‘NoneType’ object has no attribute ‘get’
@anky Can you share more details on error, also snip of code you ran?
Worked really flawless, super example to begin GUI learning with Python Tk.
One thing to note, should compile with python 3.* versions as 2.* versions have Tkinter with cap “T” and 3.* versions have tkinter with small “t”.
Keep posting more stuff, on complicated GUI’s with Tk.
Thanks,
[…] You also like this :Python Program to Add Two Numbers in Tkinter […]