import tkinter as tk
CENTER=tk.CENTER
NW=tk.NW
root=tk.Tk()
canvas1 = tk.Canvas(root, width = 450, height = 500)
canvas1.pack()
#Function to be called when button is clicked
def getImage(t_entry):
if(t_entry.get().lower()=="Hello"):
photo=tk.PhotoImage(file="C:\\Users\Alpha\PycharmProjects\Challenge1\Assets\Image2.jpg")
canvas1.create_image(225,210, anchor=NW, image=photo)
elif(t_entry.get().lower()=="My Name is"):
photo=PhotoImage(file="C:\\Users\Alpha\PycharmProjects\Challenge1\Assets\Image1.png")
canvas1.create_image(225,210, anchor=NW, image=photo)
label1 = tk.Label(root, text='Text to Sign Language Interpretation')
label1.place(relx=.5,rely=.5,anchor=CENTER)
label1.config(font=('helvetica', 14))
canvas1.create_window(225, 25, window=label1)
label2 = tk.Label(root, text='Type Your Text:')
label2.place(relx=.5,rely=.5,anchor=CENTER)
label2.config(font=('helvetica', 14))
canvas1.create_window(225, 100, window=label2)
#Entry for user to enter text they want displayed in sign langauage
entry1 = tk.Entry (root)
canvas1.create_window(225, 140, window=entry1)
#Button that Displays Sign Language Image
button1 = tk.Button(text='Show Sign Language', command=getImage(entry1), bg='grey', fg='white', font=('helvetica', 9, 'bold'))
canvas1.create_window(225, 190,anchor=CENTER, window=button1)
root.mainloop()
The application displays a sign language image when a user enters text assigned to that image. E.g if the user enters "Hello", it displays an image of "hello" in sign language.
CodePudding user response:
There are few issues in your code:
command=getImage(entry1)
will executegetImage()
immediately, not on button click. Usecommand=lambda: getImage(entry1)
insteadt_entry.get().lower()=="Hello"
will always beFalse
as the left side is all lowercase, but the right side is not. Same ont_entry.get().lower()=="My Name is"
. Suggest to change the right side to all lowercase as well.image will be garbage collected as described in this question. You can make
photo
a global variable to fix it.you have create new image item whenever
getImage()
is executed. Better create the image item once and update its image inside the function.
Below is the changed code required to fix the above issues:
...
def getImage(t_entry):
global photo # avoid garbage collection by using global variable
if t_entry.get().lower() == "hello": # changed to all lowercase
photo = tk.PhotoImage(file="C:\\Users\Alpha\PycharmProjects\Challenge1\Assets\Image2.jpg")
canvas1.itemconfigure(image_id, image=photo) # update image item
elif t_entry.get().lower() == "my name is": # change to all lowercase
photo = PhotoImage(file="C:\\Users\Alpha\PycharmProjects\Challenge1\Assets\Image1.png")
canvas1.itemconfigure(image_id, image=photo) # update image item
else:
# clear the image if text cannot be matched to an image
canvas1.itemconfigure(image_id, image='')
...
# used command=lambda: getImage(entry)
button1 = tk.Button(text='Show Sign Language', command=lambda: getImage(entry1), bg='grey', fg='white', font=('helvetica', 9, 'bold'))
canvas1.create_window(225, 190, anchor=CENTER, window=button1)
# create the image item initially without a image
image_id = canvas1.create_image(225, 210, anchor=NW)
root.mainloop()