A scrollable list in a grid using tkinter

I am new to this place and tkinter. I am stuck in creating a scrollable list or canvas. I tried both widgets. Inside this list or canvas, I have several input widgets and labels. The reference point is R0, C0. I used the line / columnconfigure to stretch the list or canvas.

In the main window, I had 4 buttons in the fourth row to the fourth column (0.4-> 4.4). I placed the scroll bar in column 5. I tried using the grid method. The problem I am facing is making scroll functionality.

Note. Turning a mainframe into a class is just one of the ways I've tried. Laying the scroll bar on the right worked, with the list / canvas packed on the left. However, the listbox / canvas widget that the scroll command controls does not scroll through the list / canvas. In addition, adding many input fields does not scroll the list / canvas. Help me 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)
+5
source share
2 answers

Without the contents in the list there is nothing to scroll ...

This seems to work (slightly shortened the example). See also an example in the scroll 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()
+7
source

command , yscrollcommand . , - .

yscrollcommand " Y, . set , , , .

command scorllbar : " , ". yview xview , Y X.

:

self.data.config(yscrollcommand=self.scrollbar.set)
scrollbar.config(command=self.data.yview)
+2

All Articles