如何将价值从一个类别返回到另一个类别?

时间:2019-09-29 14:34:29

标签: python tkinter

我正试图将entry_username从类Window1返回到名为Window2的类。

我的完整代码位于底部

我的目标是在Window2中显示一个标签,该标签显示刚从Window1登录的用户名

我在牢记Windows2内部使用笔记本电脑。

这是我在Window2内部尝试过的内容:

self.User = Label(main, text = Window1().entry_username.get())

但这会返回一个错误:

TypeError: __init__() missing 1 required positional argument: 'master'

我的密码文件:

test:1

def main():
    root = Tk()
    app = Window1(root)

    root.mainloop()

class Window1:
    def __init__(self,master):
        self.master = master
        self.master.title("User Log In")
        self.master.geometry('400x150')
        self.frame = Frame(self.master)
        self.frame.pack(fill="both", expand=True)


        self.label_username = Label(self.frame, text="Username: ",font=("bold",16))
        self.entry_username = Entry(self.frame, font = ("bold", 14))
        self.label_password = Label(self.frame, text="Password: ",font=("bold",16))
        self.entry_password = Entry(self.frame, show="*", font = ("bold", 14))

        self.label_username.pack()
        self.entry_username.pack()
        self.label_password.pack()
        self.entry_password.pack()

        self.logbtn = Button(self.frame, text="Login", font = ("bold", 10), command=self._login_btn_clicked)
        self.logbtn.pack()



    def _login_btn_clicked(self):
        # print("Clicked")
        username = self.entry_username.get()
        password = self.entry_password.get()

        # print(username, password)
        account_list = [line.split(":", maxsplit=1) for line in open("passwords.txt")]
        # list of 2-tuples. Usersnames with colons inside not supported.
        accounts = {key: value.rstrip() for key, value in account_list}
        # Convert to dict[username] = password, and slices off the line ending.
        # Does not support passwords ending in whitespace.
        if accounts[username] == password:
            self.master.withdraw()
            self.newWindow = Toplevel(self.master)
            self.app = Window2(self.newWindow)

        else:
            self.entry_username.delete(0,"end")



class Window2:
    def __init__(self,master):
        notebook = ttk.Notebook(master)

        notebook.pack(expand = 1, fill = "both")
        #Frames
        main = ttk.Frame(notebook)

        notebook.add(main, text='Main-Screen')




if __name__ == '__main__':
    main()

更新日期:30/09/2019这是正确的吗?

def main():
    root = Tk()
    app = Window1(root)

    root.mainloop()

class Window1:
    def __init__(self,master,username): #added code
        self.username = username#added code 2

        self.master = master
        self.master.title("User Log In")
        self.master.geometry('400x150')
        self.frame = Frame(self.master)
        self.frame.pack(fill="both", expand=True)

        username = 'KOB' #added code 3
        window_1_instance = Window1(username) # added code 4

        self.label_username = Label(self.frame, text="Username: ",font=("bold",16))
        self.entry_username = Entry(self.frame, font = ("bold", 14))
        self.label_password = Label(self.frame, text="Password: ",font=("bold",16))
        self.entry_password = Entry(self.frame, show="*", font = ("bold", 14))

        self.label_username.pack()
        self.entry_username.pack()
        self.label_password.pack()
        self.entry_password.pack()

        self.logbtn = Button(self.frame, text="Login", font = ("bold", 10), command=self._login_btn_clicked)
        self.logbtn.pack()



    def _login_btn_clicked(self):
        # print("Clicked")
        username = self.entry_username.get()
        password = self.entry_password.get()

        # print(username, password)
        account_list = [line.split(":", maxsplit=1) for line in open("passwords.txt")]
        # list of 2-tuples. Usersnames with colons inside not supported.
        accounts = {key: value.rstrip() for key, value in account_list}
        # Convert to dict[username] = password, and slices off the line ending.
        # Does not support passwords ending in whitespace.
        if accounts[username] == password:
            self.master.withdraw()
            self.newWindow = Toplevel(self.master)
            self.app = Window2(self.newWindow)

        else:
            self.entry_username.delete(0,"end")



class Window2:
    def __init__(self,master):
        notebook = ttk.Notebook(master)

        notebook.pack(expand = 1, fill = "both")
        #Frames
        main = ttk.Frame(notebook)

        notebook.add(main, text='Main-Screen')

        self.User = Label(main, text=window_1_instance.username)#added code 5


if __name__ == '__main__':
    main()

2 个答案:

答案 0 :(得分:1)

Window1是整个类的名称。您需要该类的单个对象实例,然后从其中检索实例变量。

例如

class Window1:

    def __init__(self, username):
        self.username = username

...

username = 'KOB'
window_1_instance = Window1(username)

....

self.User = Label(main, text=window_1_instance.username)

在代码的这一行:

self.User = Label(main, text = Window1().entry_username.get())

Window1()正在创建该类的实例,与我上面所做的相同,只是没有将其传递给变量。由于您已定义Window1构造函数(__init__方法)以将master作为参数,因此您需要将此行更改为:

self.User = Label(main, text = Window1(master=<instance of master>).entry_username.get())

这正是错误告诉您的内容:

TypeError: __init__() missing 1 required positional argument: 'master'

答案 1 :(得分:0)

两种方式:

  1. 直接从调用类中访问值,例如print(foo.x)
  2. 在第一堂课中创建一个吸气剂,然后像k = foo.getVal()一样从第二个叫该吸气剂

使用两种方法在类之间传递值的基本示例:

class foo():
    def __init__(self):
        self.x = 42
    def getVal(self):
      return self.x

class bar(object):
    def __init__(self, foo):
        print(foo.x)
        k = foo.getVal()
        print(k)

a = foo()
b = bar(a)