简体   繁体   中英

Pass arguments back and forth between two classes

I am having an issue that I can't seem to resolve. I have two classes (which are frames full of tkinter widgets - not relevant to the problem). The first class allows for user data to be input. Both classes then display the added data, as well as prior data. However, I am trying to allow the second class to delete some data, then update the displayed data for both classes. The object created from the second class is passed as an argument to the object created from the first class. The script below outlines roughly how the classes interact.

class Class1():
    def __int__(self, Class2):
        self.Class2 = Class2

    def load_data(self):
        data = ["foo", "bar"]
        self.update(data)

    def update(self, data):
        self.display(data)
        self.Class2.display(data)

    def display(self):
        # Display data
        pass

    def write(self, Class2):
        Class2.write(self.foo)

class Class2():
    def __init__(self):
        self.data = None

    def display(self, data):
        self.data = data
        # Display data
        pass

    def delete_data(self)
        self.data.remove("bar")
        Class1.update(self.data)

B = Class2()
A = Class1(B)

The function delete_data in Class2 is the addition I want to make, and obviously won't work currently. However, this is the interaction I am trying to achieve. Does anyone have any suggestions?

Edit: For clarification, I want the displayed information in both classes to update when I modify the data in class 2

Maybe this kind of thing should work.

class Database:
    def __init__(self, db=None):
        self.db = db

    def update(self, data):
        ...

    def read(self, entry):
        ...

class Class1():
    def __int__(self, database):
        self.database = database

    def display(self):
        # Display data
        self.database.read('username')


class Class2():
    def __init__(self, database):
        self.database = database

    def display(self, data):
        # Display data
        ...


db = Database()
A = Class1(db)
B = Class2(db)

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