Python Forum
How can I access objects or widgets from one class in another class?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How can I access objects or widgets from one class in another class?
#1
Here I am working with two different tkinter frame classes, in which I am trying to access the combobox widget service_list from the retrieve frame class, inside a method in the frame class menu, so that I may configure the values of the combobox.
Now my question is, how can I do so without running into a NameError or AttributeError?

import tkinter as tk from customtkinter import CTkComboBox, CTkTextbox from .backend import * class retrieve(tk.Frame): def __init__(self, parent, controller): self.controller = controller tk.Frame.__init__(self, parent, bg="black", highlightthickness=5, highlightcolor='white') self.controller = controller head = tk.Label(self, bg="black", fg="white", text="STORED DATA", font=("Terminal", 32)) services = self.controller.services data = self.controller.data data_disp = CTkTextbox(self, fg_color="black", text_color="white", font=("Terminal", 22, "bold"), scrollbar_button_color="white", scrollbar_button_hover_color="white",wrap=tk.WORD, corner_radius=0, width=450, height=250, border_width=0) def combobox_callback(choice): i = services.index(choice) result = str(data[i]) data_disp.configure(state=tk.NORMAL) data_disp.delete("0.0", tk.END) data_disp.insert("0.0", result) data_disp.configure(state=tk.DISABLED) print("combobox dropdown clicked:", choice) service_list = CTkComboBox(self, fg_color="black", text_color="white", font=("Terminal", 20, "bold"), justify="left", corner_radius=0, width=450, height=36, border_width=0, border_color="white", command=combobox_callback, dropdown_fg_color="black", dropdown_text_color="white", dropdown_font=("Terminal", 20, "bold"),hover=False, button_color="black", button_hover_color="white", dropdown_hover_color="black") service_list.set(">SERVICE NAME") back = tk.Button(self, bg="black", fg="white", text='BACK', font=("Terminal", 18, "bold"), command=lambda: controller.show_frame(page_name="menu", username=self.controller.username, services=self.controller.services, data=self.controller.data), activebackground="black", relief=tk.SOLID, borderwidth=0, padx=16, pady=0, cursor="hand2") entNote = tk.Label(self,bg="black", fg="white", text="(enter)", font=("Terminal", 10)) head.place(relx=0.5, rely=0.1, anchor=tk.CENTER) service_list.place(relx=0.1, rely=0.3, y=-10,x=-35,anchor=tk.W) data_disp.place(relx=0.1, rely=0.45, x=-30, y=65,anchor=tk.W) back.place(relx=0.5, rely=0.9, anchor=tk.CENTER) entNote.place(relx=0.5, rely=0.9, y=20, anchor=tk.CENTER) controller.exitButton(self) controller.homeButton(self) def binds(self): self.focus_set() self.controller.bind('<Return>', lambda x: self.controller.show_frame(page_name="menu", username=self.controller.username, services=self.controller.services, data=self.controller.data))
import tkinter as tk from .backend import * from .retrieve import * class menu(tk.Frame): def __init__(self, parent, controller): self.controller = controller tk.Frame.__init__(self, parent, bg="black", highlightthickness=5, highlightcolor='white') self.controller = controller head = tk.Label(self, bg="black", fg="white", text="MENU", font=("Terminal", 32)) save = tk.Button(self, bg="black", fg="white", text='STORE DATA', font=("Terminal", 20, "bold"), command=lambda: controller.show_frame(page_name="store", username=self.controller.username, services=self.controller.services, data=self.controller.data), activebackground="black", relief=tk.SOLID, borderwidth=0, padx=16, pady=7, cursor="hand2") def extract_data(): services, data = retrieve_passwords(self.controller.username) # TRYING TO ACCESS THE COMBOBOX WIDGET FROM RETRIEVE CLASS SO AS TO CONFIGURE ITS PROPERTIES retrieve1 = retrieve() retrieve1.service_list.configure(values=services) # =================================================== self.controller.show_frame(page_name="retrieve", username=self.controller.username, services=services, data=data) retrieve_but = tk.Button(self, bg="black", fg="white", text='RETRIEVE DATA', font=("Terminal", 18, "bold"), command=lambda: extract_data(), activebackground="black", relief=tk.SOLID, borderwidth=0, padx=16, pady=7, cursor="hand2") head.place(relx=0.5, rely=0.1, anchor=tk.CENTER) save.place(relx=0.5, rely=0.4, anchor=tk.CENTER) retrieve_but.place(relx=0.5, rely=0.6, anchor=tk.CENTER) controller.exitButton(self) controller.homeButton(self) def binds(self): self.focus_set()
Reply
#2
Instead of
service_list = …
Use
self.service_list = …
That is to say make service_list a member of the retriever object.

Avoid import * by all means. It obfuscates the code. Also try to follow PEP 8 coding style.
Reply
#3
(Aug-05-2023, 06:27 PM)Gribouillis Wrote: Instead of
service_list = …
Use
self.service_list = …
That is to say make service_list a member of the retriever object.

Avoid import * by all means. It obfuscates the code. Also try to follow PEP 8 coding style.

Looks like it did work because no errors and I can access the object service_list. But somehow I cant seem to configure its values with the list services, everytime I click the retrieve_but in the menu frame
Reply
#4
Nevermind, looks like it was just a type error. Because after back testing a little bit, it seems like the method retrieve_passwords was returning a tuple instead of a list which was required to be fed into the widget values
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  class Blockage not projecting Azdaghost 1 985 May-15-2025, 04:32 PM
Last Post: deanhystad
  Return a string or byte object from Enum class? Calab 5 1,848 May-14-2025, 05:21 PM
Last Post: snippsat
  Python inner classes inheritance from parent class Abedin 8 2,884 Apr-23-2025, 05:56 AM
Last Post: Gribouillis
  Accessing method attributes of python class Abedin 6 2,410 Apr-14-2025, 07:02 AM
Last Post: buran
  Python class members based on a type value voidtrance 7 2,459 Apr-11-2025, 10:10 PM
Last Post: deanhystad
  Create a new subclass in a Python extension based on an existing class voidtrance 6 2,608 Mar-25-2025, 06:37 PM
Last Post: voidtrance
  printing/out put issue with class arabuamir 3 1,647 Aug-25-2024, 09:29 AM
Last Post: arabuamir
  Class test : good way to split methods into several files paul18fr 5 5,977 Jul-17-2024, 11:12 AM
Last Post: felixandrea
  sharepoint: Access has been blocked by Conditional Access policies CAD79 0 3,805 Jul-12-2024, 09:36 AM
Last Post: CAD79
  [split] Class and methods ebn852_pan 15 4,907 May-23-2024, 11:57 PM
Last Post: ebn852_pan

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020
This forum uses Lukasz Tkacz MyBB addons.
Forum use Krzysztof "Supryk" Supryczynski addons.