Tkinter Notebook Tutorial
Notebook is also a Widget container control. This control features a number of tabs, which can be used as sub-window content when selecting different tabs to see the contents of different sub-controls.
The syntax for using the Notebook( ) constructor is as follows.
Notebook(master, options)
The options parameters are as follows.
(1) height
: the default is to use the maximum possible height, if you set a value then the set height is used.
(2) padding
: set the extra space around the periphery of the Notebook, you can set 4 values to represent the space around left, top, right and bottom.
(3) width
: the default is to use the maximum possible width, if you set a value then use the set width.
The whole steps to build the Notebook frame are as follows.
(1) Use Notebook() to create a Notebook object, assuming the object name is notebook.
(2) Call the add() method using the notebook object.
add(child, text="xxx") # xxx is the name of the tab to be added
(3) The above code can insert the child object into the notebook and generate the "xxx" tab name at the same time. If we use the regular syntax of add( ) method, it has the following syntax format.
add(child, options)
The options parameters are as follows.
(1) compound
: You can set the position of the tabs in relation to each other when they contain both images and text.
(2) image
: The tabs are rendered as images.
(3) padding
: you can set the extra space for Notebook and Panel Pane.
(4) state
: possible values are normal, disabled, hidden, if it is disabled means it cannot be selected for use, if it is hidden means it is hidden.
(5) sticky
: point out the way the sub-window panel is configured, n/s/e/w represent North, South, East, West respectively.
(6) text
: the content of the string in the tab.
(7) underline
: the index calculated from 0, pointing out the first few letters containing underline.
Example#1
Simply build the frame of Notebook, the sub-objects in each tab of this program are Frame objects, which can be found in lines 11 and 12.
from tkinter import *
from tkinter.ttk import *
root = Tk()
root.title("apidemos.com")
root.geometry("300x160")
notebook = Notebook(root) # Create Notebook
frame1 = Frame() # Create Frame1
frame2 = Frame() # Create Frame2
notebook.add(frame1,text="Frame 1") # Create tab 1 and insert Frame1 at the same time
notebook.add(frame2,text="Frame 2") # Create tab 2 and insert Frame2 at the same time
notebook.pack(padx=10,pady=10,fill=BOTH,expand=TRUE)
root.mainloop()
Output: