Reputation: 380
I searched on this website and other websites, and I still cannot figure out how to change the text of a label residing on a different frame. Here is my code:
from tkinter import *
class mainW(Tk):
def __init__(self, parent):
Tk.__init__(self, parent)
self.parent = parent
self.widgets()
def widgets(self):
self.left = leftF(self)
self.left.grid(row=0, column=0)
self.right = rightF(self)
self.right.grid(row=0, column=1)
class leftF(Frame):
def __init__(self, parent):
Frame.__init__(self, parent, bg="blue")
self.parent = parent
self.leftWidgets()
def leftWidgets(self):
self.Label = Label(self, text="Hello", bg="red", fg="white")
self.Label.grid(row=0, column=0, padx=5, pady=5)
class rightF(Frame):
def __init__(self, parent):
Frame.__init__(self, parent, bg="white")
self.parent = parent
self.rightWidgets()
def rightWidgets(self):
self.Button = Button(self, text="change text", bg="yellow", fg="black", command=self.action)
self.Button.grid(row=0, column=0, padx=5, pady=5)
def action(self):
self.targetFrame = leftF(self)
self.targetLabel = self.targetFrame.Label
self.targetLabel.config(text="World")
if __name__=="__main__":
mainW(None).mainloop()
Essentially, the action that is assigned to the button residing on rightF should change the text of the label on leftF. If anyone can direct me to the right direction, I would really appreciate.
Thank you.
Upvotes: 2
Views: 3200
Reputation: 1046
The problem is that you are creating a new object with
self.targetFrame = leftF(self)
and not referencing the original one. Change the action function to
def action(self):
self.parent.left.Label.config(text="World")
and it will work.
Upvotes: 4