Question

I have a Ttk Notebook widget containing 8 Frames - so, 8 tabs. Each frame contains a Text widget. I have a button outside the Notebook widget, and I want to insert text into the current tabs Text widget when this button is pressed.

This would seem to require working out which widget in the Notebook is currently selected, but I can't seem to find how to do this. How would I find the currently selected tab?

Alternatively, how can I implement what I want to?

If it helps, here's the code for my notebook:

self.nb = Notebook(master)
self.nb.pack(fill='both', expand='yes', padx=10, pady=10)
self.frames = []
self.texts = []
for i in xrange(8):
  self.frames.append(Frame())
  self.nb.add(self.frames[i])
  self.texts.append(Text(self.frames[i]))
  self.texts[i].pack(fill='both')
Was it helpful?

Solution

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

OTHER TIPS

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

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.

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 :-)

Getting the tageted tab in tk.Notebook it's easy all you have to do is to use the notebook object and target the index of the current tab. This can be done as follows

 # creating a notebook object
 notebook = ttk.Notebook(root, height=height, width=width, padding=20)

 # Adding tabs
 notebook.add(bin_tab, text="Binary Conversion")
 notebook.add(oct_tab, text="Octal Conversion")
 notebook.add(hex_tab, text="Hexadecimal Conversion")

 print(notebook.index("current")) # returns 0, 1, 2depending on how many tabs you have in my case i have 3 which means index from 0 to 2
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top