Python Tkinter notebook Widget

In this Python Tkinter tutorial, we will learn how to create a Notebook widget in Python Tkinter and we will also cover different examples related to Tkinter Notebook. And, we will cover these topics.

  • Python Tkinter notebook widget
  • Python Tkinter notebook background-color
  • Python Tkinter notebook style
  • Python Tkinter notebook tab size
  • Python Tkinter notebook select tab
  • Python Tkinter notebook disable tab

Python Tkinter notebook widget

In this section, we will learn how to create a notebook widget in the python Tkinter.

As we know Notebook is a tiny book with blank pages if we want we can write anything in it. As a notebook, the Tkinter Notebook widget works the same.

Notebook widget allows the user to create the tabs in the application window. Tabs are to be used to separate the workspace.

Code:

In the following code, we create a notebook widget in which the user has created the tabs “Tab 1” and “Tab 2” we also use the frame for these tabs.

  • from tkinter.ttk import Notebook we Import Notebook widget.
  • notebook.add() is used to add tabs in notebook.
from tkinter import *
from tkinter.ttk import Notebook


ws = Tk()
ws.geometry('400x300')
ws.title('Python Guides Notebook')


notebook = Notebook(ws)
notebook.pack(pady=10, expand=True)


frame1 = Frame(notebook, width=400, height=280)
frame2 = Frame(notebook, width=400, height=280)

frame1.pack(fill='both', expand=True)
frame2.pack(fill='both', expand=True)



notebook.add(frame1, text='Tab 1')
notebook.add(frame2, text='Tab 2')


ws.mainloop()

Output:

In the following output, we see a notebook widget is created with two tabs “Tab1 ” and “Tab 2”.

Python Tkinter notebook widget
Notebook widget Output

Read Python Tkinter Menu bar

Python Tkinter notebook background-color

In this section, we will learn about how to add Notebook background color in Python Tkinter.

We imported .ttk Notebook, style library to create a background color on tabs we use geometry to give the size of the window on which our tabs are created and style is applied.

Code:

In the below code we have created the notebook widget background color which helps to make a colorful GUI. We use add() to create tabs inside a notebook.

  • style.theme_use(‘default’) is used to change the theme of the widget.
  • note.add() is used to create tabs inside a window.
from tkinter import *
from tkinter.ttk import Notebook, Style


ws = Tk()


ws.geometry("700x350")

# Create an instance of ttk style
style = Style()
style.theme_use('default')
style.configure('TNotebook.Tab', background="Red")
style.map("TNotebook", background= [("selected", "red")])

# Create a Notebook widget
note =Notebook(ws)

# Add a frame for adding a new tab
frame1= Frame(note, width= 400, height=180)

# Adding the Tab Name
note.add(frame1, text= 'Tkinter-1')
frame2 = Frame(note, width= 400, height=180)
note.add(frame2, text= "Tkinter-2")

note.pack(expand= True, fill=BOTH, padx= 5, pady=5)
ws.mainloop()

Output:

In the following output, we see the Notebook widget on which tabs are created with colorful backgrounds.

Pyyhon Tkinter Notebook Widget background color
Background color Output

Read Python Tkinter Calculator

Python Tkinter notebook style

in this section, we will learn how to add notebook style in Python Tkinter.

Style refers to the design of an object or method to fulfill the task. We import .ttk Notebook, Style library to give a different style to Notebook from which our Notebook looks attractive.

COde:

In the following code, we create a Notebook widget inside the widget we use add() to create the tabs and also give the style to these tabs.

  • style.theme_create() is used to give access to the style database.
  • style.theme_use () is used to change the current theme.
  • notebook.add () is used to add tabs inside the widget.
from tkinter import *
from tkinter.ttk import Notebook, Style


ws = Tk()

Mysky = "#DCF0F2"
Myyellow = "#F2C84B"

style = Style()

style.theme_create( "dummy", parent="alt", settings={
        "TNotebook": {"configure": {"tabmargins": [2, 5, 2, 0] } },
        "TNotebook.Tab": {
            "configure": {"padding": [5, 1], "background": Mysky },
            "map":       {"background": [("selected", Myyellow)],
                          "expand": [("selected", [1, 1, 1, 0])] } } } )

style.theme_use("dummy")

notebook = Notebook(ws)
frame1 = Frame(notebook, width=300, height=200)
notebook.add(frame1, text = 'First')
frame2 = Frame(notebook, width=300, height=200)
notebook.add(frame2, text = 'Second')
notebook.pack(expand=1, fill='both', padx=7, pady=7)

Button(ws, text='dummy!').pack(fill='x')

ws.mainloop()

Output:

In the following output, we see a notebook widget inside the widget tabs is created on which we apply the style theme. when we click on tabs the style and color of the tab change.

Python tkinter Notebook Style
Notebook Style Output

Read Python Tkinter Checkbutton

Python Tkinter notebook tab size

In this section, we will learn how to change the notebook tab size in Python Tkinter.

Size is defined as how big or small an object is. Notebook tab size refers to how big or small the tab size we import .ttk import Notebook, style library to create a notebook widget and give style to the notebook.

Code:

In the following code, we create a notebook widget inside the widget tabs are added we use height and width to change the size of the tab.

  • width() is used to change the width of the tab.
  • Height() is used to change the height of the tab.
from tkinter import *
from tkinter.ttk import Notebook, Style


ws= Tk()
style = Style()
style.theme_create( "MyStyle", parent="alt", settings={
        "TNotebook": {"configure": {"tabmargins": [2, 5, 2, 0] } },
        "TNotebook.Tab": {"configure": {"padding": [100, 100] },}})

style.theme_use("MyStyle")

notebook = Notebook(ws, width=250, height=250)
tab1 = Frame(notebook)
notebook.add(tab1, text = 'Welcome to tab one')
tab2 = Frame(notebook)
notebook.add(tab2, text = 'Welcome to tab two')
notebook.pack(expand=True, fill=BOTH)

Button(ws, text='Some Text!').pack(fill=X)

ws.mainloop()

Output:

In the following output, we see a Notebook widget inside widget tabs are added. As we see the size of the tabs are too big to change the size of the tab we resize the height and width.

Python Tkinter Tab Size
Notebook Tab Size Output

Read Python Tkinter Radiobutton

Python Tkinter notebook select tab

In this section, we will learn how to select tabs in the Python Tkinter notebook.

We imported the .ttk import Notebook library to create the Notebook widget. We use geometry to give the size of the window on which our tabs are created. We move the cursor over the tab and select randomly any tab by click on them.

Code:

In the following code, we create a Notebook widget by importing the .ttk import notebook library also import the time to handle the time-related task.

Button(): When we attach some function to a button they automatically call by clicking on them.

from tkinter import *
import time
from tkinter.ttk import Notebook


ws=Tk()

ws.config(width=330,height=250)

notebook=Notebook(ws)
notebook.place(x=0,y=0)

Tablist=[]
i=0
while i<6:    
     Tablist.append(Frame(ws))
     Tablist[i].config(width=330,height=200,background='white')
     i+=1

i=0
while i<6: 
    notebook.add(Tablist[i],text='tab'+str(i))
    i+=1

def LoopTabs():
    i=0
    while i<6:
         notebook.select(i)
         time.sleep(2)
         i+=1

button=Button(ws,text='Click here',command=LoopTabs)
button.place(x=20,y=180)
ws.mainloop()

Output:

In the following output, we see a notebook widget is created inside the widget tabs are placed when the user clicks on tab1. Tab1 tab is selected like this we move further if the user clicks on the next tab “Tab2”.Tab2 tab is selected.

Python Tkinter notebook Select Tab3
Notebook select tab Output

Read Python Tkinter Exit Program

Python Tkinter notebook disable tab

In this section, we will learn Notebook disable tab in Python Tkinter.

Before learning Notebook disable tab we should have some piece of knowledge about disable. Disable means which is unable to work.

In Python Tkinter Notebook disable tab we create a notebook inside the notebook we add two tabs. If the first tab is diable they move to the next tab.

Code:

In the following code, we imported some libraries .ttk import Notebook, Style for creating the Notebook, and Style is used for giving some style to our notebook.

  • Style.layout is used to disable the first tab.
  • text.insert() is used for inserting some text in notebook.
  • note.add() is used to add frame in notebook.
from tkinter import *
from tkinter.ttk import Notebook, Style

ws = Tk()

style = Style()

style.layout('TNotebook.Tab', []) # turn off tabs

note = Notebook(ws)

frame1 = Frame(note)
text = Text(frame1, width=40, height=10)
text.insert('end', 'page0 : a text widget')
text.pack(expand=1, fill='both')
note.add(frame1)

frame2 = Frame(note)
lablel = Label(frame2, text='page1 : a label')
lablel.pack(expand=1, fill='both')
note.add(frame2)

note.pack(expand=1, fill='both', padx=5, pady=5)

def Do_Something():
    note.select(1)

ws.after(3000, Do_Something)
ws.mainloop()

Output:

In the following output, we can see page0 and page 1 when we move our cursor over page 0 they get disable and move to the next page page1.

Notebook disable tab
Notebook disable tab Output

You may like the following Python Tkinter tutorials:

In this tutorial, we discuss the Python Tkinter Notebook widget and we also covered different examples. Here is the list of examples that we have covered.

  • Python Tkinter notebook widget
  • Python Tkinter notebook background-color
  • Python Tkinter notebook style
  • Python Tkinter notebook tab size
  • Python Tkinter notebook select tab
  • Python Tkinter notebook disable tab