This is a problem with creating a tkinter notebook in a method in a class.
This question is solved by removing master, which is not my problem: Python Tkinter - How add a notebook class into a tk.toplevel?.
This question is about adding images, and it doesn't have the same structure (from which I might have been able to infer the solution): Python Tkinter Notebook widget.
When I create the notebook from __init__, the notebook opens just fine.
import tkinter as tk
from tkinter import ttk
class Stakeholders(tk.Frame):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
notebook = ttk.Notebook(self.master)
notebook.pack(pady=10, expand=True)
# create frames
frame1 = ttk.Frame(notebook, width=400, height=280)
frame2 = ttk.Frame(notebook, width=400, height=280)
frame1.pack(fill='both', expand=True)
frame2.pack(fill='both', expand=True)
# add frames to notebook
notebook.add(frame1, text='Tab 1')
notebook.add(frame2, text='Tab 2')
def create_window():
root = tk.Tk()
root.geometry('400x300')
root.title('Stakeholders')
Stakeholders(root)
root.mainloop()
if __name__ == "__main__":
create_window()
When I put the notebook in a method of the class, the window opens but there is no notebook, no tabs.
import tkinter as tk
from tkinter import ttk
class Stakeholders(tk.Frame):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.make_notebook()
def make_notebook(self):
self.notebook = ttk.Notebook(self)
self.notebook.pack(pady=10, expand=True)
frame1 = ttk.Frame(self.notebook, width=300, height=300)
frame2 = ttk.Frame(self.notebook, width=300, height=300)
frame1.pack(fill='both', expand=True)
frame2.pack(fill='both', expand=True)
self.notebook.add(frame1, text='Owner')
self.notebook.add(frame2, text='Architect')
def create_window():
root = tk.Tk()
root.geometry('400x300')
root.title('Stakeholders')
Stakeholders(root)
root.mainloop()
if __name__ == "__main__":
create_window()
Stakeholdersfor your second code..pack()layout where eachFrameand theNotebookgoes?Stakeholder, but instance ofStakeholderis invisible because no layout function is called on it.Stakeholder? It's created in a method of the class. It's not a subclass or a child class.self.masteris the parent ofself. It is the different.