Tuesday, July 5, 2022

Exercises to Python GUI Programming

Tkinter - GUI Programming Python

Question 1


Write a Python GUI Program to find the Area of a Rectancle with the following features. Able to enter width and height of a rectangle by the users.

  • Proper Prompted Messages to user for the above input.
  • Button to calculate and display the area of the rectangle for the above input.
  • Font should be Helvetica and size 15

Answer - Sample Python GUI Program

from tkinter import *
from tkinter.font import *

def myclick():
    A=int(W.get())*int(H.get())
    label3.configure(text="Area of the Rectangle :" +str(A))

root = Tk(className='Python Examples - Find Area of a Rectangle')
myfont=Font(family='Helvetica', size=15, weight='bold' )

label1 =Label(text="Enter the Width of a Rectangle", width=40,font=myfont, fg='Red')
label2 = Label(text="Enter the Height of a Rectangle", width=40, font=myfont, fg='Red')
label3 = Label(text=" ", width=40, font=myfont, fg='Green')

W=Entry(font=myfont, width=4)
H=Entry(font=myfont, width=4)

button = Button(text = 'Click me', font=myfont,command = myclick)

label1.grid(row=0, column=0)
W.grid(row=0, column=1)
label2.grid(row=1, column=0)
H.grid(row=1, column=1)
button.grid(row=2, column=0,columnspan=2)
label3.grid(row=5,column=0)

root.geometry("600x500")

mainloop()

Sample Output Screen


Python GUI with Tkinter

No comments:

Post a Comment