我正在从youtube视频中学习tkinter,这是代码:
import tkinter as tk
def click():
entered_text = textentry.get()
output.delete(0.0, tk.END)
try:
definition = my_compdictionary[entered_text]
except:
definition = "sorry there is no word like that please try again"
output.insert(tk.END, definition)
# main
window = tk.Tk()
window.title("My Computer Science Glossary")
window.configure(background="black")
# adding photo
photo1 = tk.PhotoImage(file="plusimg.png")
a = tk.Label(window, image=photo1, bg="black")
a.grid(row=0, column=0, sticky=tk.W)
# create label/text
lab = tk.Label(window, text="Enter the word you would like a definition for:", bg="black",
fg="white", font="none 12 bold").grid(row=1, column=0, sticky=tk.W)
# text entry box
textentry = tk.Entry(window, width=20, bg="white")
textentry.grid(row=2, column=0, sticky=tk.W)
# button
tk.Button(window, text="SUBMIT", width=6).grid(row=3, column=0, command=click(), sticky=tk.W)
# another label
tk.Label(window, text="\nDefinition:", bg="black", fg="white", font="none 12 bold").grid(row=4, column=0, sticky=tk.W)
# output text box
output = tk.Text(window, width=75, height=6, wrap=tk.WORD, background="white")
output.grid(row=5, column=0, columnspan=2, sticky=tk.W)
# the dictionary
my_compdictionary = {
'algorithm': 'Step by step instructions to complete a task', 'bug': 'piece of code that causes a program to fail'
}
window.mainloop()
该问题似乎是在click()
函数中发生的。我得到的错误是“输出”未定义
错误消息也会打印此行,但没有说明出什么毛病:
tk.Button(window, text="SUBMIT", width=6).grid(row=3, column=0, command=click(), sticky=tk.W)
该程序假定允许我输入一个单词,并且将在my_compdictionary中设置该单词时为其提供定义