我正试图将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()
答案 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)
两种方式:
print(foo.x)
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)