Writing a Python3 ttk.Notebook

Rich Shepard rshepard at appl-ecosys.com
Fri Jan 15 15:51:19 EST 2021


I want to replace the menu on my application with the more appropriate
notebook. After looking at examples in my reference books and on the Web I
still cannot get it working properly.

Here's a small example (nbtest.py):
---8< -----------------------
#!/usr/bin/env python3

import tkinter as tk
from tkinter import ttk
from tkinter.ttk import Notebook as n

class Test(tk.Tk):

     def __init__(self, *args, **kwargs):
         super().__init__(*args, **kwargs)

         # notebook
         n = ttk.Notebook(self)

         self.tab1 = ttk.Frame(n) # activities
         self.tab2 = ttk.Frame(n) # people
         self.tab3 = ttk.Frame(n) # locations
         self.tab4 = ttk.Frame(n) # organizations
         self.tab5 = ttk.Frame(n) # reports
         self.tab6 = ttk.Frame(n) # lookup tables

         self.add(tab1, text='Activities')
         self.add(tab2, text='People')
         self.add(tab3, text='Locations')
         self.add(tab4, text='Organizations')
         self.add(tab5, text='Reports')
         self.add(tab6, text='Lookups')

         n.pack(expand = 1, fill ="both")

if __name__ == "__main__":
     app = Test()
     app.mainloop()
---->8 -------------------------

python-3.9.1 returns:
$ ./nbtest.py 
Traceback (most recent call last):
   File "/home/rshepard/development/business_tracker/./nbtest.py", line 32, in <module>
     app = Test()
   File "/home/rshepard/development/business_tracker/./nbtest.py", line 22, in __init__
     self.add(tab1, text='Activities')
   File "/usr/lib64/python3.9/tkinter/__init__.py", line 2346, in __getattr__
     return getattr(self.tk, attr)
AttributeError: '_tkinter.tkapp' object has no attribute 'add'

Explanation of the error and suggestions for ttk.notebook references are
needed.

TIA,

Rich


More information about the Python-list mailing list