I can create this GUI
┏━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━┓
┃ @ ┃ hello world ┃ X ┃
┣━━━┻━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┻━━━┫
┃ ┃
┃ ┃
┃ ┃
┃ ┃ <- Frame 0
┃ ┃
┃ ┃
┃ ┃
┣━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┫
┃ OTHER ┃ <- Frame 1
┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
with the code shown below1.
However, I want to add a ttk.Notebook
to the Frame 0
.
┏━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━┓
┃ @ ┃ hello world ┃ X ┃
┣━━━┻━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┻━━━┫
┃ ┏━━━━━━━┳━━━━━━━┳━━━━━━━━━┳━━━━━━━┓ ┃
┃ ┃ Tab 0 ┃ Tab 1 ┃ Tab ... ┃ Tab n ┃ ┃
┃ ┣━━━━━━━┻━━━━━━━┻━━━━━━━━━┻━━━━━━━┫ ┃
┃ ┃ ┃ ┃
┃ ┃ ┃ ┃ <- Frame 0 with n tabs (Notebooks)
┃ ┃ ┃ ┃
┃ ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛ ┃
┣━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┫
┃ OTHER ┃ <- Frame 1
┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
Just adding the ttk.Notebook
to the tk.Frame
instance does not seem to work (i.e., tabControl = ttk.Notebook(self.frame0)
).
So, how do I add a ttk.Notebook
widget to a tk.Frame
?
1 GUI code:
import tkinter as tk
class MyApp(tk.Tk):
def __init__(self):
super().__init__()
self.title("hello world")
self.frame0 = tk.Frame(self, background="green")
self.frame1 = tk.Frame(self, background="blue")
self.frame0.pack(side="top", fill="both", expand=True)
self.frame1.pack(side="top", fill=tk.X, expand=True)
self.frame0_label0 = tk.Label(self.frame0, text=" ")
self.frame0_label0.grid(row=0, column=0)
self.frame1_label0 = tk.Label(self.frame1, text="OTHER")
self.frame1_label0.grid(row=0, column=0)
def main():
MyApp().mainloop()
if __name__ == "__main__":
main()
You add it exactly like you add any other widget. Create the widget, add it to the window with pack
, grid
, or place
, and then add tabs to it using the add
method.
For example:
tabControl = ttk.Notebook(self.frame0)
tabControl.grid(row=1, column=0, sticky="nsew")
tab1 = tk.Frame(tabControl)
tab2 = tk.Frame(tabControl)
tabControl.add(tab1, text="Tab 1")
tabControl.add(tab2, text="Tab 2")
For more information, see the official tkinter docs. Tkdocs.com also has an example