简体   繁体   English

Tkinter 在(笔记本)选项卡之间共享数据

[英]Tkinter Share data between (Notebook) Tabs

I have found a very interesting post under the title "TTK Notebook Share Data Between Imported Tabs".我在标题“TTK Notebook Share Data Between Imported Tabs”下发现了一篇非常有趣的帖子。

Here is the link: [https://stackoverflow.com/questions/36032712/ttk-notebook-share-data-between-imported-tabs][1]这是链接:[https://stackoverflow.com/questions/36032712/ttk-notebook-share-data-between-imported-tabs][1]

This is the main application:这是主要应用程序:

I've created as described there and it works.我已经按照那里的描述进行了创建,并且可以正常工作。 But how can I share data between these two classes?但是我怎样才能在这两个类之间共享数据呢?

For example, if I write the following code to the Main Application:例如,如果我将以下代码写入主应用程序:

self.hello = "Hello World"
page1 = Page1(self.notebook, self.hello)
page2 = Page2(self.notebook, self.hello)

I get the error message:我收到错误消息:

page1 = Page1(self.notebook, self.app_data) page1 = Page1(self.notebook, self.app_data)

TypeError: init () takes 2 positional arguments but 3 were given TypeError: init () 需要 2 个位置 arguments 但给出了 3 个

I have been working on it for days, but unfortunately I do not understand where the mistake is.我已经为此工作了好几天,但不幸的是我不明白错误在哪里。 I would be very happy if someone would help me here.如果有人在这里帮助我,我会很高兴。

This is the main application (main.py):这是主应用程序(main.py):

import tkinter as tk
from tkinter import ttk
from page1 import Page1
from page2 import Page2

class Example(tk.Frame):
    def __init__(self, parent):
        tk.Frame.__init__(self, parent)

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

        page1 = Page1(self.notebook)
        page2 = Page2(self.notebook)
        self.notebook.add(page1, text="Tab 1")
        self.notebook.add(page2, text="Tab 2")

        self.hello = "Hello World"
        page1 = Page1(self.notebook, self.hello)
        page2 = Page2(self.notebook, self.hello)



if __name__ == "__main__":
    root = tk.Tk()
    Example(root).pack(fill="both", expand=True)
    root.mainloop()

This is the page1 "page1.py":这是第 1 页“page1.py”:

import tkinter as tk

class Page1(tk.Frame):
    def __init__(self, parent):
        tk.Frame.__init__(self, parent)

        label = tk.Label(self, text="Das ist Tab 1"+ self.hello)
        label.pack(fill ="both", expand=True, padx=20, pady=10)

And thi is page2 "page2.py":这是page2“page2.py”:

import tkinter as tk

class Page2(tk.Frame):
    def __init__(self, parent):
        tk.Frame.__init__(self, parent)

        label = tk.Label(self, text="Das ist Tab 2"+ self.hello)
        label.pack(fill ="both", expand=True, padx=20, pady=10)

The error is telling you exactly what is wrong:该错误准确地告诉您出了什么问题:

page1 = Page1(self.notebook, self.app_data)

TypeError: __init()__ takes 2 positional arguments but 3 were given

As we can see, your Page2.__init__ takes two positional arguments: self and parent .正如我们所见,您的Page2.__init__需要两个位置 arguments: selfparent When you do Page1(self.notebook, self.app_data) , self is automatically passed to __init__ as the first argument.当您执行Page1(self.notebook, self.app_data)时, self会自动作为第一个参数传递给__init__ self.notebook is passed as parent , and there's no third parameter to accept self.app_data . self.notebook作为parent传递,并且没有接受self.app_data的第三个参数。

You need to modify __init__ to accept the data parameter:您需要修改__init__以接受 data 参数:

class Page2(tk.Frame):
    def __init__(self, parent, app_data):
        tk.Frame.__init__(self, parent)
        self.app_data = app_data

Be aware that will cause a different line to fail, because you also call Page2 without that extra argument here:请注意,这将导致不同的行失败,因为您在这里也调用Page2没有那个额外的参数:

page1 = Page1(self.notebook)

It's unclear why you're creating page1 twice, but you need to be consistent in how you're creating it.目前尚不清楚为什么要创建page1两次,但您需要在创建它的方式上保持一致。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM