Tkinter Notebook Bind tabs to child control content
The content of each tab seen in the previous tutorial’s program is empty. The examples in this section focus on creating child control content within the tabs.
Example#1
In tab 1, add a tag subobject with the content "Python", at this time the tag object creation process can be referred to line 17, the focus is as follows.
label = Label(frame1, ...) # frame1 is the parent object of label
Add a function button sub-object with the name "Help" in tab 2. At this point, the function button object creation process can be found in line 19, with the following highlights.
btn = Button(frame2, ...) # frame2 is the parent object of btn
When the Help function button is clicked, the showinfo content message is listed.
from tkinter import *
from tkinter import messagebox
from tkinter.ttk import *
def msg():
messagebox.showinfo("Notebook","Welcome use Notebook")
root = Tk()
root.title("apidemos.com")
root.geometry("300x160")
notebook = Notebook(root) # Create Notebook
frame1 = Frame() # Create Frame1 # frame1 = Frame(root)
frame2 = Frame() # Create Frame2 # frame2 = Frame(root)
label = Label(frame1,text="Python") # Create label controls in Frame1
label.pack(padx=10,pady=10)
btn = Button(frame2,text="Help",command=msg)
btn.pack(padx=10,pady=10)
# The order in which notebook.add is called determines the order of the layout
# notebook.add(frame2,text="Frame 2") # Create tab 2 and insert Frame2 at the same time
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: