Autohide Tkinter canvas scrollbar with pack geometry

前端 未结 1 959
北恋
北恋 2021-01-20 23:09

I need help with autohiding tkinter scrollbars when it is not needed. I have found from effbot.org this code that autohides the scrollbar but only with grid geometry. I am not u

1条回答
  •  孤街浪徒
    2021-01-20 23:40

    I adapted the example from effbot.org to pack method:

    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.pack_forget()
            else:
                if self.cget("orient") == HORIZONTAL:
                    self.pack(fill=X)
                else:
                    self.pack(fill=Y)
            Scrollbar.set(self, lo, hi)
        def grid(self, **kw):
            raise TclError, "cannot use grid with this widget"
        def place(self, **kw):
            raise TclError, "cannot use place with this widget"
    
    # create scrolled canvas
    
    root = Tk()
    
    hscrollbar = AutoScrollbar(root, orient=HORIZONTAL)
    canvas = Canvas(root,
                    xscrollcommand=hscrollbar.set)
    canvas.pack(side=TOP, fill=BOTH, expand=True)
    hscrollbar.pack()
    
    hscrollbar.config(command=canvas.xview)
    
    # make the canvas expandable
    root.grid_rowconfigure(0, weight=1)
    root.grid_columnconfigure(0, weight=1)
    
    # create canvas contents
    
    frame = Frame(canvas)
    frame.rowconfigure(1, weight=1)
    frame.columnconfigure(1, weight=1)
    
    rows = 5
    for i in range(1,rows):
        for j in range(1,10):
            button = Button(frame, padx=7, pady=7, 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()
    

    0 讨论(0)
提交回复
热议问题