如何使用 Tkinter return 对特定输入发出警报?

How to return an alert for a specific input using Tkinter?

我使用 Tkinter 创建了一个模拟搜索引擎。我希望用户在搜索引擎中输入“测试”一词并点击提交。这应该 return 一个警报。如果用户插入“测试”一词以外的任何内容,那么我希望搜索引擎 return 什么都没有。我已经为模拟搜索引擎创建了界面,但接受用户输入的部分无法正常工作。下面是我的代码:

import tkinter as tk
root = tk.Tk()
canvas1=tk.Canvas(root,width=400,height=300,relief='raised')
canvas1.pack()
label1 = tk.Label(root,text='LookUp')
label1.config(fg='blue',font=('times',30,'bold'))
canvas1.create_window(200,100,window=label1)
entry1 = tk.Entry (root)
canvas1.create_window(200,140,window=entry1)

def values():
     userinput = tk.StringVar(entry1.get())
     if userinput == 'test':
             Output = ('Alert Executed')
             label_Output = tk.Label(root,text=Alert,bg='red')
             canvas1.create_window(270,200,window=label_Output)                     

     else:
             Output = ('')
             label_Output = tk.Label(root,text= Alert)
             canvas1.create_window(270,200,window=label_Output)

button1=tk.Button(root,text='Search',command=values,bg='green',fg='white')
canvas1.create_window(200,180,window=button1)

root.mainloop()

userinput变量需要引用entry1.get()

label_Output 需要将您的 Output 变量作为其 text

import tkinter as tk
root = tk.Tk()
canvas1=tk.Canvas(root,width=400,height=300,relief='raised')
canvas1.pack()
label1 = tk.Label(root,text='LookUp')
label1.config(fg='blue',font=('times',30,'bold'))
canvas1.create_window(200,100,window=label1)
entry1 = tk.Entry (root)
canvas1.create_window(200,140,window=entry1)

def values():
     userinput = entry1.get()
     if userinput == 'test':
             Output = ('Alert Executed')
             label_Output = tk.Label(root,text=Output,bg='red')
             canvas1.create_window(270,200,window=label_Output)                     

     else:
             Output = ('')
             label_Output = tk.Label(root,text= Output)
             canvas1.create_window(270,200,window=label_Output)

button1=tk.Button(root,text='Search',command=values,bg='green',fg='white')
canvas1.create_window(200,180,window=button1)

root.mainloop()