将一个类的入口值传递给另一个类tkinter python

时间:2017-03-05 16:09:11

标签: python-3.x tkinter

我想使用get()方法将条目值从一个类传递到另一个类,我不知道如何。提前致谢

class SampleApp(tk.Tk):

    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)

        # the container is where we'll stack a bunch of frames
        # on top of each other, then the one we want visible
        # will be raised above the others
        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.x=tk.StringVar()
        self.y=tk.StringVar()
        self.key=tk.IntVar()
        self.key.set(0)

        self.frames = {}
        for F in (StartPage, PageOne, PageTwo, LoggedIn, Balance):
            page_name = F.__name__
            frame = F(parent=container, controller=self)
            self.frames[page_name] = frame

            # put all of the pages in the same location;
            # the one on the top of the stacking order
            # will be the one that is visible.
            frame.grid(row=0, column=0, sticky="nsew")

        self.show_frame("StartPage")

    def show_frame(self, page_name):
        '''Show a frame for the given page name'''
        frame = self.frames[page_name]
        frame.tkraise()



class StartPage(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller
        label = tk.Label(self, text="Banking Application", font=TITLE_FONT)
        label.pack(side="top", fill="x", pady=10)

        button1 = tk.Button(self, text="User Login",
                            command=lambda: controller.show_frame("PageOne"))
        button2 = tk.Button(self, text="Create Account",
                            command=lambda: controller.show_frame("PageTwo"))
        button1.pack()
        button2.pack()

这是我要将userid条目值传递给Balance class

的地方
class PageOne(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller



        label = tk.Label(self, text="Login Page", font=TITLE_FONT)
        label.pack(side="top", fill="x", pady=10)
        #user id entry block
        tk.Label(self,text="UserID").pack(side="top", fill="x", pady=4)
        self.userid=tk.Entry(self,textvariable=self.controller.x)
        self.userid.pack(side="top", fill="x", pady=6)
        #password entry block
        tk.Label(self,text="Password").pack(side="top", fill="x", pady=4)
        self.password=tk.Entry(self,textvariable=self.controller.y)
        self.password.pack(side="top", fill="x", pady=6)

        self.submit=tk.Button(self, text="SUBMIT",
                           command=self.login)
        self.submit.pack();


        button = tk.Button(self, text="Back to Main Menu",
                           command=lambda: controller.show_frame("StartPage"))
        button.pack()

    def show_frame(self, page_name):
        '''Show a frame for the given page name'''
        frame = self.frames[page_name]
        frame.tkraise()
    def getname(self):
        name=self.controller.x.get()
        print (name)
        return name
    def login(self):
        getname(self);
        for x in range(0,listsize):
            if(self.controller.x.get()==files[x]):
                print("welcome %s"%(self.controller.x.get()))
                if(self.controller.y.get()==keys[x]):
                    bal=open(self.controller.x.get()+'.txt','r')

                    currbal=int(bal.read())
                    print("login success")
                    self.controller.key.set(1)
                   # print(self.controller.key=1)
                    self.controller.show_frame("LoggedIn");
                    break
        else:
                print("Invalid credentials")

如何将userid从pageone传递到此类

class Balance(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller
        label = tk.Label(self, text="Balance Enquiry", font=TITLE_FONT)
        label.pack(side="top", fill="x", pady=10)


        username = PageOne.getname(self);
        print(username)
        if(self.controller.key==1):
            print(username)
            bal=open(username+'.txt','r')
            currbal=int(bal.read())
            label1 = tk.Label(self, text=currbal)
            label1.pack(side="top", fill="x", pady=10)
###########


        button = tk.Button(self, text="Go to the savings page",
                           command=lambda: controller.show_frame("LoggedIn"))
        button.pack()


if __name__ == "__main__":
    app = SampleApp()
    app.mainloop()

0 个答案:

没有答案