我試圖編寫圖書館管理系統的程序我正在使用tkinter模塊。我已經寫了下面的代碼,但是當我嘗試創建多個文本框時,我正在下面的錯誤。Tkinter模塊錯誤erroe在calss pageone中創建登錄頁面
File "Hope_work.py", line 22, in __init__
frame = F(container, self)
File "Hope_work.py", line 62, in __init__
pwd_lable.pack()
UnboundLocalError: local variable 'pwd_lable' referenced before assignment
下面是完整的程序,我在PAGEONE類得到錯誤
import tkinter as tk
import os
LARGE_FONT= ("Verdana", 12)
class Myprogramapp(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
container = tk.Frame(self)
container.pack(side="top", fill="both", expand = True)
container.grid_rowconfigure(0, weight=1)
container.grid_columnconfigure(0, weight=1)
self.frames = {}
for F in (StartPage, PageOne):
frame = F(container, self)
self.frames[F] = frame
frame.grid(row=0, column=0, sticky="nsew")
self.show_frame(StartPage)
def show_frame(self, cont):
frame = self.frames[cont]
frame.tkraise()
class StartPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self,parent)
label = tk.Label(self, text="Library Managment System", font=LARGE_FONT)
label.pack(pady=10,padx=10)
button = tk.Button(self, text="Admin Login",
command=lambda: controller.show_frame(PageOne))
button.pack()
button1 = tk.Button(self, text="Lib Login")
button1.pack()
class PageOne(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
name_label = tk.Label(self, text="User ID : ")
pwd_label = tk.Label(self.name_lable, text="Password:")
name_label.pack(pady=10,padx=10)
pwd_lable.pack(pady=10,padx=10)
name_lable = tk.Entry(self)
pwd_lable = tk.Entry(self, show="*")
name_lable.pack()
pwd_lable.pack()
button1 = tk.Button(self, text="Login")
button1.pack()
if __name__ == "__main__":
app = Myprogramapp()
app.mainloop()
**
這裏有幾個問題。因爲拼寫錯誤,縮進一個,標籤名稱的拼寫錯過匹配,您正在創建分配給另一個小部件而不是框架或根窗口的'pwd_label'。你的課程設置很奇怪,我不認爲他們會這樣工作。嘗試將所有內容放到一個類中,這樣會更有意義。對於任何您要使用/更新的小部件,您需要使用'self.'前綴將它們設置爲類屬性。 –
您發佈的代碼不會給出您所說的錯誤。 –
@SierraMountainTech:代碼設計直接或間接來自http://stackoverflow.com/questions/7546050。不幸的是,有人在不理解它的情況下複製它,使用它創建了一個視頻教程,但沒有正確地解釋它,現在很多初學者都使用它作爲開始模板。 _sigh_ –