Finding the currently selected tab of Ttk Notebook

前端 未结 5 1423
南旧
南旧 2021-02-07 19:58

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

相关标签:
5条回答
  • 2021-02-07 20:08

    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.

    0 讨论(0)
  • 2021-02-07 20:11

    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

    0 讨论(0)
  • 2021-02-07 20:11

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

    0 讨论(0)
  • 2021-02-07 20:26

    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
    
    0 讨论(0)
  • 2021-02-07 20:30

    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

    0 讨论(0)
提交回复
热议问题