pythontkintertk-toolkitpyautoguilistboxitem

How do I add a listbox?


How do I add a listbox, tried many ways but still can't work. How do I add a listbox, tried many ways but still can't work. How do I add a listbox, tried many ways but still can't work.

class App:
    def __init__(self, root):
        #setting title
        root.title("-----")
        #setting window size
        width=600
        height=500
        screenwidth = root.winfo_screenwidth()
        screenheight = root.winfo_screenheight()
        alignstr = '%dx%d+%d+%d' % (width, height, (screenwidth - width) / 2, (screenheight 
        - height) / 2)
        root.geometry(alignstr)
        root.resizable(width=False, height=False)

        List_box=tk.Listbox(root)
        List_box["borderwidth"] = "1px"
        ft = tkFont.Font(family='Times',size=10)
        List_box["font"] = ft
        List_box["fg"] = "#333333"
        List_box["justify"] = "left"
        List_box.place(x=50,y=130,width=192,height=293)


    def imageupload_command(self):
         global list
         list = fun.runbot.selectfile()
         app.List_box.insert('5555')

if __name__ == "__main__":
    root = tk.Tk()
    app = App(root)
    root.mainloop()


Solution

  • Your code is working fine.

    ...

    You can check here there is a Listbox in the window.

    problems:

    1. First you need to change List_box to self.List_box
    2. you are not calling the imageupload_command function to insert value in list box
    3. On this app.List_box.insert('5555') you need to replace app to self. You also need to provide the index in .insert method like self.List_box.insert(1,'5555')
    
    import tkinter as tk
    import tkinter.font as tkFont
    class App:
        def __init__(self, root):
            #setting title
            root.title("-----")
            #setting window size
            width=600
            height=500
            screenwidth = root.winfo_screenwidth()
            screenheight = root.winfo_screenheight()
            alignstr = '%dx%d+%d+%d' % (width, height, (screenwidth - width) / 2, (screenheight 
            - height) / 2)
            root.geometry(alignstr)
            root.resizable(width=False, height=False)
    
            self.List_box=tk.Listbox(root)
            self.List_box["borderwidth"] = "1px"
            ft = tkFont.Font(family='Times',size=10)
            self.List_box["font"] = ft
            self.List_box["fg"] = "#333333"
            self.List_box["justify"] = "left"
            self.List_box.place(x=50,y=130,width=192,height=293)
    
    
        def imageupload_command(self):
             global list
             list = fun.runbot.selectfile()
             self.List_box.insert(1,'5555')
    
    if __name__ == "__main__":
        root = tk.Tk()
        app = App(root)
        app.imageupload_command() # Calling the function.
        root.mainloop()