Home > Blockchain >  notebook.add() returns bad window path name
notebook.add() returns bad window path name

Time:11-13

I'm trying to have a ttk notebook display 2 tabs based on 2 classes, the code is the following :

import tkinter as tk
from tkinter import ttk


class Dashboard:
    def __init__(self,master):
        self.master = master
        self.frame = tk.Frame(self.master, width = 400, height = 400, bg = "red")
        self.frame.pack(expand = True)
class Options :
    def __init__(self,master):
        self.master = master
        self.frame = tk.Frame(self.master, width = 400, height = 400, bg = "blue")
        self.frame.pack(expand = True)
class Tabs:
    def __init__(self,master):
        self.master = master
        self.notebook = ttk.Notebook(self.master)
        self.options = Options(self.notebook)
        self.dashboard = Dashboard(self.notebook)
        self.notebook.add(self.options)
        self.notebook.add(self.dashboard)
        self.notebook.pack(pady=10, expand=True)


class MainGUI:
    def __init__(self,master):
        self.master = master
        self.tabs = Tabs(self.master)


if __name__ == '__main__':
    root = tk.Tk()
    gui = MainGUI(root)
    root.mainloop()

However, when I run this I have this error (only happens when I try to use the "add" method of the ttk.Notebook) :_tkinter.TclError: bad window path name "<__main__.Options object at 0x0000016E51653DC0>"

I don't understand why my syntax is wrong, can someone help me with that ? Thanks a lot !!

CodePudding user response:

A child pane widget is usually a Frame or a subclass of one. I'm not exactly sure what you're doing, but you can get rid of the error (and a similar one for the other class) by deriving both of them from the tkinter Frame class like this:

import tkinter as tk
from tkinter import ttk


class Dashboard(tk.Frame):
    def __init__(self,master):
        super().__init__(master)
        self.frame = tk.Frame(self.master, width=400, height=400, bg="red")
        self.frame.pack(expand=True)


class Options(tk.Frame):
    def __init__(self,master):
        super().__init__(master)
        self.frame = tk.Frame(self.master, width=400, height=400, bg="blue")
        self.frame.pack(expand=True)

class Tabs:
    def __init__(self,master):
        self.master = master
        self.notebook = ttk.Notebook(self.master)
        self.options = Options(self.notebook)
        self.dashboard = Dashboard(self.notebook)
        self.notebook.add(self.options)
        self.notebook.add(self.dashboard)
        self.notebook.pack(pady=10, expand=True)


class MainGUI:
    def __init__(self,master):
        self.master = master
        self.tabs = Tabs(self.master)


if __name__ == '__main__':
    root = tk.Tk()
    gui = MainGUI(root)
    root.mainloop()
  • Related