访问从一个类到另一个类的变量

时间:2017-08-14 23:19:12

标签: python python-3.x

我是python的新手,需要一些代码帮助。我想从User类访问Login类中的MainMenu变量。任何帮助表示赞赏。提前谢谢你,祝你有个美好的一天。

class Login (tk.Frame):
def __init__(self, parent, controller):
    tk.Frame.__init__(self, parent)
    self.controller = controller
    Username = ttk.Label(self, text="Username: ")
    Password = ttk.Label(self, text="Password: ")
    Username.grid(row=0, column=0, sticky='nsew')
    Password.grid(row=1, column=0, sticky='nsew')
    UsernameE = ttk.Entry(self)
    PasswordE = ttk.Entry(self, show='*')
    UsernameE.grid(row=0, column=1, sticky='nsew')
    PasswordE.grid(row=1, column=1, sticky='nsew')
    BackButton = ttk.Button(self, text="Sign up", command=lambda:controller.show_frame(NewHere))
    BackButton.grid(row=4, column=0, sticky='nsew')
    LoginButton = ttk.Button(self, text="Log In")
    LoginButton.grid(row=4, column=1, sticky='nsew')

    def CheckLogin(event):
        global Active
        try:
            Accounts = np.load("Dictionary.npy").item()
        except FileNotFoundError:
            messagebox.showerror("Account Error", "The account could not be found in our database with the credentials provided. Sign up and try again.")
        if UsernameE.get().rstrip() in Accounts and PasswordE.get() != '':
            Check = Accounts[UsernameE.get().rstrip()]
            if PasswordE.get() == Check:
                self.Usser = UsernameE.get()
                messagebox.showinfo("Success!", "Welcome, "+self.Usser)
                UsernameE.delete(0, 'end')
                PasswordE.delete(0, 'end')
                controller.show_frame(MainMenu)
            elif UsernameE.get().rstrip() in Accounts and PasswordE.get() != Check:
                messagebox.showerror("Error 511", "The account could not be found in our database with the credentials provided. Double check them and try again.")
                UsernameE.delete(0, 'end')
                PasswordE.delete(0, 'end')
        else:
            messagebox.showerror("Error 510", "The account could not be found in our database with the credentials provided. Double check them and try again.")
            UsernameE.delete(0, 'end')
            PasswordE.delete(0, 'end')
    LoginButton.bind("<Button-1>", CheckLogin)

# The end of the Log in Window

class MainMenu(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller
        ActiveUser = ttk.Label(self, text="Logged in as: ")
        ActiveUser.grid(row=0, column=3, sticky='nsew')
        vers = '1.0'
        Version = ttk.Label(self, text = "Version: "+ vers)
        Version.grid(row=1, column=3, sticky='nsew')
        Logout = ttk.Button(self, text='Logout')
        Logout.grid(row=2, column=3, sticky='nsew')

0 个答案:

没有答案