skywang329
skywang329

Reputation: 49

Updating the displayed contents of a Listbox as a separate list's contents change?

Currently, I have this:

from tkinter import *
from tkinter.ttk import *


class MainGUI(Frame):

    chapters = []

    def __init__(self, parent):
        Frame.__init__(self, parent)
        self.parent = parent
        #refresh function contents here
        self.chapters.append("hi")
        #
        self.initUI()

    def initUI(self):
        self.parent.title("Latest Chapters")
        self.style = Style()
        #self.style.theme_use("default")

        scrollbar = Scrollbar(self)
        scrollbar.pack(side=RIGHT, fill=BOTH)

        listbox = Listbox(self, yscrollcommand=scrollbar.set)

        for newChapter in self.chapters:
            listbox.insert(END, newChapter)

        listbox.pack(side=TOP, fill=BOTH)
        scrollbar.config(command=listbox.yview)

        frame = Frame(self, relief=RAISED, borderwidth=1)
        frame.pack(fill=BOTH, expand=True)

        self.pack(fill=BOTH, expand=True)

        closeButton = Button(self, text="Close", command=self.master.destroy)
        closeButton.pack(side=RIGHT, padx=5, pady=5)
        refreshButton = Button(self, text="Refresh Now", command=self.refresh)
        refreshButton.pack(side=RIGHT)

    def refresh(self):
        self.chapters.append("hi")
        #print("Refresh")
        #refresh func here
        self.initUI()

def main():
    root = Tk()
    root.geometry("300x207+300+300")
    app = MainGUI(root)
    root.mainloop()

if __name__ == '__main__':
    main()

Clicking my refresh button has the chapters array appended with an additional item. The chapters array is initialized and displayed as a Listbox in initUI(). When I click the refresh button, I would like to have the changes to the chapters array also shown in the GUI window. I'm not sure what to do - so far I've tried calling self.initUI() again but it just makes multiple smaller frames. All help is appreciated!

Upvotes: 2

Views: 1284

Answers (1)

martineau
martineau

Reputation: 123423

You can do it by making the Listbox an attribute of the MainGUI instance so you can easily reference it in the refresh() method. Once that is done, it's a simple matter to add new items to both the chapters list and the Listbox instance:

Here's what I mean:

from tkinter import *
from tkinter.ttk import *


class MainGUI(Frame):
    chapters = []

    def __init__(self, parent):
        Frame.__init__(self, parent)
        self.parent = parent
        #refresh function contents here
        self.chapters.append("hi")
        self.initUI()

    def initUI(self):
        self.parent.title("Latest Chapters")
        self.style = Style()
        #self.style.theme_use("default")
        scrollbar = Scrollbar(self)
        scrollbar.pack(side=RIGHT, fill=BOTH)

        self.listbox = Listbox(self, yscrollcommand=scrollbar.set)

        for newChapter in self.chapters:
            self.listbox.insert(END, newChapter)

        self.listbox.pack(side=TOP, fill=BOTH)
        scrollbar.config(command=self.listbox.yview)

        frame = Frame(self, relief=RAISED, borderwidth=1)
        frame.pack(fill=BOTH, expand=True)

        self.pack(fill=BOTH, expand=True)

        closeButton = Button(self, text="Close", command=self.master.destroy)
        closeButton.pack(side=RIGHT, padx=5, pady=5)
        refreshButton = Button(self, text="Refresh Now", command=self.refresh)
        refreshButton.pack(side=RIGHT)

    def refresh(self):
        self.chapters.append("hi2")  # add string to list
        self.listbox.insert(END, self.chapters[-1])  # update Listbox

def main():
    root = Tk()
    root.geometry("300x207+300+300")
    app = MainGUI(root)
    root.mainloop()

if __name__ == '__main__':
    main()

Here's what it would look like after clicking the Refresh Now button a few times.

screenshot of results

Upvotes: 2

Related Questions