我正在使用 tkinter 创建这个简单的应用程序,但按钮不起作用。我不知道可能是什么问题
I'm creating this simple app using tkinter but the button won't work. I don't know what could be the issue
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()
当用户输入分配给图像的文本时,该应用程序会显示手语图像。例如,如果用户输入“你好”,它会用手语显示“你好”的图像。
您的代码中有几个问题:
command=getImage(entry1)
将立即执行 getImage()
,而不是在单击按钮时执行。使用 command=lambda: getImage(entry1)
代替
t_entry.get().lower()=="Hello"
将始终是 False
,因为左侧全部是小写,但右侧不是。 t_entry.get().lower()=="My Name is"
也一样。建议把右边也改成全小写
图像将按照 question 中的描述进行垃圾回收。您可以将 photo
设为全局变量来修复它。
每当执行 getImage()
时,您就创建了新的图像项目。最好创建一次图像项并在函数内更新其图像。
以下是修复上述问题所需的更改代码:
...
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()
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()
当用户输入分配给图像的文本时,该应用程序会显示手语图像。例如,如果用户输入“你好”,它会用手语显示“你好”的图像。
您的代码中有几个问题:
command=getImage(entry1)
将立即执行getImage()
,而不是在单击按钮时执行。使用command=lambda: getImage(entry1)
代替t_entry.get().lower()=="Hello"
将始终是False
,因为左侧全部是小写,但右侧不是。t_entry.get().lower()=="My Name is"
也一样。建议把右边也改成全小写图像将按照 question 中的描述进行垃圾回收。您可以将
photo
设为全局变量来修复它。每当执行
getImage()
时,您就创建了新的图像项目。最好创建一次图像项并在函数内更新其图像。
以下是修复上述问题所需的更改代码:
...
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()