如何将项目从列表附加到列表框?

How to append items from List to Listbox?

在我的程序中有一个字段,用户可以在其中输入姓名,之后该姓名将保存在“个人”列表中。

之后,用户转到一个新页面,在那里他可以查看包含所有输入名称的列表,如果需要,return 到上一页添加另一个名称。

万事大吉,但在“个人”列表中输入的所有名称都没有显示在下一页的列表框中,而我可以在控制台的列表中看到它们的显示方式。

我想知道如何将这些名称从列表附加到列表框。

我的代码:

from tkinter import *
import tkinter.ttk as ttk


class CollegeApp(Tk):
    def __init__(self):
        Tk.__init__(self)
        container = ttk.Frame(self)
        container.pack(side="top", fill="both", expand=True)
        self.frames = {}
        for F in (IndividPage, listCheckPage):
            frame = F(container, self)
            self.frames[F] = frame
            frame.grid(row=0, column=0, sticky="nsew")
        self.show_frame(IndividPage)
        self.lift()

    def show_frame(self, cont):
        frame = self.frames[cont]
        frame.tkraise()


class IndividPage(ttk.Frame):

    def __init__(self, parent, controller):
        self.controller = controller
        ttk.Frame.__init__(self, parent)
        self.userEntry()

    def userEntry(self):
        headingTest = Label(self, text="Enter your UserName:", font="Arial 20")
        headingTest.grid(row=0, column=0, pady=5, padx=5)

        self.usernameEnter = Entry(self, width=40)
        self.usernameEnter.grid(row=0, column=1, padx=5, pady=5)

        confirmBtn = Button(self, text="Confirm User", font="Arial 16",
                            command=self.confirm)

        confirmBtn.config(height=4, width=12)
        confirmBtn.grid(row=2, column=2, sticky=E, padx=45, pady=360)

    def confirm(self):
        if self.add_to_indivList():
            self.controller.show_frame(listCheckPage)

    def add_to_indivList(self):
        users = self.usernameEnter.get()
        Individuals.append(users)
        processedInd = list(dict.fromkeys(Individuals))
        self.controller.show_frame(listCheckPage)
        print(processedInd)


class listCheckPage(ttk.Frame):
    def __init__(self, parent, controller):
        self.controller = controller
        ttk.Frame.__init__(self, parent)
        self.userEntry()

    def userEntry(self):

        indList = Listbox(self)
        indList.config(height=13, width=15)
        indList.grid(row=1, column=0, padx=10, pady=20)
        for user in Individuals:
            indList.insert(user)

        INDHeading = Label(self, text="Individuals", font="Arial 16")
        INDHeading.grid(row=0, column=0, pady=0, padx=15, sticky=N)

        addUserBtn = Button(self, text="Add User", font="Arial 16",
                            command=lambda: self.controller.show_frame(IndividPage))
        addUserBtn.config(height=3, width=80)
        addUserBtn.grid(row=2, column=0, columnspan=5, pady=0, sticky=N)

        BackBtn = Button(self, text="BACK", font="Arial 16",
                         command=lambda:self.controller.show_frame(IndividPage))
        BackBtn.config(height=2, width=12)
        BackBtn.grid(row=4, column=0, sticky=W, padx=0, pady=0)


if __name__ == '__main__':
    Individuals = []
    app = CollegeApp()
    app.geometry("800x500")
    app.title('Points Counter')
    app.mainloop()

好吧,由于我是新手,研究了很久,还是找到了解决问题的方法(感谢Reddit用户)。

问题是“userEntry”函数仅在创建“listCheckPage”框架期间被调用。

当我将新用户添加到“Individuals”列表然后提升“listCheckPage”框架时,它没有调用“userEntry”函数,因此列表框保持为空。

解决方法是:

from tkinter import *
import tkinter.ttk as ttk


class CollegeApp(Tk):
    def __init__(self):
        Tk.__init__(self)
        self.container = ttk.Frame(self)
        self.container.pack(side="top", fill="both", expand=True)
        self.frames = {}
        for F in (IndividPage, listCheckPage):
            frame = F(self.container, self)
            self.frames[F] = frame
            frame.grid(row=0, column=0, sticky="nsew")
        self.show_frame(IndividPage)
        self.lift()

    def show_frame(self, cont):
        frame = self.frames[cont]
        frame.tkraise()

    def show_list_frame(self):
        list_frame = listCheckPage(self.container, self)
        list_frame.grid(row=0, column=0, sticky="nsew")
        list_frame.tkraise()


class IndividPage(ttk.Frame):

    def __init__(self, parent, controller):
        self.controller = controller
        ttk.Frame.__init__(self, parent)
        self.userEntry()

    def userEntry(self):
        headingTest = Label(self, text="Enter your UserName:", font="Arial 20")
        headingTest.grid(row=0, column=0, pady=5, padx=5)

        self.usernameEnter = Entry(self, width=40)
        self.usernameEnter.grid(row=0, column=1, padx=5, pady=5)

        confirmBtn = Button(self, text="Confirm User", font="Arial 16",
                            command=self.confirm)

        confirmBtn.config(height=4, width=12)
        confirmBtn.grid(row=2, column=2, sticky=E, padx=45, pady=360)

    def confirm(self):
        if self.add_to_indivList():
            pass

    def add_to_indivList(self):
        users = self.usernameEnter.get()
        Individuals.append(users)
        processedInd = list(dict.fromkeys(Individuals))
        self.controller.show_list_frame()
        print(processedInd)


class listCheckPage(ttk.Frame):
    def __init__(self, parent, controller):
        self.controller = controller
        ttk.Frame.__init__(self, parent)
        self.userEntry()

    def userEntry(self):
        indList = Listbox(self)
        indList.config(height=13, width=15)
        indList.grid(row=1, column=0, padx=10, pady=20)
        for user in Individuals:
            indList.insert(END, user)

        INDHeading = Label(self, text="Individuals", font="Arial 16")
        INDHeading.grid(row=0, column=0, pady=0, padx=15, sticky=N)

        addUserBtn = Button(self, text="Add User", font="Arial 16",
                            command=lambda: self.controller.show_frame(IndividPage))
        addUserBtn.config(height=3, width=80)
        addUserBtn.grid(row=2, column=0, columnspan=5, pady=0, sticky=N)

        BackBtn = Button(self, text="BACK", font="Arial 16",
                         command=lambda: self.controller.show_frame(IndividPage))
        BackBtn.config(height=2, width=12)
        BackBtn.grid(row=4, column=0, sticky=W, padx=0, pady=0)


if __name__ == '__main__':
    Individuals = []
    app = CollegeApp()
    app.geometry("800x500")
    app.title('Points Counter')
    app.mainloop()