Welcome, guest | Sign In | My Account | Store | Cart

This simple widget is a notebook that changes its height to fit the height of the selected tab.

This recipe also answer this question: http://stackoverflow.com/questions/25675098/change-window-size-each-tab-tkinter-notebook

Python, 26 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
from tkinter.ttk import Notebook

class Autoresized_Notebook(Notebook):
  def __init__(self, master=None, **kw):

    Notebook.__init__(self, master, **kw)
    self.bind("<<NotebookTabChanged>>", self._on_tab_changed)

  def _on_tab_changed(self,event):
    event.widget.update_idletasks()

    tab = event.widget.nametowidget(event.widget.select())
    event.widget.configure(height=tab.winfo_reqheight())

if __name__== "__main__":
    from tkinter import Frame, Tk
    root = Tk()      

    notebook = Autoresized_Notebook(root)    
    notebook.add(Frame(notebook, width=400, height=200, name="a"),text="TAB 1")
    notebook.add(Frame(notebook, width=400, height=300, name="b"),text="TAB 2")
    notebook.add(Frame(notebook, width=400, height=100, name="c"),text="TAB 3")

    notebook.pack()

    root.mainloop()
Created by Miguel Martínez López on Tue, 6 Dec 2016 (MIT)
Python recipes (4591)
Miguel Martínez López's recipes (56)

Required Modules

  • (none specified)

Other Information and Tasks