简体   繁体   English

如何访问 tkinter 类中 __init__() 方法中的元素?

[英]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.

相关问题 如何在实例化 object 时传递 **kwargs 并在类的 __init__() 方法中访问它 - How do I pass **kwargs when instantiating an object and access it in the class's __init__() method 我可以不使用__init__来访问课程吗? -Python - Can I access a class without an __init__? - Python Python - 如何从__init__方法中引用类变量或方法? - Python - how can I reference a class variable or method from within the __init__ method? 如何从基类中引用Python中派生类的__init__方法? - How can I reference the __init__ method of a derived class in Python from the base class? 如何在不同的 class __init__ 中的实例上调用 class 方法 - How can I call a class method on an instance within a different class __init__ 如何从另一个的 __init__ 访问 class 的属性? - How may I access an attribute of a class from the __init__ of another? 我如何访问在我的 class 的 __init__ 中声明的变量之一 - How can I access variables declared in __init__ of my class in one of it's functions __init__是一种类方法吗? - Is __init__ a class method? 如何对Enum的__init__方法进行单元测试? - How can I unittest an Enum's __init__ method? 如何使用__init__函数创建服务器Protocol类? - How can I create a server Protocol class with an __init__ function?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM