如何使用 类 在不同的选项卡中正确使用我们的代码?

How properly use our code in different tabs using the classes?

我想在我的 GUI 中创建不同的选项卡,并且能够在每个选项卡中编写不同的代码,并且能够在选项卡之间传递变量。所以我试着用不同的 classes 来写。但是我的代码无法在正确的选项卡中显示定义的标签。我想在 class tab1 中显示 label1,在 class tab2 中显示 label2,等等。 这是我的代码:

import tkinter as tk
from tkinter import ttk

class Data:
    def __init__(self):
        self.n = tk.IntVar()

class SampleApp(tk.Tk):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.minsize(400, 400)
        self.maxsize(400, 400)

        my_notebook = ttk.Notebook(self)
        my_notebook.pack()

        tab_frame1 = tk.Frame(my_notebook, width=400, height=400)
        tab_frame1.pack()

        tab_frame2 = tk.Frame(my_notebook, width=400, height=400)
        tab_frame2.pack()

        tab_frame3 = tk.Frame(my_notebook, width=400, height=400)
        tab_frame3.pack()

        my_notebook.add(tab_frame1, text="Tab1")
        my_notebook.add(tab_frame2, text="Tab2")
        my_notebook.add(tab_frame3, text="Tab3")
        self.data = Data()

        self.frames = {}
        for F in (tab1, tab2, tab3):
            frame1 = F(tab_frame1, self.data)
            self.frames[F] = frame1
            frame1.pack()


class tab1(tk.Frame):
    def __init__(self, parent, data):
        super().__init__(parent)
        self.data = data

        label1 = tk.Label(self, text="Tab1")
        label1.pack()


class tab2(tk.Frame):
    def __init__(self, parent, data):
        super().__init__(parent)
        self.data = data

        label2 = tk.Label(self, text="Tab2")
        label2.pack()


class tab3(tk.Frame):
    def __init__(self, parent, data):
        super().__init__(parent)
        self.data = data

        label3 = tk.Label(self, text="Tab3")
        label3.pack()

app = SampleApp()
app.mainloop()

你可以这样做:

import tkinter as tk
from tkinter import ttk

class Data:
    def __init__(self):
        self.n = tk.IntVar()

class SampleApp(tk.Tk):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.minsize(400, 400)
        self.maxsize(400, 400)

        self.my_notebook = ttk.Notebook(self)
        self.my_notebook.pack(fill="both", expand=True)

        self.app_data = {}

        lst = [tab1, tab2, tab3]
        for N, F in enumerate(lst):
            tab = F(self.my_notebook, self.app_data)
            self.my_notebook.add(tab, text="Tab"+str(N+1))

        self.data = Data()


class tab1(tk.Frame):
    def __init__(self, parent, data):
        super().__init__(parent)
        self.data = data

        label1 = tk.Label(self, text="Tab1")
        label1.pack()


class tab2(tk.Frame):
    def __init__(self, parent, data):
        super().__init__(parent)
        self.data = data

        label2 = tk.Label(self, text="Tab2")
        label2.pack()


class tab3(tk.Frame):
    def __init__(self, parent, data):
        super().__init__(parent)
        self.data = data

        label3 = tk.Label(self, text="Tab3")
        label3.pack()

app = SampleApp()
app.mainloop()