gpt4 book ai didi

python - 当满足特定条件时无法显示另一个 tkinter 框架

转载 作者:太空宇宙 更新时间:2023-11-03 15:36:32 25 4
gpt4 key购买 nike

我正在尝试制作一个通过网络驱动器运行的即时通讯程序,并使用@Bryan Oakley 的一些代码在 tkinter 框架之间切换。第一帧应该是保存在 txt 文件中的用户名条目。如果我的程序检测到用户名已经保存过一次,我想跳过此帧。但是,当我尝试这样做时,出现以下错误。

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)

最佳答案

这行代码导致了问题:

ChatApp.show_frame(ChatApp,[PageOne])

您不是在 ChatApp实例上调用 show_frame,而是在类对象本身上调用。该类没有该属性,这就是为什么您收到错误“'ChatApp'没有属性'frames'”

您不能在类上调用 show_frame,您必须在类的实例上调用它。在您的情况下,实例作为 controller 传入,您需要保存它以便稍后使用。

class StartPage(tk.Frame):

def __init__(self, parent, controller,*args):
self.controller = controller
...

def check_user(self):
...
if usrcount == 1:
self.controller.show_frame(PageOne)
...

我认为您还会遇到一个问题,即 show_frame 在它不是空字典之前被调用。这是因为您在创建 StartPage 时立即调用 check_user,这发生在您将任何内容插入 self.frames 之前。

关于python - 当满足特定条件时无法显示另一个 tkinter 框架,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42471513/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com