A simple python program to add, subtract, divide or multiply two integers. There ara two Entry, 4 RadioButtons and one Button to calculate the result. There are also Labels for provide information.
Example 1: This code is a simple calculator that allows the user to perform basic arithmetic operations (addition, subtraction, multiplication, and division) on two numbers by entering them in the input fields and selecting the desired operation using radio buttons. When the user clicks the “Calculate” button, the function calculate
is called, which uses an if-else statement to determine which operation to perform based on the value of the valRadio
variable, which is set by the radio buttons. The result is then displayed in a label widget.
Interface:
Python Code: Calculator in Python 3 Using 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 33 34 35 36 37 38 39 40 41 42 43 44 |
import tkinter as tk def calculate(): if valRadio.get() == 1: res=int(e1.get())+int(e2.get()) elif valRadio.get() == 2: res=int(e1.get())-int(e2.get()) elif valRadio.get() == 3: res=int(e1.get())*int(e2.get()) elif valRadio.get() == 4: res=int(e1.get())/int(e2.get()) else: res ="check radio button" myText.set(res) root = tk.Tk() valRadio = tk.IntVar() myText=tk.StringVar() e1 =tk.StringVar() e2 =tk.StringVar() tk.Label(root, text="""Simple Calculator""").grid(row = 0, column = 0) tk.Label( text="First" ).grid(row=1,column = 0) tk.Label( text="Second").grid(row=2,column=0) result=tk.Label(text="(result)", textvariable=myText).grid(row=5,column=0) tk.Entry(textvariable = e1).grid(row=1, column=1) tk.Entry(textvariable = e2).grid(row=2, column=1) r1 = tk.Radiobutton(text="Add(+)",variable=valRadio, value=1).grid(row=3, column=0) r2 = tk.Radiobutton(text="Sub(-)",variable=valRadio, value=2).grid(row=3, column=1) r3 = tk.Radiobutton(text="Mul(*)",variable=valRadio, value=3).grid(row=4, column=0) r4 = tk.Radiobutton(text="Div(/)",variable=valRadio, value=4).grid(row=4, column=1) b = tk.Button(text="Calculate", command=calculate).grid(row=1, column=3) root.mainloop()  |
The first line import tkinter as tk
imports the tkinter library, which is a library for creating graphical user interfaces (GUIs) in Python.
The calculate
function is called when the user clicks the “Calculate” button. It uses an if-else statement to determine which operation to perform based on the value of the valRadio
variable, which is set by the radio buttons. The valRadio
variable is an instance of the IntVar
class, which is a Tkinter variable class that holds an integer value. The IntVar
class is used to associate the radio buttons with a variable and to get the value of the selected radio button.
If the value of valRadio
is 1, the function performs addition. If the value of valRadio
is 2, the function performs subtraction. If the value of valRadio
is 3, the function performs multiplication. If the value of valRadio
is 4, the function performs division. If the value of valRadio
is none of these values, the function sets the result to the string “check radio button”.
The input fields for the two numbers are created using the Entry
class, which creates a single-line text field where the user can enter a value. The StringVar
class is used to create variables that are associated with the input fields and to get the values entered by the user.
The radio buttons are created using the Radiobutton
class. Each radio button is associated with the valRadio
variable and is given a unique value (1, 2, 3, or 4) to determine which operation to perform.
The “Calculate” button is created using the Button
class and is associated with the calculate
function. When the button is clicked, the calculate
function is called.
The result is displayed in a label widget using the Label
class and the textvariable
option, which is set to the myText
variable. The myText
variable is an instance of the StringVar
class, which is used to set the text of the label.
Finally, the mainloop
method is
called to start the event loop, which listens for user input and redraws the GUI as necessary. This is what allows the calculator to function properly.
You also like this :Python Program to Add Two Numbers in Tkinter
Example 1: Here is an example of a simple calculator program in Python 3 using 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 33 34 35 36 37 38 39 40 41 42 43 |
import tkinter as tk def calculate(): num1 = float(num1_entry.get()) num2 = float(num2_entry.get()) if operator.get() == "+": result = num1 + num2 elif operator.get() == "-": result = num1 - num2 elif operator.get() == "*": result = num1 * num2 elif operator.get() == "/": result = num1 / num2 result_label.config(text=result) # create the main window root = tk.Tk() root.title("Calculator") # 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) operator = tk.StringVar() operator.set("+") # default operator operator_menu = tk.OptionMenu(root, operator, "+", "-", "*", "/") calculate_button = tk.Button(root, text="Calculate", command=calculate) 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) operator_menu.grid(row=2, column=0, columnspan=2) calculate_button.grid(row=3, column=0, columnspan=2, pady=10) result_label.grid(row=4, column=0, columnspan=2) # run the main loop root.mainloop() |
This program creates a simple calculator GUI with two entry fields for the numbers, an option menu for selecting the operator, and a button to trigger the calculation. When the button is clicked, the calculate
function is called, which gets the numbers and operator from the entry fields and option menu, performs the appropriate operation, and displays the result in a label. The widgets are then laid out using the grid
geometry manager.
Here is a more detailed explanation of the code:
1 2 3 |
import tkinter as tk |
This line imports the tkinter
library and renames it as tk
for convenience.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
def calculate(): num1 = float(num1_entry.get()) num2 = float(num2_entry.get()) if operator.get() == "+": result = num1 + num2 elif operator.get() == "-": result = num1 - num2 elif operator.get() == "*": result = num1 * num2 elif operator.get() == "/": result = num1 / num2 result_label.config(text=result) |
This function is called when the “Calculate” button is clicked. It retrieves the two numbers that the user entered in the text entry fields, converts them to floating point numbers (since they are initially treated as strings), and stores them in the variables num1
and num2
. It then determines the operation to be performed based on the value of the operator
variable (which is set by the dropdown menu), and calculates the result. Finally, it updates the text of the result_label
widget to display the result.
1 2 3 4 5 |
# create the main window root = tk.Tk() root.title("Calculator") |
This creates the main window of the calculator, and sets its title to “Calculator”.
1 2 3 4 5 6 7 8 9 10 11 12 |
# 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) operator = tk.StringVar() operator.set("+") # default operator operator_menu = tk.OptionMenu(root, operator, "+", "-", "*", "/") calculate_button = tk.Button(root, text="Calculate", command=calculate) result_label = tk.Label(root, text="Result:") |
This code creates the various widgets that make up the calculator’s user interface. The Label
widgets are used to display text, the Entry
widgets are used to allow the user to enter text, the OptionMenu
widget is used to create a dropdown menu, and the Button
widget is used to create a button that the user can click. The StringVar
object is used to store the selected operator, and the command
argument of the Button
widget specifies the function to be called when the button is clicked.
1 2 3 4 5 6 7 8 9 10 |
# 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) operator_menu.grid(row=2, column=0, columnspan=2) calculate_button.grid(row=3, column=0, columnspan=2, pady=10) result_label.grid(row=4, column=0, columnspan=2) |
The grid()
function is used to specify the layout of the widgets in the window. It takes a number of arguments that control the widget’s position and size in the window.
The row
and column
arguments specify the row and column numbers of the cell in which the widget should be placed. Rows and columns are numbered starting from 0.
The sticky
argument is used to specify how the widget should be aligned within its cell. The value “e” stands for “east”, so in this case the widget will be aligned to the right side of the cell.
The columnspan
argument is used to specify how many columns the widget should span. In this case, the operator_menu
and result_label
widgets span both columns.
The pady
argument is used to specify the amount of padding to add above and below the widget. This adds some space between the calculate_button
and the other widgets.
1 2 3 4 |
# run the main loop root.mainloop() |
This runs an infinite loop that waits for the user to interact with the window, and handles the events that are triggered as a result. The mainloop()
function is called at the end of the code to start the loop. When the user closes the window, the loop will terminate and the program will end.
[…] You also like this : Simple Calculator Example in Python 3 Tkinter […]