How could I get a Frame with a scrollbar in Tkinter? How could I get a Frame with a scrollbar in Tkinter? tkinter tkinter

How could I get a Frame with a scrollbar in Tkinter?


If you can use Tix, there is the ScrolledWindow widget which has a window Frame and one or two Scrollbar widgets:

import Tix as tkr= tk.Tk()r.title("test scrolled window")sw= tk.ScrolledWindow(r, scrollbar=tk.Y) # just the vertical scrollbarsw.pack(fill=tk.BOTH, expand=1)for i in xrange(10):    e= tk.Entry(sw.window)    e.pack()r.mainloop()

Change the size of the root window. You will want to add code to the focus_get event of the Entry widgets to scroll the ScrolledWindow when tabbing through the keyboard.

Otherwise, you will have to use a Canvas widget (you can add Label, Entry and Text subwidgets) and write a lot more code yourself to implement your required functionality.


The following is an example of auto hiding scrollbars that only works if you just use the grid geometry manager, taken from the effbot.org documentation:

from tkinter import *class AutoScrollbar(Scrollbar):    # A scrollbar that hides itself if it's not needed.    # Only works if you use the grid geometry manager!    def set(self, lo, hi):        if float(lo) <= 0.0 and float(hi) >= 1.0:            # grid_remove is currently missing from Tkinter!            self.tk.call("grid", "remove", self)        else:            self.grid()        Scrollbar.set(self, lo, hi)    def pack(self, **kw):        raise TclError("cannot use pack with this widget")    def place(self, **kw):        raise TclError("cannot use place with this widget")# create scrolled canvasroot = Tk()vscrollbar = AutoScrollbar(root)vscrollbar.grid(row=0, column=1, sticky=N+S)hscrollbar = AutoScrollbar(root, orient=HORIZONTAL)hscrollbar.grid(row=1, column=0, sticky=E+W)canvas = Canvas(root, yscrollcommand=vscrollbar.set, xscrollcommand=hscrollbar.set)canvas.grid(row=0, column=0, sticky=N+S+E+W)vscrollbar.config(command=canvas.yview)hscrollbar.config(command=canvas.xview)# make the canvas expandableroot.grid_rowconfigure(0, weight=1)root.grid_columnconfigure(0, weight=1)# create canvas contentsframe = Frame(canvas)frame.rowconfigure(1, weight=1)frame.columnconfigure(1, weight=1)rows = 5for i in range(1, rows):    for j in range(1, 10):        button = Button(frame, text="%d, %d" % (i,j))        button.grid(row=i, column=j, sticky='news')canvas.create_window(0, 0, anchor=NW, window=frame)frame.update_idletasks()canvas.config(scrollregion=canvas.bbox("all"))root.mainloop()