简体   繁体   English

在类中设置变量,并使用python中的方法对其进行更新以在类中重用

[英]Setting a variable in a class and updating it with a method in python to re-use in the class

I am trying to set a variable in a class and then update it with a class method, 我正在尝试在一个类中设置一个变量,然后使用类方法对其进行更新,

class App:
    x = 2
    y = 2

    def __init__(self, master, x, y):
        for i in range(x):
            for j in range(y):
                b = Entry(master)
                b.grid(row=i, column=j)
        Button(text='Add ', command=self.enlarge).grid(row=height,
               column=width)
        Button(text='Remove', command=self.shrink).grid(row=height,
               column=width - 1, sticky="e")

    @classmethod
    def enlarge(cls):
        cls.x += 1

    @classmethod
    def shrink(cls):
        cls.x -= 1 

root = Tk()
app = App(root)
mainloop()

Even though the methods update x, it does not update the global x in my init function. 即使方法更新了x,它也不会更新我的init函数中的全局x。

You have created x and y as class variables. 您已经创建了xy作为类变量。 The x and y you are passing to your __init__ function are just function parameters, they neither belong to the class nor the instance. 您传递给__init__函数的xy只是函数参数,它们既不属于类也不属于实例。

Now, if you do want to access the x and y class variables in init , you can access it either using self or self.__class__ . 现在,如果您确实想在init中访问x和y类变量,则可以使用selfself.__class__ But, do note that if you instance variables with the same name as class variables and if you try to access them using self, then instance variables will be first chosen, only when instance variables of the same name are not found, class variables are considered. 但是,请注意,如果您使用与类变量同名的实例变量,并且尝试使用self访问它们,那么将首先选择实例变量,只有当未找到相同名称的实例变量时,才考虑使用类变量。

I created a simpler example out of the code you posted to show how to access class variables in your init function. 我用您发布的代码创建了一个更简单的示例,以显示如何在init函数中访问类变量。 Check it below: 在下面检查:

class App:
    x = 2
    y = 2

    def __init__(self):
        for i in range(self.__class__.x):
            for j in range(self.__class__.y):
                print ("%s %s" %(i,j))

    @classmethod
    def enlarge(cls):
        cls.x += 1

    @classmethod
    def shrink(cls):
        cls.x -= 1

app = App()

I tried changing your code a little bit. 我尝试过一点更改您的代码。 Firstly, When the x inside your class 'App' is changed the an additional row is not automatically appended as the window is not reloaded. 首先,当更改类“ App”中的x时,由于不重新加载窗口,因此不会自动附加一行。 I assume you have another function to reload the window when x changes. 我假设您有另一个功能可以在x更改时重新加载窗口。 If not, you can just call init () again. 如果没有,您可以再次调用init ()。

Now the changed code (python3): 现在更改后的代码(python3):

from tkinter import Entry, Button
import tkinter as tk

class App:
    x = 2
    y = 2

    def __init__(self, master):
        self.master = master
        self.topFrame = tk.Frame(master)
        self.topFrame.grid(row = 0, columnspan = 2)
        for i in range(self.x):
            for j in range(self.y):
                b = Entry(self.topFrame)
                b.grid(row=i, column=j)
        Button(self.topFrame, text='Add ', command=self.enlarge).grid(row=2,
               column=2)
        Button(self.topFrame, text='Remove', command=self.shrink).grid(row=2,
               column=2 - 1, sticky="e")

    def enlarge(self):
        self.x += 1
        self.topFrame.destroy()
        self.__init__(self.master)

    def shrink(self):
        self.x -= 1
        self.topFrame.destroy()
        self.__init__(self.master)

root = tk.Tk()
app = App(root)
tk.mainloop()

Note that the init () method is called to reload the window. 请注意,调用了init ()方法来重新加载窗口。 Basically, the x and y parameters are removed from the init () method and became properties of the class 'App' 基本上,x和y参数已从init ()方法中删除,并成为类“ App”的属性

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM