The error i am getting is
search_box.insert(0,'Enter id') NameError: name 'search_box' is not defined
the username is a and password is a
i tried making the variable search_box
global but it did not worked
when i click on voice search button
def id():
search_box.insert(0,'Enter id')
this function doesnot work and the name error shows up help to fix it
This is the Code
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()```
CodePudding user response:
The problem can be fixed by defining the search_box
as global within the page()
function.
global w2, search_box # Added global definition for search_box
And by shifting the id()
function below the page function, it seems to be working as intended now.
The full code with these changes in place looks like -:
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()
NOTE: Also, as id
is an existing keyword in python eventhough its not throwing any errors yet, it is suggested you change the name of your function perhaps to id_
or some other name, as id is also a built-in function, and defining another id function, overrides the built-in id
function, which may or may not cause errors down the development process. Even if there is no error it is generally a good practice to make the code more readable.