Alexander Urum
Alexander Urum

Reputation: 131

How to append items from List to Listbox?

In my program there is a field in which the user enters a name, after which this name is saved in the "Individuals" list.

After that, the user goes to a new page, where he can view the list with all entered names and, if necessary, return to the previous page to add another name.

Everything would be fine, but all the names entered in the "Individuals" list are not displayed in the listbox on the next page, while I can see how they appear in the list in the console.

I would like to know how to append those names from list to listbox.

My code:

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

Upvotes: 1

Views: 163

Answers (1)

Alexander Urum
Alexander Urum

Reputation: 131

Okay, since I'm a newbie, it took a long time to research, but I still found a solution to the problem (thanks to Reddit users).

The problem is that the "userEntry" function is called only during the creation of the "listCheckPage" frame.

When I add a new user to the "Individuals" list and then raise the "listCheckPage" frame, it is not calling the "userEntry" function so the Listbox remains empty.

The solution is that:

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

Upvotes: 1

Related Questions