0

How can I pass a variable from class in another class in python? I'm a beginner, I just started learning python and tkinter. Example:

File main.py:

if __name__ == "__main__":
    root = Tk()
    window = Articles(root)
    root.mainloop()

when run this code, articles.py work; Window it's open.

File articles.py

class Articles:
    def __init__(self, master)
    self.master = master
    #...
    #Here is many widgets. No matter, it's works. :)
    #....
    #When I want to double-click on one of the rows in the treeview, it should send me the <item>         
    #variable in the ArticleInfo class and display it in a label when the popup window opens.

    mytree.bind('Double-1', self.get_article_id)

    def get_article_id(self):
        item = self.myTree.identify("item", event.x, event.y)

File article_info.py

class ArticleInfo:
    def __init__(self):
        t = Toplevel()
        myvar_from_articlesclass = ???
        Label(t, text=myvar_from_articlesclass).pack()

I tried many options from what I found on the net, but I was getting different errors.

JRiggles
  • 4,847
  • 1
  • 12
  • 27
Ovidiu
  • 1
  • 2
  • Welcome to SO. What errors did you get? What did you try? What are you trying to achieve? One really cannot tell from your question. – Friedrich Feb 27 '23 at 19:42
  • If you instantiate the `ArticleInfo` class within `main.py` and pass it the same `root` as the `Articles` class object, you can share information between the two class objects via `root`. I've found the approach discussed in [this answer](https://stackoverflow.com/a/33650527/8512262) to be extremely helpful! – JRiggles Feb 27 '23 at 19:57
  • Does this answer your question? [How to access variables from different classes in tkinter?](https://stackoverflow.com/questions/33646605/how-to-access-variables-from-different-classes-in-tkinter) – JRiggles Feb 27 '23 at 19:58

0 Answers0