pythonpython-3.xtkintertkinter-menu

changing tab through combobox and vice versa


hi guys anyone can help me? Im new to programming and Im trying to do a small project using tkinter.. basically my problem is how can I make it so when I choose an option from a combobox the tab on the ttk notebook will also be selected and vice versa... thanks in advance

 import tkinter as tk
 from tkinter import ttk

 class Main(tk.Tk):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

        container = ttk.Frame(self)
        container.grid()

        frame = Frame(container, self)
        frame.grid(column=0, row=0)

        notebook = Notebook(container, self)
        notebook.grid(column=1, row=0)

 class Frame(ttk.Frame):
    def __init__(self, parent, controller, *args, **kwargs):
        super().__init__(parent, *args, **kwargs)

        combobox = ttk.Combobox(self)
        combobox["values"] = ["a", "b", "c"]
        combobox.grid()


 class Notebook(ttk.Notebook):
    def __init__(self, parent, controller, *args, **kwargs):
        super().__init__(parent, *args, **kwargs)

        first = ttk.Frame(self)
        second = ttk.Frame(self)
        third = ttk.Frame(self)


        self.add(first, text="a")
        self.add(second, text="b")
        self.add(third, text="c")

 root = Main()
 root.mainloop()

Solution

  • Try below full code,

    import tkinter as tk
    from tkinter import ttk
    
    class Main(tk.Tk):
        def __init__(self, *args, **kwargs):
            super().__init__(*args, **kwargs)
    
            container = ttk.Frame(self)
            container.grid()
    
            frame = Frame(container, self)
            frame.grid(column=0, row=0)
    
            notebook = Notebook(container, self)
            notebook.grid(column=1, row=0)
    
    class Frame(ttk.Frame):
        options=["a", "b", "c"]
        def __init__(self, parent, controller, *args, **kwargs):
            super().__init__(parent, *args, **kwargs)
    
            Frame.combobox = ttk.Combobox(self)
            self.combobox["values"] = self.options
            self.combobox.grid()
    
    class Notebook(ttk.Notebook):
        def __init__(self, parent, controller, *args, **kwargs):
            super().__init__(parent, *args, **kwargs)
    
            first = ttk.Frame(self)
            second = ttk.Frame(self)
            third = ttk.Frame(self)
    
            self.add(first, text="a")
            self.add(second, text="b")
            self.add(third, text="c")
            Frame.combobox.bind("<<ComboboxSelected>>", self.on_selection)
            self.bind("<<NotebookTabChanged>>",self.on_tab_selection)
        def on_selection(self,a):
            selected=Frame.combobox.get()
            Frame.options.index(selected)
            self.select(Frame.options.index(selected))
        def on_tab_selection(self,a):
            Frame.combobox.set(Frame.options[self.index("current")])
    
    root = Main()
    root.mainloop()
    

    Some modifications are made,

    Frame.combobox.bind("<<ComboboxSelected>>", self.on_selection)
    self.bind("<<NotebookTabChanged>>",self.on_tab_selection)
    

    above code is used to take the selection events of notebook and combobox

    def on_selection(self,a):
            selected=Frame.combobox.get()
            Frame.options.index(selected)
            self.select(Frame.options.index(selected))
    def on_tab_selection(self,a):
            Frame.combobox.set(Frame.options[self.index("current")])
    

    Above two functions are made to handle both events

    combobox values are put in a variable list "Options"