Python – scrollable listbox within a grid using tkinter

gridpythonscrollbartkinter

I'm new to this place and tkinter. I am stuck at making a scrollable listbox or canvas. I have tried both widgets. Within this listbox or canvas, I have several entry and label widgets. The origin point is R0,C0. I used row/columnconfigure to stretch the listbox or canvas.

In the main window, I had 4 buttons on row four to column four (0,4->4,4). I placed the scrollbar on column 5. I attempted to use the grid method. The issue I am having is making the scrollbar functional.

Note: Turning the mainframe into a class is only one of the ways I have tried. Packing the scrollbar on the right has worked, with the listbox/canvas packed on the left. However, the listbox/canvas widget that the scrollbar is commanded to does not scroll the listbox/canvas. Also, adding many entry boxes does not cause the listbox/canvas to scroll. Help please.

from tkinter import *
from tkinter.ttk import *

Style().configure("B.TFrame", relief="flat",
background="blue")
Style().configure("R.TFrame", relief="flat",
background="red")
Style().configure("R.TLabel", background="red")

class Application(Frame): 
    def __init__(self, master=None):
        Frame.__init__(self, master, style="B.TFrame") 
        self.grid(sticky=N+S+E+W) 
        self.mainframe()

    def mainframe(self):
        top=self.winfo_toplevel()
        self.menuBar = Menu(top)
        top["menu"] = self.menuBar
        self.subMenu = Menu(self.menuBar, tearoff=0)
        self.subMenu2 = Menu(self.menuBar, tearoff=0)
        self.menuBar.add_cascade(label="File", menu=self.subMenu)
        self.menuBar.add_cascade(label="About", menu=self.subMenu2)
        self.subMenu.add_command(label="Open")
        self.subMenu.add_command(label="Save")
        self.subMenu.add_command(label="Exit")
        self.subMenu2.add_command(label="About")
        self.subMenu2.add_command(label="Help")



        self.data = Listbox (self, bg='red')
        scrollbar = Scrollbar(self.data, orient=VERTICAL)

        self.add = Button(self, text="")
        self.remove = Button(self, text="")
        self.run = Button(self, text="")
        self.stop = Button(self, text="")

        self.data.grid (row=0, column=0, rowspan=4, columnspan=4, sticky=N+E+S+W)
        self.data.columnconfigure(1, weight=1)
        self.data.columnconfigure(3, weight=1)

        self.add.grid(row=4,column=0,sticky=EW)       
        self.remove.grid(row=4,column=1,sticky=EW)
        self.run.grid(row=4,column=2,sticky=EW)
        self.stop.grid(row=4,column=3,sticky=EW)
        scrollbar.grid(column=5, sticky=N+S)

Best Answer

Without any content in the listbox, there's nothing to scroll...

This seems to work though (shortened the example a bit). See also the example at the scrollbar documentation.

class Application(Frame):   
    def __init__(self,  master=None):
        Frame.__init__(self, master)    
        self.grid(sticky=N+S+E+W)   
        self.mainframe()

    def mainframe(self):                
        self.data = Listbox(self, bg='red')
        self.scrollbar = Scrollbar(self.data, orient=VERTICAL)
        self.data.config(yscrollcommand=self.scrollbar.set)
        self.scrollbar.config(command=self.data.yview)

        for i in range(1000):
            self.data.insert(END, str(i))

        self.run = Button(self, text="run")
        self.stop = Button(self, text="stop")

        self.data.grid(row=0, column=0, rowspan=4,
                   columnspan=2, sticky=N+E+S+W)
        self.data.columnconfigure(0, weight=1)

        self.run.grid(row=4,column=0,sticky=EW)
        self.stop.grid(row=4,column=1,sticky=EW)

        self.scrollbar.grid(column=2, sticky=N+S)

a = Application()
a.mainframe()
a.mainloop()
Related Topic