Search code examples
pythonclasstkintertreeviewtoplevel

How to pass variable from one class into Treeview of another class


I am trying to pass a value obtained in a tkinter.Toplevel into my root application. The window opens up to customize a pizza, and I am trying to get the object I create back into the root application so I can input into a Treeview I'm using as like a checkout box.

Abbreviated:

def submit(self):
     self.custom_item = Item(000, name, self.total, 'Pizza')
     PoS.custom_item = self.custom_item
     addItem = PoS.add_custom_item(PoS,PoS.custom_item)
     self.win.destroy()

This is the function that runs (more or less) when I click the submit button in my Toplevel

The function referenced in my PoS class is this:

    def add_custom_item(self,item):
        self.custom_item = item
        self.coTree.insert('','end',text='1',open=True,values=(self.custom_item.name,'{:.2f}'.format(self.custom_item.price)))

coTree is the checkout box Tree. However whenever I run this I get an AttributeError: type object 'PoS' has no attribute 'coTree'. I have tried doing addItem = PoS.add_custom_item(self,self.custom_item) to no avail either. Can anyone help me out of this rut? Thanks.


Solution

  • Would like to officially answer on behalf of Michael Guidry because his answer worked like a charm to me. I am still new to classes. I changed it to:

    class custom:
        def __init__(self,root,func)
             self.func = func
             self.root = root
    
        def submit(self):
                self.func(self.custom_item)
                self.win.destroy()
    

    and my button in the root PoS class to:

    custom_button =tk.Button(self.root,text='Custom',command=partial(custom,self.root,self.add_custom_item)
    

    and it worked perfectly. Thank you again Michael!