当满足某个条件时,无法显示另一个tkinter帧

时间:2017-02-26 17:03:35

标签: python python-3.x tkinter

我正在尝试制作一个即时通讯程序,该程序可以在网络驱动器上运行,并使用@Bryan Oakley的一些代码在tkinter帧之间切换。第一帧应该是username的一个条目,它保存在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)

1 个答案:

答案 0 :(得分:1)

这行代码导致问题:

ChatApp.show_frame(ChatApp,[PageOne])

您呼叫show_frame不是ChatApp实例,而是类对象本身。该类没有该属性,这就是您收到错误的原因&#34;&#39; ChatApp&#39;没有属性&#39;框架&#39;&#34;

你不能在课堂上调用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之前被调用的问题,而不是空字典。这是因为您在创建check_user时立即致电StartPage,这是在您向self.frames插入任何内容之前发生的。