PYTHON please check simple code The GUI has three widgets: a label, an entry, an
ID: 3678048 • Letter: P
Question
PYTHON please check simple code
The GUI has three widgets: a label, an entry, and a button. The label prompts the user to enter a word. These can be any combination of lower and upper case alphabetical characters. When the user enters a word into the entry and presses the button, the GUI tests whether the word is a palindrome and displays a dialog box indicating the result. When the user dismisses the dialog box, the GUI clears the entry and goes back to waiting for the next interaction with the user.
The constructor for the GUI is provided in the exam template file. It initializes a Tk window to hold the widgets and calls the make_widgets() method. The make_widgets() method has also been provided. It creates the Label, Button, and Entry and places them into the main window.
Your task for this question is to write the event handler for the Button. The method retrieves whatever the user has typed into the Entry and checks to see if the word is a palindrome. Before it can check, however, it must be sure the word is alphabetical and does not contain any numbers, punctuation or spaces. If the word entered is invalid, it displays a showinfo box reporting that. If the word entered is valid and is a palindrome, it displays a showinfo box reporting that. If the word entered is not a palindrome, it displays a message in a showinfo box reporting that. Regardless of what the user types, the Entry is cleared as the last thing that the event handler does.
When the GUI is complete, it will look like the following when you type PalCheck().mainloop():
If the user types a valid word into the entry that is a palindrome, for example, as follow
If the user types a valid word into the entry that is a palindrome, for example, as follows
and then presses the button, the following pop-up window is displayed:
If the user types a valid integer into the entry that is not a palindrome for example, as follows
and then presses the button, then the following pop-up window is displayed:
If the user types something into the entry that cannot be evaluated as a string, for example, as follows:
and then presses the button, then the following pop-up window is displayed:
In any of the above cases, after the user dismisses the pop-up window, the entry should be cleared as follows:
Simple code
from tkinter import * #Tk, Label, Entry, Button, TOP, LEFT, END
from tkinter.messagebox import showinfo
import sys
class PalCheck(Tk):
def __init__(self, parent=None):
Tk.__init__(self, parent)
self.title('Palindrome Check')
self.make_widgets()
# Define the main function
def make_widgets(self):
Label(self, text="Enter your word:").pack(side=TOP)
self.ent = Entry(self)
self.ent.pack(side=TOP)
self.ent.focus_set()
# Event handler
def cmd():
pass
btn=Button(self, text="Submit", command=cmd)
btn.pack(side=BOTTOM)
Explanation / Answer
we will create a function pallindrome check and check if the entered string is pallindrome or not.
z=raw_input("Please enter the String:")
def palindromecheck(z):
index=0
check=True
while index<len(z):
if z[index]==z[-1-index]:// check if reverse is equal to same string
index+=1
return True
return False
if palindromecheck(z)==True:
print "It is a Palindrome for the given string"
else:
print "No It is not a Palindrome for the given string"
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.