Home > Net >  how to add a scroolbar to tkinter listbox
how to add a scroolbar to tkinter listbox

Time:10-07

Could you please give me a hint on how to add the scrollbar to the listbox not to the main window as it is right now? Thanks!

enter image description here

class App(tk.Frame):

    global Button
    global update_gui

    
    def __init__(self, master):
        global Button
        global showSelected
        global var
        tk.Frame.__init__(self, master)
        def showSelected():
            for i in self.listbox.curselection():
                print(self.listbox.get(i))


        scrollbar = tk.Scrollbar()
        scrollbar.pack(side = 'right', fill = 'both')
        
        self.listbox = tk.Listbox(selectmode= 'SINGLE')
        self.listbox.place(x=5,y=5)
        
        for item in products:
            self.listbox.insert("end", item)

        self.listbox.config(yscrollcommand = scrollbar.set)
        scrollbar.config(command = self.listbox.yview)

        self.Label=tk.Label(self, text="Result:", font=('Arial', 20))
        self.Label.place(x=300,y=800)
        Button=tk.Button(self, text="Start test", command=lambda:self.display())
        Button.place(x=350,y=170)

        self.text_widget = tk.Text(self,width=72, height=35)
        self.text_widget.place(x=100,y=205)

CodePudding user response:

The most common solution is to put the listbox and the scrollbars into a frame.

For example:

listboxframe = tk.Frame(self)
scrollbar = tk.Scrollbar(listboxframe)
self.listbox = tk.Listbox(listboxframe, selectmode= 'SINGLE')

scrollbar.pack(side = 'right', fill = 'y')
self.listbox.pack(side="top", fill="both", expand=True)

CodePudding user response:

anxansdmnada $clr-info-50$clr-info-50$clr-info-50$clr-info-50

  • Related