-1

Please how can I call 'new' in class staff from self.btn2 in class intro Here is my code

import tkinter
class intro:
   def __init__(self, page1):
     self.page1 = page1
     
     self.btn = tkinter.Button(page1, text="next", command=self.me).grid(column=0, row=0)
     self.btn2 = tkinter.Button(page1, text="message", command=staff.new(self)).grid(column=1, row=0)
   def me(self):
     self.page2 = tkinter.Toplevel()
     self.b = admin(self.page2)
     self.page1.withdraw()


class admin:
   def __init__(self, page2):
     self.page2 = page2

     self.btn = tkinter.Button(page2, text="next", command=self.me2).grid(column=0, row=0)

   def me2(self):
     self.page3 = tkinter.Toplevel()
     self.b = staff(self.page3)
     self.page2.withdraw()


class staff:
   def __init__(self, page3):
     self.page3 = page3

   def new(self):
       print("hello everybody")

if __name__ == '__main__':
   window = tkinter.Tk()
   app = intro(window)
   window.mainloop()

Please help. I want to be able to call 'new' in class staff from button in class intro. I use python 3.6.

Dan
  • 1
  • 4
  • You need an instance of `staff` to call its new() function. – acw1668 Jun 23 '20 at 11:14
  • Please how? I've tried a lot of methods for the instance but not still working – Dan Jun 23 '20 at 11:50
  • `lambda: staff(self.page1).new()` – acw1668 Jun 23 '20 at 12:01
  • It worked perfectly. Thanks. Please post your answer so that I can mark you as 'answered'. – Dan Jun 23 '20 at 12:53
  • This question already has answers here:[how-would-i-access-variables-from-one-class-to-another](https://stackoverflow.com/questions/19993795) – stovfl Jun 23 '20 at 12:57
  • @ stovfl I think someone else would like to get explicit answer using instance with button calling def function from another class – Dan Jun 23 '20 at 13:30

1 Answers1

0

From the comment of @acw1668, I repost the question in a better way as answered

import tkinter
class intro:
   def __init__(self, page1):
     self.page1 = page1
     
     self.btn = tkinter.Button(page1, text="next", command=self.me).grid(column=0, row=0)
     self.btn2 = tkinter.Button(page1, text="message", command=lambda: staff(self.page1).new()).grid(column=1, row=0)
   def me(self):
     self.page2 = tkinter.Toplevel()
     self.b = admin(self.page2)
     self.page1.withdraw()


class admin:
   def __init__(self, page2):
     self.page2 = page2

     self.btn = tkinter.Button(page2, text="next", command=self.me2).grid(column=0, row=0)

   def me2(self):
     self.page3 = tkinter.Toplevel()
     self.b = staff(self.page3)
     self.page2.withdraw()


class staff:
   def __init__(self, page3):
     self.page3 = page3

   def new(self):
       print("hello everybody")

if __name__ == '__main__':
   window = tkinter.Tk()
   app = intro(window)
   window.mainloop()

Dan
  • 1
  • 4