Home > Back-end >  What is the purpose of Tkinter ttk notebook.pressed_index = index?
What is the purpose of Tkinter ttk notebook.pressed_index = index?

Time:09-28

What is the purpose of tkinter ttk.notebook.pressed_index = index??

I saw it on this snippet: (full code in the end of the question)

def btn_press(event):
    x, y, widget = event.x, event.y, event.widget
    elem = widget.identify(x, y)
    index = widget.index("@%d,%d" % (x, y))

    if "close" in elem:
        widget.state(['pressed'])
        widget.pressed_index = index

I got intrigued with the widget.pressed_index = index line.

Tried to get some intel from Interactive Mode:

help(ttk.Notebook)
dir(ttk.notebook)

But got nothing...

Tried:

print(dir(widget))

It prints this, but not so useful too :

[(lots of methods yada, yada, yada ) , 'option_add', 'option_clear', 'option_get', 'option_readfile', 'pack', 'pack_configure', 'pack_forget', 'pack_info', 'pack_propagate', 'pack_slaves', 'place', 'place_configure', 'place_forget', 'place_info', 'place_slaves', 'pressed_index', 'propagate', 'quit', 'register', 'rowconfigure', 'select', 'selection_clear', 'selection_get', 'selection_handle', 'selection_own', 'selection_own_get', 'send', 'setvar', 'size', 'slaves', 'state', 'tab', 'tabs', 'tk', 'tk_bisque', 'tk_focusFollowsMouse', 'tk_focusNext', 'tk_focusPrev', 'tk_setPalette', 'tk_strictMotif', 'tkraise', 'unbind', (more yada yada) ]

Also tried, but then again, no:

widget.pressed_index = index
print(widget.pressed_index)

Deep within I know this code is simply setting a tab index (int) to the variable.

But what is the purpose of that variable inside the notebook class? Why do I need to include it on the code to make it handle the user click? Why is it mandatory for the function to work?

Does it set the user click event to a particular tab added to the ttk.notebook?

In the example the index of the tab that received the click event is assigned to the variable. But if the widget is passed along with the event.widget, why do I need to set the pressed_index variable 'manually?'

In sum:

  1. What is ttk.notebook.pressed_index purpose?

  2. Is it documented anywhere? (If it is, I could not find it)

  3. Shouldn´t it be documented?

  4. How do you guys figure this kind of arcanery out?

I already searched here:

https://docs.python.org/3/library/tkinter.ttk.html#tab-identifiers

https://docs.python.org/3/library/tkinter.ttk.html#tkinter.ttk.Widget.identify

Googled it.

Stackoverflowed it.

Found uses of it in a lot of code around the web, but no explanation whatsoever.

Here is the full example code:

"""A Ttk Notebook with close buttons.

Based on an example by patthoyts, http://paste.tclers.tk/896
"""
import os
import Tkinter
import ttk

root = Tkinter.Tk()

imgdir = os.path.join(os.path.dirname(__file__), 'img')
i1 = Tkinter.PhotoImage("img_close", file=os.path.join(imgdir, 'close.gif'))
i2 = Tkinter.PhotoImage("img_closeactive",
    file=os.path.join(imgdir, 'close_active.gif'))
i3 = Tkinter.PhotoImage("img_closepressed",
    file=os.path.join(imgdir, 'close_pressed.gif'))

style = ttk.Style()

style.element_create("close", "image", "img_close",
    ("active", "pressed", "!disabled", "img_closepressed"),
    ("active", "!disabled", "img_closeactive"), border=8, sticky='')

style.layout("ButtonNotebook", [("ButtonNotebook.client", {"sticky": "nswe"})])
style.layout("ButtonNotebook.Tab", [
    ("ButtonNotebook.tab", {"sticky": "nswe", "children":
        [("ButtonNotebook.padding", {"side": "top", "sticky": "nswe",
                                     "children":
            [("ButtonNotebook.focus", {"side": "top", "sticky": "nswe",
                                       "children":
                [("ButtonNotebook.label", {"side": "left", "sticky": ''}),
                 ("ButtonNotebook.close", {"side": "left", "sticky": ''})]
            })]
        })]
    })]
)

def btn_press(event):
    x, y, widget = event.x, event.y, event.widget
    elem = widget.identify(x, y)
    index = widget.index("@%d,%d" % (x, y))

    if "close" in elem:
        widget.state(['pressed'])
        widget.pressed_index = index

def btn_release(event):
    x, y, widget = event.x, event.y, event.widget

    if not widget.instate(['pressed']):
        return

    elem =  widget.identify(x, y)
    index = widget.index("@%d,%d" % (x, y))

    if "close" in elem and widget.pressed_index == index:
        widget.forget(index)
        widget.event_generate("<<NotebookClosedTab>>")

    widget.state(["!pressed"])
    widget.pressed_index = None


root.bind_class("TNotebook", "<ButtonPress-1>", btn_press, True)
root.bind_class("TNotebook", "<ButtonRelease-1>", btn_release)

# create a ttk notebook with our custom style, and add some tabs to it
nb = ttk.Notebook(width=200, height=200, style="ButtonNotebook")
nb.pressed_index = None
f1 = Tkinter.Frame(nb, background="red")
f2 = Tkinter.Frame(nb, background="green")
f3 = Tkinter.Frame(nb, background="blue")
nb.add(f1, text='Red', padding=3)
nb.add(f2, text='Green', padding=3)
nb.add(f3, text='Blue', padding=3)
nb.pack(expand=1, fill='both')

root.mainloop()

CodePudding user response:

pressed_index is not part of the notebook widget. It is a custom attribute used by the code you copied in order to keep track of the index of the tab that was most recently clicked on. It looks like it's being used on a button release, to only trigger some code if you released the button over the same tab that you clicked the button.

  • Related