TTK Notebook Share data between imported tabs

*爱你&永不变心* 提交于 2019-12-25 06:26:57

问题


I designed an application using the ttk notebook and I created three tab. Everyone of them has a starkly distinct and complex layout and very few functions and methods in common between them.

As a result, the code file is getting quite hard to understand with so many line of code dedicated to the layout.

There is a way to create every tab as a separate module/file/class and "import" them, so that they can be easier to maintain?

EDIT:

The problem I had was how to share attributes between the Frame subclass to the main application. The solution was simply add a second argument to the Frame subclass. To be more specific, I wanted to use the "common_text" and "font" attributes from the main.py in the two notebooks. It now works, even thou I don't know if this is the correct solution.

main.py

from Tkinter import *
import ttk
import first_tab
import second_tab

class Application(object):
    def __init__(self, root):
        super(Application, self).__init__()
        self.common_text = "This is a test"
        self.font = ('courier', 10, 'bold')

        self.root = root
        self.notebook = ttk.Notebook(root)
        self.notebook.pack(fill='both', expand = 'yes')
        self.tab_1 = first_tab.tab_frame(self)
        self.tab_2 = second_tab.tab_frame(self)
        self.notebook.add(self.tab_1, text = "First Tab")
        self.notebook.add(self.tab_2, text = "Second Tab")

root = Tk()
app = Application(root)
root.title("Utility")
root.mainloop()

first_tab.py

from Tkinter import *
import ttk

class tab_frame(Frame):
    def __init__(self, relative):
        Frame.__init__(self)

        self.F_1_00 = Frame(self)
        self.F_1_00.grid(column=0, row=0)
        self.F_1_10 = Frame(self)
        self.F_1_10.grid(column=0, row=1)

        self.sign = Label(self.F_1_00, text = relative.common_text, pady=10)
        self.sign.configure(font = relative.font)
        self.sign.grid(column=0, row=0)
        self.reset = Button(self.F_1_10, text = "First", width = 10)
        self.reset.grid(column=2, row=3, padx = 10)

second_tab.py

from Tkinter import *
import ttk

class tab_frame(Frame):
    def __init__(self, relative):
        Frame.__init__(self)

        self.F_2_00 = Frame(self)
        self.F_2_00.grid(column=0, row=0)
        self.F_2_10 = Frame(self)
        self.F_2_10.grid(column=0, row=1)

        self.sign = Label(self.F_2_00, text = relative.common_text, pady=10)
        self.sign.configure(font = relative.font)
        self.sign.grid(column=0, row=0)

        self.reset = Button(self.F_2_10, text = "Second", width = 10)
        self.reset.grid(column=2, row=3, padx = 10)

回答1:


Putting notebook tabs in separate files is no different than putting any other python code in separate files.

For example, create a file named "page1.py" with the following contents:

import Tkinter as tk

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

        label = tk.Label(self, text="This is page 1")
        label.pack(fill ="both", expand=True, padx=20, pady=10)

Create a second file with nearly identical contents, changing "1" to "2":

import Tkinter as tk

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

        label = tk.Label(self, text="This is page 2")
        label.pack(fill ="both", expand=True, padx=20, pady=10)

Now, create a main application that uses these two files:

import Tkinter as tk
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="Page 1")
        self.notebook.add(page2, text="Page 2")

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

If you need to share data between these two classes, they must share something. For example, you could have a common dictionary that gets passed in to each frame. For example:

self.app_data = {...}
page1 = Page1(self.notebook, self.app_data)
page2 = Page2(self.notebook, self.app_data)

Another solution is to adopt a bit of the model-view-controller pattern, where the app is the controller.

page1 = Page1(self.notebook. self)
...
class Page1(tk.Frame):
    def __init__(self, parent, controller)
        self.controller = controller
    ...
    def some_function(self):
        # get data from page 2
        page = self.controller.get_page("Page2")
        data = page.data

Tkinter is no different than any other python code in this regard. If two objects -- no matter what the class -- need access to the same information, they must be given the information or be given a way to access the information.



来源:https://stackoverflow.com/questions/36032712/ttk-notebook-share-data-between-imported-tabs

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!