在使用命令从按钮调用函数以使用函数插入条目小部件时使用 Tkinter,它表示未定义条目变量

Using Tkinter while calling a function from button using commad to insert in entry widget suing function , it says entry variable is not defined

我得到的错误是

search_box.insert(0,'Enter id') NameError: name 'search_box' is not defined

用户名是a,密码是a 我尝试将变量 search_box 设置为全局变量,但没有成功 当我点击 voice search button

def id():

search_box.insert(0,'Enter id') 

此功能无效,出现名称错误 帮助修复它

这是代码

from tkinter import *
from tkinter import messagebox

root=Tk()
root.geometry('1000x700+122+1')
root.configure(bg='#8644f4')

def id():

    search_box.insert(0,'Enter id') #here is the problem , the error says search_box is not defined



#page after login
def page():
    global w2
    w2=Tk()
    w2.geometry('1000x700+122+1')
    w2.configure(bg='#8644f4')
    # search and register for patient
    search_PID = Label(w2, text="Patient ID", bg='#8644f4')
    search_PID.place(x=10, y=20)

    search_box =Entry(w2)
    search_box.place(x=70, y=20)

    getinfo = Button(w2, text='Search')
    getinfo.place(x=100, y=50)

    voice_button = Button(w2, text='Voice Search ', command=id)
    voice_button.place(x=85, y=90)

    register_button = Button(w2, text='Register')
    register_button.place(x=95, y=130)



    w2.title('Find And Register Patient Information')
    w2.mainloop()




def logcheck():
    uid = str(user_entry.get())
    psw=str(pw_entry.get())

    if uid =='a':
        if psw=='a':
            messagebox.showinfo('cool', 'login successful')
            root.destroy()
            page()



        else:
            messagebox.showinfo('Error', 'Password Wrong')

    elif uid=='' and psw=='':
        messagebox.showinfo('Error',"All Field are Required")

    else:
        messagebox.showinfo('Error', 'ID and Password Wrong')






#loginframe
logf=Frame(root,bg='#8081CD')
logf.place(x=250,y=150, height=340, width=500)

logintitle=Label(logf,text='Log in Panel',font=('impact',35,'bold'),bg='#8081CD', fg='darkblue')
logintitle.place(x=119,y=1)

username=Label(logf, text='Username',bg='lightblue')
username.place(x=135,y=110)
user_entry=Entry(logf)
user_entry.place(x=210,y=110)

password=Label(logf, text='Password',bg='lightblue')
password.place(x=135,y=140)

pw_entry=Entry(logf, bg='lightgray',show='*')
pw_entry.place(x=210,y=141)

login=Button(logf,text='Log in',bg='#8644f4',command=logcheck, cursor='hand2')
login.place(x=210,y=180)


root.title('Patient Information')
root.mainloop()```

可以通过在 page() 函数中将 search_box 定义为全局来解决此问题。

global w2, search_box # Added global definition for search_box

并且通过将 id() 函数移动到页面函数下方,它现在似乎可以正常工作了。

具有这些更改的完整代码如下 -:

from tkinter import *
from tkinter import messagebox

root=Tk()
root.geometry('1000x700+122+1')
root.configure(bg='#8644f4')

#page after login
def page():
    global w2, search_box # Added global definition for search_box
    w2=Tk()
    w2.geometry('1000x700+122+1')
    w2.configure(bg='#8644f4')
    # search and register for patient
    search_PID = Label(w2, text="Patient ID", bg='#8644f4')
    search_PID.place(x=10, y=20)

    search_box =Entry(w2)
    search_box.place(x=70, y=20)

    getinfo = Button(w2, text='Search')
    getinfo.place(x=100, y=50)

    voice_button = Button(w2, text='Voice Search ', command=id)
    voice_button.place(x=85, y=90)

    register_button = Button(w2, text='Register')
    register_button.place(x=95, y=130)



    w2.title('Find And Register Patient Information')
    w2.mainloop()

# shifted this function below
def id():
    global search_box
    search_box.insert(0,'Enter id') #here is the problem , the error says search_box is not defined


def logcheck():
    uid = str(user_entry.get())
    psw=str(pw_entry.get())

    if uid =='a':
        if psw=='a':
            messagebox.showinfo('cool', 'login successful')
            root.destroy()
            page()



        else:
            messagebox.showinfo('Error', 'Password Wrong')

    elif uid=='' and psw=='':
        messagebox.showinfo('Error',"All Field are Required")

    else:
        messagebox.showinfo('Error', 'ID and Password Wrong')

#loginframe
logf=Frame(root,bg='#8081CD')
logf.place(x=250,y=150, height=340, width=500)

logintitle=Label(logf,text='Log in Panel',font=('impact',35,'bold'),bg='#8081CD', fg='darkblue')
logintitle.place(x=119,y=1)

username=Label(logf, text='Username',bg='lightblue')
username.place(x=135,y=110)
user_entry=Entry(logf)
user_entry.place(x=210,y=110)

password=Label(logf, text='Password',bg='lightblue')
password.place(x=135,y=140)

pw_entry=Entry(logf, bg='lightgray',show='*')
pw_entry.place(x=210,y=141)

login=Button(logf,text='Log in',bg='#8644f4',command=logcheck, cursor='hand2')
login.place(x=210,y=180)


root.title('Patient Information')
root.mainloop()

注意: 此外,由于 id 是 python 中的现有关键字,尽管它还没有抛出任何错误,但建议您更改名称您的函数的名称可能为 id_ 或其他名称,因为 id 也是一个 built-in 函数,并且定义另一个 id 函数会覆盖 built-in id 函数,该函数可能或可能不会导致开发过程出错。即使没有错误,使代码更具可读性通常也是一个好习惯。