# Simple Calculator Example in Python 3 Tkinter8 min read

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

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:

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:

This line imports the `tkinter` library and renames it as `tk` for convenience.

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.

This creates the main window of the calculator, and sets its title to “Calculator”.

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.

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.

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.

### 1 Comment

• […] You also like this : Simple Calculator Example in Python 3 Tkinter […]