Writing a Python3 ttk.Notebook

MRAB python at mrabarnett.plus.com
Fri Jan 15 16:39:53 EST 2021


On 2021-01-15 20:51, Rich Shepard wrote:
> 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')
> 
You should be adding the frames to the notebook. Also, the tabs are 
'self.tab1', not 'tab1', etc:

          n.add(self.tab1, text='Activities')

Similarly for the others.

>           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.
> 


More information about the Python-list mailing list