What I'm trying to accomplish is a way of changing a variable in another class instance. I've been scratching my head trying to understand how (or if) this is possible.
How can one update the value of
self.lblvar
that is in the classMainWindow
from the classSecondWindow
?
This is what I'm working with to test my theory:
from tkinter import *
class MainWindow:
def __init__(self, rootWin):
self.rootWin = rootWin
self.rootWin.geometry('400x200')
self.mainMenu = Menu(self.rootWin)
self.mainMenu.add_command(label = 'Open Second Window', command = self.openSecondWindow)
self.lblvar = StringVar()
self.lblvar.set('Change Me!')
self.lbl = Label(rootWin, textvariable = self.lblvar)
self.lbl.pack()
self.rootWin.config(menu = self.mainMenu)
def openSecondWindow(self):
self.secondWin = Tk()
self.secWin = SecondWindow(self)
self.secondWin.mainloop()
class SecondWindow:
def __init__(self, parent):
self.parent = parent
self.btn = Button(self, label = 'Change Label?', command = self.changeOther)
self.btn.pack()
def changeOther(self):
self.parent.lblvar.set('Changed it!')
def main():
root = Tk()
mainWin = MainWindow(root)
root.mainloop()
if __name__ == "__main__":
main()
I'm a bit of a novice when it comes to Python classes, so any guidance and/or explanation regarding this would be appreciated!
Edit: Changed original question to a clearer question to aid further searches on the topic