Finding the currently selected tab of Ttk Notebook

∥☆過路亽.° 提交于 2019-12-03 06:25:53

You can retrieve the selected tab through select method. However, this method returns a tab_id which is not much useful as is. index convert it to the number of the selected tab.

>>> nb.select()
'.4299842480.4300630784'
>>> nb.index(nb.select())
2

Note that you coud also get more information about the selected tab using tab

>>> nb.tab(nb.select(), "text")
'mytab2'

You might look at Notebook reference documentation : http://docs.python.org/3/library/tkinter.ttk.html#notebook

bcatets

You can get currently selected tab using the "current" keyword:

noteBook.index("current")

Check this website: https://docs.python.org/2/library/ttk.html#tab-identifiers 24.2.5.3. Tab Identifiers

I am not a expert at all but hope i can help with some "fresh eyes". I imagine it could be something involving

def buttonclick():
      somevariablename = focus_get()
      #Print your text into the somevariable notebook could be
      #something like(not sure about the syntax):
      focusednotebook = somevariablename
      focusednotebook.insert('1.0', 'your text here')

yourbutton = Button(parent, text = "button name", command = buttonclick)
yourbutton.pack()

Hope it works or get you in the right direction.

Please feel free to edit as I am fairly new here amd with python :-)

There are two simple ways to see which tab is selected:

nb.select()  # returns the Tab NAME (string) of the current selection

and

nb.index('current') # returns the Tab INDEX (number) of the current selection

The .select() method can also be used to select which tab is currently active, via nb.select(tabId). Without the arg, it returns the tabId (in "name" form) of the current selection.

The .index(tabId) converts a tabId into a numerical index. It also can take the string "end" which will return the number of tabs. So, nb.index(tkinter.END) is like a len() method for a notebook widget.

When there are no tabs, .select() returns an empty string, but .index('current') throws an exception. So, if you want the index, I would say

if nb.select():
    idx = nb.index('current')

is the best way to go.

In your particular case, you would probably want to grab the current notebook tab name and then convert that name into the actual child text widget, via the nametowidget() method, for manipulation. So...

tabName = notebook.select()
if tabName:
    textWidget = notebook.nametowidget(tabName) # here, 'notebook' could be any widget
    textWidget.insert(pos, text, tags)

The nametowidget(name) method maps a Tkinter name to the actual widget. It is a method callable by any actual widget.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!