程序启动后,根据后台加载的数据,UI加载tab页签。
继承Frame创建控件
这里创建plugin模块,定义PluginBase.py继承Frame
import tkinter as tk
class PluginBase(tk.Frame):
friendly_name = ""
def __init__(self, root=None):
super().__init__(root)
self.friendly_name = "plugin name"
self.master = root
self.pack()
self.create_widgets()
def create_widgets(self):
self.hello_label = tk.Label(self, text="This is plugin base")
self.hello_label.pack()
实现一个具体的页签控件example.py
class example(PluginBase):
def __init__(self, root=None):
super().__init__(root)
def create_widgets(self):
self.hello_label = tk.Label(self, text="Hello, example")
self.hello_label.pack()
动态加载模块
#调用importlib模块实现动态加载
module = importlib.import_module(f'{PLUGIN_DIR}.example.example')
plugin = getattr(module, "example")
#生成Frame对象实例
plugin_tab = plugin(root)
#添加tab页签
tab_container.add(plugin_tab, text=plugin_tab.friendly_name)
添加多个定制tab页签: