TTK Notebook 在导入的选项卡之间共享数据
我使用 ttk notebook 设计了一个应用程序,并创建了三个选项卡.它们每个人的布局都截然不同且复杂,它们之间的共同功能和方法很少.
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?
我遇到的问题是如何在 Frame 子类与主应用程序之间共享属性.解决方案只是向 Frame 子类添加第二个参数.更具体地说,我想在两个笔记本中使用 main.py 中的common_text"和font"属性.现在它可以工作了,即使我不知道这是否是正确的解决方案.
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
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
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)
将笔记本选项卡放在单独的文件中与将任何其他 Python 代码放在单独的文件中没有什么不同.
Putting notebook tabs in separate files is no different than putting any other python code in separate files.
例如创建一个名为page1.py"的文件,内容如下:
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)
创建具有几乎相同内容的第二个文件,将1"更改为2":
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 与任何其他 python 代码没有什么不同.如果两个对象——不管是什么类——需要访问相同的信息,则必须为它们提供信息或提供访问信息的方法.
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.