我得到的錯誤是
search_box.insert(0,'Enter id') NameError: name 'search_box' is not defined
用戶名是a,密碼是a
我嘗試將變數設為search_box全域,但是當我單擊時它不起作用voice search button
定義標識():
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()```
uj5u.com熱心網友回復:
這個問題可以通過在函式中定義search_box為全域來解決。page()
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()
注意:另外,作為idpython 中的現有關鍵字,盡管它還沒有引發任何錯誤,但建議您將函式的名稱更改為id_或其他名稱,因為 id 也是一個內置函式,并定義另一個 id函式,覆寫內置id函式,這可能會也可能不會導致開發程序中的錯誤。即使沒有錯誤,使代碼更具可讀性通常也是一個好習慣。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/435638.html
