0

I have two listboxes on tkinter, what I'd like to do is double-click the item in one listbox, add it to the list, and have it show up in the other listbox. Currently, the adding to list part is working, but it's not showing up in the other listbox for some reason.

import tkinter as tk

testList = ["dog", "cat", "orange"]
newList = []

class SampleApp(tk.Tk):
    def __init__(self):
        tk.Tk.__init__(self)
        self._frame = HomePage

class HomePage(tk.Frame):
    def __init__(self, master):
        tk.Frame.__init__(self, master)
        self.config(width=300, bg='white', height=500, relief='sunken', borderwidth=2)

        self.reportNames = tk.StringVar(value=tuple(testList))
        self.lbox = tk.Listbox(self, listvariable=self.reportNames, height=15, width=50, borderwidth=2)
        self.lbox.pack()
        self.lbox.bind('<Double-1>', lambda x: addButton.invoke())

        addButton = tk.Button(self, text='Select', command=self.selection)
        addButton.pack()

        self.testNames = tk.StringVar(value=newList)
        self.lbox2 = tk.Listbox(self, listvariable=self.testNames, height=15, width=50, borderwidth=2)
        self.lbox2.pack()

    def selection(self):
        addThis = self.lbox.selection_get()
        print(self.lbox.selection_get())
        newList.append(addThis)
        print(newList)


if __name__ == "__main__":
    global app
    app = SampleApp()
    sidebar = HomePage(app)
    sidebar.pack(expand=False, fill='both', side='left', anchor='nw')
    app.geometry("1200x700")
    app.mainloop()
Lionel Yu
  • 328
  • 3
  • 11
  • Just out of curiosity, why are the testList and newList variables global? Could they not be made class variables, for instance of the SampleApp class? – strava Oct 05 '18 at 23:42
  • strava - thanks again for the answer. What are the advantages of having them as class variables as part of the SampleApp class? The original reason i kept them as globals is 1) i'm not too familiar with classes yet and 2) i'm planning on using them in multiple frames – Lionel Yu Oct 06 '18 at 00:32
  • No problem. It's considered bad practice to use global variables because they generally create confusion, see [this question](https://stackoverflow.com/q/19158339/8201979). – strava Oct 06 '18 at 09:31
  • Makes perfect sense. I'll have to work on understanding how to add those variables into the SampleApp class and learn how to call them from the Frame classes. This generally causes me confusion, but it's better to get this right earlier than taking the easy way out. – Lionel Yu Oct 06 '18 at 20:00
  • The standard way of doing it is to pass your SampleApp class to all the windows it controls, and then assign it to a parent attribute in each class. That way you can access variables in your SampleApp class from any of your other classes through their parent attributes. I hope that makes sense. – strava Oct 06 '18 at 22:10

1 Answers1

3

Your StringVar testNames won't track changes made to newList, you need to update the StringVar every time newList changes.

def selection(self):
    addThis = self.lbox.selection_get()
    print(self.lbox.selection_get())
    newList.append(addThis)
    print(newList)
    # update StringVar
    self.testNames.set(newList)
strava
  • 765
  • 6
  • 14
  • brilliant! Thank you strava! I spent a good 2 hours pounding my head against the wall trying to do various methods like frame.refresh, frame.update, etc all to no avail. – Lionel Yu Oct 06 '18 at 00:29