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:

calculate area and perimeter in tkinter
Python Code:
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 |
# -*- coding: utf-8 -*- import tkinter as tk def calculate(): if valRadio.get() == 1: res = (int(e1.get()) + int(e2.get())) * 2 #Perimeter elif valRadio.get() == 2: res = int(e1.get()) * int(e2.get()) #Area else: res ="check radio button" myText.set(res) root = tk.Tk() root.title("Code4Example.com") # Add a title valRadio = tk.IntVar() myText=tk.StringVar() e1 =tk.StringVar() e2 =tk.StringVar() tk.Label(root, text="""Code4Example Calculator""").grid(row = 0, column = 0) tk.Label( text="Length :" ).grid(row=1,column = 0) tk.Label( text="Width :").grid(row=2,column=0) result=tk.Label(text="(result)", textvariable=myText).grid(row=5,column=0) r1 = tk.Radiobutton(text="Perimeter", variable=valRadio, value=1).grid(row=3, column=0) r2 = tk.Radiobutton(text="Area", variable=valRadio, value=2).grid(row=3, column=1) tk.Entry(textvariable = e1).grid(row=1, column=1) tk.Entry(textvariable = e2).grid(row=2, column=1) b = tk.Button(text="Calculate", command=calculate).grid(row=1, column=3) root.mainloop() |
Source:
https://docs.python.org/3/library/tkinter.html
[…] You May Also Like: Python Program to Calculate Area and Perimeter of Rectangle in Tkinter […]