目前,我有这个:

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()


单击我的刷新按钮,将chapters数组附加一个附加项。 chapters数组已初始化,并在Listbox中显示为initUI()。当我单击刷新按钮时,我希望对chapters数组所做的更改也显示在GUI窗口中。我不确定该怎么做-到目前为止,我已经尝试过再次调用self.initUI(),但是它只会生成多个较小的帧。感谢所有帮助!

最佳答案

您可以通过将Listbox设置为MainGUI实例的属性来执行此操作,以便可以轻松地在refresh()方法中引用它。一旦完成,将新项目添加到chapters列表和Listbox实例很简单:

这就是我的意思:

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()


单击几次“立即刷新”后的外观如下。

python - 更新列表框的显示内容作为单独列表内容的更改?-LMLPHP

09-25 23:54