I am using Tkinter in Python 3, and have two windows, each represented by a class. I need to click a button in WindowB (my second class) that runs a method from WindowA (my first class). This should be fine, however the method I am calling calls another method from WindowA (my first class). This results in the error: AttributeError: 'WindowB' object has no attribute 'functionB'. How would I fix this so I can run functionA in WindowB (2nd class) with no errors? Code:
from tkinter import *
from functools import partial
class WindowA(Frame):
def __init__(self, master= None): # initialise Menu
Frame.__init__(self, master) # initialise Frame
self.master = master
self.createWindow() # adds buttons
def createWindow(self):
self.pack(fill = BOTH, expand = 1)
# lambda prevents self.changeWindow from executing before button pressed
button1 = Button(self, text = 'Change to Window B', command = lambda: self.changeWindow(WindowB))
button1.pack(fill = X, padx = 10, pady = 10)
button2 = Button(self, text = 'Run Function A', command = lambda: self.functionA())
button2.pack(fill = X, padx = 10, pady = 10)
def changeWindow(self, object):
root.withdraw()
currentFrame = object(root)
def functionA(self):
print("I am function A.")
self.functionB()
def functionB(self):
print("I am function B, called from function A.")
class WindowB(Toplevel):
def __init__(self, master = None):
Toplevel.__init__(self, master)
self.master = master
self.Window()
def Window(self):
button3 = Button(self, text = 'Run Function A', command = lambda:WindowA.functionA(self))
button3.grid(row = 1, column = 0, padx = 10)
root = Tk()
app = WindowA(root)
root.mainloop()