How to send data between different classes (frames)? #2620
-
Hi, I am struggling with sending data between two different frames. The idea is simple: in one frame, there is an entry and a button and in the other frame there is a label that will display what I have enter in the entry when I press the button. However, I am not able to get the expected result. The code is as follows: ===================
===================The error that pops up is: AttributeError: type object 'Frame1' has no attribute 'label' Any idea what is going on? Thanks in advance :) |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
It is recommended that you should learn some Python OOP and Tkinter basics. Your modified code is as follows: import customtkinter as ctk
class Main(ctk.CTk): # Define Main class
def __init__(self):
super().__init__()
self.frame1 = Frame1(self)
self.frame1.grid(row=0, column=0, padx=20, pady=20)
self.frame2 = Frame2(self, frame1_ref=self.frame1)
self.frame2.grid(row=0, column=1, padx=20, pady=20)
class Frame1(ctk.CTkFrame): # Define frame class for containing the label
def __init__(self, master):
super().__init__(master)
self.label = ctk.CTkLabel(master=self, text='', fg_color='grey25')
self.label.grid(row=0, column=0, padx=20, pady=20)
class Frame2(ctk.CTkFrame): # Define frame class for containing the entry and the button
def __init__(self, master, frame1_ref):
super().__init__(master)
self.entry = ctk.CTkEntry(master=self, placeholder_text='Input')
self.entry.grid(row=0, column=0, padx=20, pady=20)
self.button = ctk.CTkButton(master=self, text='Click', command=lambda: self.send(self.entry.get()))
self.button.grid(row=0, column=1, padx=20, pady=20)
self.frame1_ref = frame1_ref
def send(self, text):
self.frame1_ref.label.configure(text=text)
app = Main()
app.mainloop() Let me know if you need further help. |
Beta Was this translation helpful? Give feedback.
It is recommended that you should learn some Python OOP and Tkinter basics. Your modified code is as follows: