-2

coding a scrollable frame with a ttk.notebook inside works. But the scrollbar / notebook has a fixed size. How can I change it?

from tkinter import Canvas, Scrollbar, Button, Tk
from tkinter.ttk import Frame, Notebook

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>', _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>', _configure_canvas)

root = Tk()

class Overview:

    def __init__(self):

        #mainframe to make a scrollable window
        self.mainframe = VerticalScrolledFrame(root)
        self.mainframe.grid(row=0, column=0)

        # create a notebook  
        self.TNotebook_Overview = Notebook(self.mainframe.interior)        
        self.TNotebook_Overview.grid(row=0, column=0)
        self.TNotebook_Overview.configure(takefocus="")     

        self.Frame_Overview = Frame(self.TNotebook_Overview) 

        self.TNotebook_Overview.add(self.Frame_Overview)    
        self.TNotebook_Overview.tab(0, text="Table", compound="left",underline="-1", )

        buttons = []
        for i in range(30):
            buttons.append(Button(self.Frame_Overview, text="Button " + str(i)))
            buttons[-1].grid(column=0, row=i)


if __name__ == "__main__":

    ov = Overview()
    root.title('Overview Items Database')
    root.geometry('800x800+10+10')
    root.configure(background="#4C7274")
    root.grab_set()
    root.mainloop() 

I expect an expanded scrollable notebook/frame filled entire Tk() window.

Because the "ai" of stackoverflow don't allow use code from another thread, here is code https://pastebin.com/ykJGViAz

Stop harming Monica
  • 10,533
  • 1
  • 27
  • 48
newsnake
  • 1
  • 1
  • Please fix the formatting of the code. – Bryan Oakley Jun 17 '19 at 16:22
  • I've try to format code, but I can't because I've use too much code from another post. So I can't edit it. The ````class VerticalScrolledFrame(Frame):```` is complete from another thread and that's too much, so I can't edit it – newsnake Jun 18 '19 at 11:33

1 Answers1

0

Inside init, do this instead self.mainframe.grid(row=0, column=0, sticky="nsew") self.TNotebook_Overview.grid(row=0, column=0, sticky="nsew") This tells the frame to fill as much space as it needs

wolfy
  • 113
  • 1
  • 6