简体   繁体   中英

How to get widget object from ttk.Notebook tab?

I have notebook widget of class ttk.Notebook. In this notebook I'm adding many tabs:

notebook.add(child=my_object, text='my tab')

How can I get tab object and not tab name?

UPDATE 1

So, there is children dictionary. OK that brings next problem.

What I really want to do is to take my hands on object of active tab in Notebook. So I have this:

notebook.children[notebook.select().split('.')[2]])

but it looks ugly. Unfortunately widget name returned by select() has not exactly same format as widget names in children[]. Is there any method of reliable translation of one format to another or split() will never fail in this situation?

This is demo of problem:

#!/usr/bin/env python3

from tkinter import *
from tkinter.ttk import *


class App(Tk):
    def __init__(self):
        super().__init__()
        notebook = Notebook(self)
        notebook.add(child=Frame(notebook), text='tab1')
        notebook.add(child=Frame(notebook), text='tab2')
        notebook.add(child=Frame(notebook), text='tab3')
        notebook.pack()

        print('Tabs are: ', notebook.children.keys())
        print('Active tab: ', notebook.select())
        print(type(notebook.children[notebook.select().split('.')[2]]))

App().mainloop()

The notebook.tabs() call returns a list of "tab names" which correspond to the children widget objects. A call to someWidget.nametowidget(tab_name) resolves to the child widget associated with that tab.

In other words, the Notebook .tabs() method returns a list of the names of the Notebook's children.

For your UPDATE:

The same as above applies, only now it's:

active_object = notebook.nametowidget(notebook.select())

Different versions of Python use different naming conventions, so using nametowidget() is the better way to go rather than manually processing the name to get the desired key to use for a reference into the children dict.

ttk.Notebook stores Tab-Objects within it's children attribute which is a python dictionary like:

{my_tab._name: tab_object} e.g. {'4465111168': <Viewer.Tab object .4435079856.4465020320.4465111168>}

You can access a Tab-object via:

my_notebook.children[my_tab._name]

Where my_notebook is an instance of an ttk.Notebook object and my_tab is your Tab object. The tab attribute _name is the value wich get's stored as the key in my_notebook.children .

So the easiest way of accessing the tab objects would be by separately storing your my_tab._name values in a list or variable. With all the names stored you can access the children dict like above.

See https://docs.python.org/3.1/library/tkinter.ttk.html for more reference.

Deleted my former post, because it was basically bs.

To get the actual widget-object use the following code:

name =  mywidget.winfo_name() # this returns this ominous string
                              # of numbers and dots

object_from_name = mywidget._nametowidget(name) # this returns your
                                                # actual widget

If you print object_from_name you'll likely get the name. But I think, this is how tkinter widget objects are represented. You should be able to do

object_from_name.config(text="stuff", do_further_stuff_here="")

That's what did the trick for me:

for name in notebook.tabs():
    tab_widget = notebook.nametowidget(name)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM