Exercise 198

Create a graphical application with Python Tkinter which tests whether a given integer is a perfect square or not as shown in the figure below:

Solution


from tkinter import *

def action():
N = int(entryNumber.get())
# create a counter to test if the number is perfect square
j = 0
for i in range(0,N):
if(i**2 == N):
j = i
break
if j > 0:
lblDisplay['text'] = str(N) + " is perfect square " + str(N) + " = " + str(j) + " ^ 2"
else :
lblDisplay['text'] = str(N) + " is not perfect square! "

#lblDisplay['text'] = "Welcome " + name + " !"
# You can also use the following syntax :
#lblDisplay.configure(text = "Welcome " + name + " !")

root = Tk()
root.geometry("400x170")

# Create Label and Entry Name
lblNumber = Label(root , text= "Enter integer N : " , width = 20 )
lblNumber.place(x = 10 , y = 20)
entryNumber = Entry(root)
entryNumber.place(x = 175 , y = 20 , width = 175)

# Create label to display result
lblDisplay = Label(root , text='...........')
lblDisplay.place(x = 175 , y = 120)

# Create button to execute action
btnAction = Button(root , text = "Validate" , command = action)
btnAction.place(x = 175 , y = 70 , width = 175)

root.mainloop()


Younes Derfoufi
my-courses.net

Leave a Reply