Issue
I am working with Python's tkinter package. I've constructed a Notebook object, and assigned two frames that can be "tabbed" between...
notebook=ttk.Notebook(root)
frame1=ttk.Frame(notebook)
frame2=ttk.Frame(notebook)
notebook.add(frame1,text="Tab 1")
notebook.add(frame2,text="Tab 2")
However, I can't figure out how to do one thing in this configuration. I want to write a function that can tell which "tab" the user is looking at. Perhaps it is called every five seconds, and it prints a "1" if the user is on tab 1 and a "2" if the user is on tab "2"? I'm hoping there is a simple function like frame1.isvisible() or something like that, but I haven't been able to find it in the documentation.
Solution
You can actually use the method notebook.select to find the tab currently selected. But the problem is that it will not fetch you the direct result you want and rather will return you the tab_id
So you also have to use the notebook.index method to find the actual tab index and not the tab_id.
notebook.index(notebook.select())
This should answer you. But here too if you want the actual tab names you gave then filter it using the 'text' argument -:
notebook.tab(notebook.select(), 'text')
Another way to do it is using the current tag which saves the current tab value -:
notebook.index('current')
Both will give results as desired.
Answered By - Matrix Programmer
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.