0

I'm having a bit of trouble with tracing listbox changes in tkinter. I've managed to trace widgets that have variables associated to them, like scales, but that doesn't work with listboxes.

I've tried associating StringVar to it, but that doesn't work since you can't give a "variable=" argument to a Listbox widget. I've also tried the "command=" argument but it doesn't seem to work either.

This is my primary trouble, though it's not a vital function for my program. I have tried to implement this without the tracing, and thus calling the function I would call with trace after I change the Listbox, but I can't access the widget that is defined inside another class than the one I'm working in.

Here is the code:

class GUI:
    def __init__(self, master):
       ...
       GUI.NetworkLayout = Listbox(master, exportselection=False)
       GUI.NetworkLayout.insert(END, "Ellipse", "Random", "Form 3", "Form 4")
       GUI.NetworkLayout.select_set(0)
       GUI.NetworkLayout.grid(row=2, column=0, padx=5, pady=5)
       ...
class NetworkFrame:
    def __init__(self,master):
       ...
       def update_canvas(*args):
        """
        Redraws the shapes child to the canvas as follows:
            - Adapts them to match the node size slider.
            - Adapts them to match the bond width slider.
            - Adapts them to match the number of people.
        """
        pts = points(720, 320, GUI.NetworkLayout.get())
        NetworkFrame.DisplayFrame.delete("all")

        for i in range(GUI.number_of_people_var.get()-1):
            Link(NetworkFrame.DisplayFrame, pts, i)

        for i in range(GUI.number_of_people_var.get()):
            person_id = Person(NetworkFrame.DisplayFrame, pts, i)
            if i % 2 == 0:
                Person.change_color(person_id, GUI.hex_rumor)
            else:
                pass

As you can see, I try to access the GUI.NetworkLayout object by writing GUI.NetworkLayout, however I get this error when trying to execute the program-

AttributeError: type object 'GUI' has no attribute 'NetworkLayout'

Thank you

Iocust
  • 105
  • 1
  • 14
  • How are you defining "change the listbox"? Are you wanting this trace to happen when something in the listbox is selected, or whenever something is added or removed from the listbox? – Bryan Oakley Mar 01 '15 at 20:18
  • Thank you for helping! I mean whenever the user selects a different item in the listbox. – Iocust Mar 01 '15 at 20:52

0 Answers0