1
我正在嘗試製作一個即時消息程序,該程序在網絡驅動器上工作,並使用@Bryan Oakley的某些代碼在tkinter幀之間進行切換。第一幀應該是保存在txt文件中的用戶名的條目。如果我的程序檢測到用戶名已經保存一次,我想跳過這個框架。但是,當我嘗試這樣做時出現以下錯誤。當滿足某些條件時無法顯示另一個tkinter幀
Traceback (most recent call last):
File "C:\Users\Douglas Rouse\Google Drive\Python\New structure.py", line 260, in <module>
app = ChatApp()
File "C:\Users\Douglas Rouse\Google Drive\Python\New structure.py", line 73, in __init__
frame = StartPage(self.container, self)
File "C:\Users\Douglas Rouse\Google Drive\Python\New structure.py", line 129, in __init__
self.check_user()
File "C:\Users\Douglas Rouse\Google Drive\Python\New structure.py", line 144, in check_user
ChatApp.show_frame(ChatApp,[PageOne])
File "C:\Users\Douglas Rouse\Google Drive\Python\New structure.py", line 106, in show_frame
self.frame = self.frames[cont]
AttributeError: type object 'ChatApp' has no attribute 'frames'
我是相當新的Python那麼,究竟我錯在哪裏,將不勝感激的解釋!
class ChatApp(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
tk.Tk.wm_title(self,"Messenger")
self.container = tk.Frame(self)
self.container.grid()
self.container.grid_rowconfigure(0, weight=1)
self.container.grid_columnconfigure(0, weight=1)
self.frames = {}
frame = StartPage(self.container, self)
self.frame_ = PageOne(self.container, self)
frame1 = BanPage(self.container, self)
self.frames[StartPage] = frame
self.frames[PageOne] = self.frame_
self.frames[BanPage] = frame1
frame.grid(row=0, column=0, sticky="nsew")
self.frame_.grid(row=0, column=0, sticky="nsew")
self.show_frame(StartPage)
#ban()
def show_frame(self, cont):
self.frame = self.frames[cont]
self.frame.tkraise()
class StartPage(tk.Frame):
def __init__(self, parent, controller,*args):
tk.Frame.__init__(self, parent)
usrlabel = ttk.Label(self, text="Input Username", font=LARGE_FONT)
usrlabel.grid(pady=10,padx=10)
self.usrentry = ttk.Entry(self)
self.usrentry.grid()
StartPage.uu = self.usrentry.get()
#command within button cant throw args to funcs. Use lambda to throw those args to the func instead
button1 = ttk.Button(self, text="Visit Page 1",command=
lambda: controller.show_frame(PageOne))
button1.grid()
Button2 = ttk.Button(self, text = "test",command = self.save_user)
Button2.grid()
self.check_user()
def save_user(self,*args):
usr = open("user.txt","a")
self.public_usr = open("publicusr.txt","a")
self.public_usr.write(self.usrentry.get())
usr.write(self.usrentry.get())
self.usrentry.delete(0, 'end')
def check_user(self):
usrcount=0
with open ("user.txt","rb") as usr:
for line in usr:
usrcount+=1
if usrcount == 1:
ChatApp.show_frame(ChatApp,[PageOne])
print(usrcount)
非常感謝你。這個解釋非常有幫助。你對我試圖解決的空字典是正確的,但我想象禮儀規定,我試圖弄清楚自己後,在另一個問題中提出這個問題。非常感謝您的幫助。 –