[英]How can I access the element in a __init__() method in a tkinter class?
I'm trying to access an element from a list that is created in the following.我正在尝试从下面创建的列表中访问一个元素。 The program switches between multiple pages, and I want receive input from the user to store as a string in another file.该程序在多个页面之间切换,我希望接收来自用户的输入以将其作为字符串存储在另一个文件中。
class SignupPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent, bg='light blue')
ulabel = tk.Label(self, text="User Name").pack()
uname = tk.StringVar()
uEntry = tk.Entry(self, textvariable=uname). pack()
plabel = tk.Label(self, text='Password').pack()
psswd = tk.StringVar()
pEntry = tk.Entry(self, textvariable=psswd).pack()
def Signup():
username = uname.get()
password = psswd.get()
credentials_list = [username, password]
print(credentials_list)
print(username, password)
controller.show_frame(SignupPage2)
nextbutton = tk.Button(self, text='Next', command=Signup).pack()
backbutton = tk.Button(
self, text='Back', command=lambda: controller.show_frame(WelcomePage)).pack()
I want to access the username variable in another file, so I imported the class SignupPage.我想访问另一个文件中的用户名变量,所以我导入了类 SignupPage。
from app import SignupPage
However, trying to access the element by typing the following doesn't work.但是,尝试通过键入以下内容来访问该元素是行不通的。
print(SignupPage.__init__.Signup.credentials_list[0])
How can I access the username variable?如何访问用户名变量?
Let's start with the following function inside your SignupPage
class`:让我们从SignupPage
类中的以下函数开始:
def Signup():
username = uname.get()
password = psswd.get()
...
You want to access username
inside that function.您想访问该函数内的username
。 Unfortunately, username
only exists inside Signup()
, and cannot be accessed by using the dot operator.不幸的是, username
只存在于Signup()
,不能使用点运算符访问。 The same goes for the rest of the variables inside the function.函数内的其余变量也是如此。 So, you cannot get the username value by using因此,您无法通过使用获取用户名值
SignupPage.__init__.Signup.credentials_list[0]
We have to do something else.我们必须做其他事情。 One thing we could do is put Signup()
outside of __init__()
and give SignupPage
some instance variables.我们可以做的一件事是将Signup()
放在__init__()
之外,并为SignupPage
一些实例变量。 The SignupPage
class would look something like the following. SignupPage
类将类似于以下内容。
class SignupPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent, bg='light blue')
self.credentials_list = list()
ulabel = tk.Label(self, text="User Name").pack()
self.uname = tk.StringVar()
uEntry = tk.Entry(self, textvariable=self.uname). pack()
plabel = tk.Label(self, text='Password').pack()
self.psswd = tk.StringVar()
pEntry = tk.Entry(self, textvariable=self.psswd).pack()
nextbutton = tk.Button(self, text='Next', command=self.Signup).pack()
backbutton = tk.Button(self, text='Back', command=lambda: controller.show_frame(WelcomePage)).pack()
self.pack()
def Signup(self):
username = self.uname.get()
password = self.psswd.get()
self.credentials_list = [username, password]
print(self.credentials_list)
print(username, password)
controller.show_frame(SignupPage2)
With this one, when we press the "Next" button, an instance of the SignupPage
class will hold the values entered in the username and password text fields.有了这个,当我们按下“下一步”按钮时, SignupPage
类的一个实例将保存在用户名和密码文本字段中输入的值。 To create an instance of the class, we simply do:要创建类的实例,我们只需执行以下操作:
# root = tk.Tk()
signupPage = SignupPage(root, controller)
At this point, we can now access credentials_list
by doing:此时,我们现在可以通过执行以下操作访问credentials_list
:
signupPage.credentials_list
Remember to pass the same instance of SignupPage
to anything that will get the value of any attributes the class has.请记住将SignupPage
的相同实例SignupPage
给将获得该类具有的任何属性值的任何内容。
I reckon that what I suggested is not the best design, but it should at least give you an idea on how to solve your problem.我认为我的建议不是最好的设计,但它至少应该让你知道如何解决你的问题。
Side Note : One way you can improve upon this is to use to provide a getter method for SignupPage
that returns the value of credentials_list
, instead of having to access the aforementioned variable by doing <instance>.credentials_list
.边注:可以提高在这方面的一个方法是使用为提供一个getter方法SignupPage
该回报的价值credentials_list
而不必做访问上述变量, <instance>.credentials_list
。 Doing this is generally a better programming practice than the other method.这样做通常是比其他方法更好的编程实践。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.