Writing a Python3 ttk.Notebook

Rich Shepard rshepard at appl-ecosys.com
Fri Jan 15 17:32:11 EST 2021


On Fri, 15 Jan 2021, Dennis Lee Bieber wrote:

> 	Off-hand, I'd suspect you should be adding these to the NOTEBOOK
> object "n".

Dennis,

You're correct. The MWE didn't have the proper syntax.

Now, the problem is the notebook doesn't display its tabs on the main
window, while the proper syntax on a separate file opens a window with the
tabs displayed.

The file 'application.py' is attached. If I had better docs here I could
probably work a lot of this out by myself.

Regards,

Rich
-------------- next part --------------
#!/usr/bin/env python3

# main file to start application.

from os import environ
import sys
from datetime import datetime
import tkinter as tk
from tkinter import ttk
from tkinter.ttk import Notebook as nb
from tkinter import filedialog
from tkinter import messagebox
from tkinter.font import nametofont
from functools import partial
import model as m
import views as v
import controller as c


class Application(tk.Tk):
    """ Application root window """

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

        # the top level frame holding notebook, status bar, etc.
        self.columnconfigure(0, weight=1)   # helps with frame/app resizing on the fly
        self.rowconfigure(0, weight=1)      # helps with frame/app resizing on the fly
        # open application centered on screen; set window width and height
        self.appwidth = 1000
        self.appheight = 800
        # get screen width and height
        self.scrwidth = self.winfo_screenwidth()
        self.scrheight = self.winfo_screenheight()
        # formula to find screen center
        self.xLeft = (self.scrwidth/2) - (self.appwidth/2)
        self.yTop = (self.scrheight/2) - (self.appheight/2)
        # set geometry
        self.geometry(str(self.appwidth) + "x" + str(self.appheight) +
                      "+" + str(int(self.xLeft)) + "+" + str(int(self.yTop)))
        
        self.title("Main Window Title Goes Here")
        self.resizable(width=True, height=True)

        datestring = datetime.today().strftime("%Y-%m-%d")

        # variables for adjusting font size and style
        self.font_bold = tk.BooleanVar()
        self.font_size = tk.IntVar()

        def set_font(*args):
            self.font_spec = 'TkDefaultFont {size} {bold}'.format(
                size=font_size.get(),
                bold='bold' if font_bold.get() else '')
            self.label.config(font=font_spec)
            self.font_bold.trace('w', set_font)
            self.font_size.trace('w', set_font)
        
        # status bar
        self.status = tk.StringVar()
        self.statusbar = ttk.Label(self, textvariable=self.status)
        self.statusbar.grid(sticky="we", row=3, padx=10)

        # notebook
        nb = ttk.Notebook(self)

        self.tab1 = ttk.Frame(nb) # activities
        self.tab2 = ttk.Frame(nb) # people
        self.tab3 = ttk.Frame(nb) # locations
        self.tab4 = ttk.Frame(nb) # organizations
        self.tab5 = ttk.Frame(nb) # reports
        self.tab6 = ttk.Frame(nb) # lookup tables
        
        nb.add(self.tab1, text='Activities')
        nb.add(self.tab2, text='People')
        nb.add(self.tab3, text='Locations')
        nb.add(self.tab4, text='Organizationbs')
        nb.add(self.tab5, text='Reports')
        nb.add(self.tab6, text='Lookups')
        
                
if __name__ == "__main__":
    app = Application()
    app.mainloop()


More information about the Python-list mailing list