Calling widget from another function? - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: GUI (https://python-forum.io/forum-10.html) +--- Thread: Calling widget from another function? (/thread-13591.html) |
Calling widget from another function? - WuchaDoin - Oct-22-2018 class Inv(tk.Frame): def __init__(self, parent, controller): tk.Frame.__init__(self, parent) table_frame = tk.LabelFrame(main_frame, bg='yellow', width=640, height=720) tk_table = ttk.Treeview(table_frame, columns=imp_df.columns.values) t_entry = tk.Entry(self, width=100) t_entry.insert(0,"ello") t_entry.pack() t_btn.pack() table_frame.pack(side='right', fill='y', expand=True, anchor='e', ipadx=150) table_frame.pack_propagate(0) tk_table.pack(anchor='center', expand=True, fill='both') tk_table.bind("<<TreeviewSelect>>", self.insert_info) def insert_info(self, event): t_entry.insert(0, "test insert text")I have tried numerous setups, but I cannot call my test entry widget (t_entry) from another function. I am wanting my entry widgets populated with the data that is selected on the tk tree, but when declaring a new function: def insert_info(self, event): t_entry.insert(0, "test insert text")"t_entry" is never recognized. I understand this code won't work, but I typed it to give an example of what I am trying to achieve. Solved it unexpectedly. def insert_info(self, event): t_entry.insert(0, "test insert text")needed to be def insert_info(self, event): self.t_entry.insert(0, "test insert text")I also had to declare "self" on all my previous widgets to be able to call any widget appropriately. RE: Calling widget from another function? - stullis - Oct-23-2018 Here's the explanation as to why you had to do that: In your original code, t_entry was a local variable inside the __init__() method. Therefore, when the method finished, it ceased to exist. Whereas, when you name it self.t_entry, it becomes an attribute of the Inv class and exists after the __init__() method has finished. Then, to call it up again, you treat it like any other class attribute and call up Inv.t_entry - which is self.t_entry within the methods of the Inv class. |