简体   繁体   中英

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.

You have created x and y as class variables. The x and y you are passing to your __init__ function are just function parameters, they neither belong to the class nor the instance.

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__ . 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.

I created a simpler example out of the code you posted to show how to access class variables in your init function. 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. I assume you have another function to reload the window when x changes. If not, you can just call init () again.

Now the changed code (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. Basically, the x and y parameters are removed from the init () method and became properties of the class 'App'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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