Tkinter scrollbar for frame

后端 未结 4 772
难免孤独
难免孤独 2020-11-22 09:47

My objective is to add a vertical scroll bar to a frame which has several labels in it. The scroll bar should automatically enabled as soon as the labels inside the frame ex

4条回答
  •  长发绾君心
    2020-11-22 10:37

    Please note that the proposed code is only valid with Python 2

    Here is an example:

    from Tkinter import *   # from x import * is bad practice
    from ttk import *
    
    # http://tkinter.unpythonic.net/wiki/VerticalScrolledFrame
    
    class VerticalScrolledFrame(Frame):
        """A pure Tkinter scrollable frame that actually works!
        * Use the 'interior' attribute to place widgets inside the scrollable frame
        * Construct and pack/place/grid normally
        * This frame only allows vertical scrolling
    
        """
        def __init__(self, parent, *args, **kw):
            Frame.__init__(self, parent, *args, **kw)            
    
            # create a canvas object and a vertical scrollbar for scrolling it
            vscrollbar = Scrollbar(self, orient=VERTICAL)
            vscrollbar.pack(fill=Y, side=RIGHT, expand=FALSE)
            canvas = Canvas(self, bd=0, highlightthickness=0,
                            yscrollcommand=vscrollbar.set)
            canvas.pack(side=LEFT, fill=BOTH, expand=TRUE)
            vscrollbar.config(command=canvas.yview)
    
            # reset the view
            canvas.xview_moveto(0)
            canvas.yview_moveto(0)
    
            # create a frame inside the canvas which will be scrolled with it
            self.interior = interior = Frame(canvas)
            interior_id = canvas.create_window(0, 0, window=interior,
                                               anchor=NW)
    
            # track changes to the canvas and frame width and sync them,
            # also updating the scrollbar
            def _configure_interior(event):
                # update the scrollbars to match the size of the inner frame
                size = (interior.winfo_reqwidth(), interior.winfo_reqheight())
                canvas.config(scrollregion="0 0 %s %s" % size)
                if interior.winfo_reqwidth() != canvas.winfo_width():
                    # update the canvas's width to fit the inner frame
                    canvas.config(width=interior.winfo_reqwidth())
            interior.bind('', _configure_interior)
    
            def _configure_canvas(event):
                if interior.winfo_reqwidth() != canvas.winfo_width():
                    # update the inner frame's width to fill the canvas
                    canvas.itemconfigure(interior_id, width=canvas.winfo_width())
            canvas.bind('', _configure_canvas)
    
    
    if __name__ == "__main__":
    
        class SampleApp(Tk):
            def __init__(self, *args, **kwargs):
                root = Tk.__init__(self, *args, **kwargs)
    
    
                self.frame = VerticalScrolledFrame(root)
                self.frame.pack()
                self.label = Label(text="Shrink the window to activate the scrollbar.")
                self.label.pack()
                buttons = []
                for i in range(10):
                    buttons.append(Button(self.frame.interior, text="Button " + str(i)))
                    buttons[-1].pack()
    
        app = SampleApp()
        app.mainloop()
    

    It does not yet have the mouse wheel bound to the scrollbar but it is possible. Scrolling with the wheel can get a bit bumpy, though.

    edit:

    to 1)
    IMHO scrolling frames is somewhat tricky in Tkinter and does not seem to be done a lot. It seems there is no elegant way to do it.
    One problem with your code is that you have to set the canvas size manually - that's what the example code I posted solves.

    to 2)
    You are talking about the data function? Place works for me, too. (In general I prefer grid).

    to 3)
    Well, it positions the window on the canvas.

    One thing I noticed is that your example handles mouse wheel scrolling by default while the one I posted does not. Will have to look at that some time.

提交回复
热议问题