0

嗨,任何人都可以帮助我吗?我是编程新手,我正在尝试使用 tkinter 做一个小项目。基本上我的问题是我怎样才能做到这一点,所以当我从组合框中选择一个选项时,也会选择 ttk 笔记本上的选项卡,反之亦然...提前致谢

 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()
4

1 回答 1

0

试试下面的完整代码,

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

做了一些修改,

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")])

以上两个函数用于处理这两个事件

组合框值放在变量列表“选项”中

于 2020-09-16T13:03:59.983 回答