'Getting the current selected tab name of ttk notebook in tkinter? - python

I'm creating a GUI that shows different plot types in different tabs, so I created this helping function and I'm trying to figure out how to get the current tab info.

I tried testing the code with print(tabControl.tab(tabControl.select(), "text")), but it is always returning "Intro" which is my first tab in the GUI.

Also I would like to know if there's a way to get the name of the variable of the selected tab so I could use it i.e instead of tab_bar in FigureCanvasTkAgg(fig, tab_bar).

 def show(x, y):
        fig = Figure(figsize=(5, 4), dpi=100)
        a = fig.add_subplot(111)
    
        print(tabControl.tab(tabControl.select(), "text"))
    
        if (tabControl.tab(tabControl.select(), "text") == "Line"):
            a.plot(x, y)
        if (tabControl.tab(tabControl.select(), "text")== "Bar"):
            a.bar(x, y)
        if (tabControl.tab(tabControl.select(), "text")== "Scatter"):
            a.scatter(x, y)
    
        a.set_title(gettitle())
        a.set_xlabel(getx())
        a.set_ylabel(gety())

        canvas = FigureCanvasTkAgg(fig, tab_bar)
        canvas.draw()
        canvas.get_tk_widget().pack(side=tkinter.BOTTOM, fill=tkinter.BOTH, expand=1)
        toolbar = NavigationToolbar2Tk(canvas, tab_bar)
        toolbar.update()
        canvas.get_tk_widget().pack(side=tkinter.BOTTOM, fill=tkinter.BOTH, expand=1)


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source