Tkinter

# Python Program to Calculate Area and Perimeter of Rectangle in Tkinter3 min read

In this example. I’ll show how to calculate Area and Perimeter of Rectangle in Tkinter.

## What is Tkinter?

The tkinter package (“Tk interface”) is the standard Python interface to the Tk GUI toolkit. Both Tk and tkinter are available on most Unix platforms, as well as on Windows systems.

## How to Calculate Area and Perimeter

To find the area of a rectangle or a square you need to multiply the length and the width of a rectangle or a square. There are different units for perimeter and area. The perimeter has the same units as the length of the sides of rectangle or square whereas the area’s unit is squared.

You May Also Like: Python Program to Calculate Area and Perimeter of Rectangle

Output:

Python Code:

This is a Python program that creates a simple graphical user interface (GUI) using the Tkinter library. The GUI allows the user to input the length and width of a rectangle and then calculates either the perimeter or the area of the rectangle, depending on the radio button selected by the user.

The program starts by importing the Tkinter library as tk and defines a function called calculate. Inside the function, the program checks the value of the variable valRadio, which is linked to the radio buttons, and performs the corresponding calculation. If valRadio is equal to 1, the program calculates the perimeter of the rectangle by multiplying the sum of the length and width by 2. If valRadio is equal to 2, the program calculates the area of the rectangle by multiplying the length and width. If valRadio is not equal to either 1 or 2, the program sets the result variable to the string “check radio button”.

The program then creates a Tkinter root object and sets the title of the window to “Code4Example.com”. It then creates several Tkinter widgets, including labels, radio buttons, an entry field, and a button. The entry fields are used to input the length and width of the rectangle. The radio buttons are used to select either the perimeter or the area calculation. The button is used to trigger the calculate function when clicked.

The program sets the layout of the widgets using the grid() method, with the row and column arguments specifying the position of each widget in the grid.

Finally, the program calls the mainloop() method on the root object, which starts the GUI event loop and displays the window on the screen.

This program demonstrates how to create a simple GUI using the Tkinter library in Python, that allows user to enter the length and width of a rectangle and then calculates either the perimeter or the area of the rectangle, depending on the radio button selected by the user. The result is displayed on the screen.

Source:

https://docs.python.org/3/library/tkinter.html

### 1 Comment

• […] You May Also Like: Python Program to Calculate Area and Perimeter of Rectangle in Tkinter […]