| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899 | import tkinter as tkfrom tkinter import Menu, Text, Label, Entry, Buttonfrom PIL import Image, ImageTk  # 需要安装 Pillow 库class Application(tk.Tk):    def __init__(self):        super().__init__()        self.content_frame = None        self.navigation_buttons = None        self.navigation_frame = None        self.title("Tkinter 复杂 UI 示例")        self.geometry("800x600")        self.create_menu()        self.create_navigation()        self.create_content_area()        self.content_cache = {}  # 缓存内容    def create_menu(self):        menubar = Menu(self)        self.config(menu=menubar)        menu1 = Menu(menubar, tearoff=0)        menu1.add_command(label="选项 1")        menu1.add_command(label="选项 2")        menubar.add_cascade(label="菜单 1", menu=menu1)        menu1 = Menu(menubar, tearoff=0)        menu1.add_command(label="选项 1")        menu1.add_command(label="选项 2")        menubar.add_cascade(label="菜单 2", menu=menu1)        # 添加其他菜单...    def create_navigation(self):        self.navigation_frame = tk.Frame(self, width=150, bg="#f0f0f0")        self.navigation_frame.pack(side=tk.LEFT, fill=tk.Y)        navigation_items = ["导航 1", "导航 2", "导航 3", "导航 4", "导航 5", "导航 6", "导航 7", "导航 8", "导航 9", "导航 10"]        # self.navigation_buttons = []        for item in navigation_items:            button = tk.Button(self.navigation_frame, text=item, command=lambda i=item: self.show_content(i))            button.pack(fill=tk.X, pady=5)            # self.navigation_buttons.append(button)    def create_content_area(self):        self.content_frame = tk.Frame(self)        self.content_frame.pack(side=tk.RIGHT, fill=tk.BOTH, expand=True)    def show_content(self, item):        print(f"---> {item},{self.content_cache}")        for key, value in self.content_cache.items():            if key != item:                value.pack_forget()        if item in self.content_cache:            content = self.content_cache[item]            content.pack(fill=tk.BOTH, expand=True)        else:            content = tk.Frame(self.content_frame)            content.pack(fill=tk.BOTH, expand=True)            # 表单区域            form_frame = tk.Frame(content)            form_frame.pack(side=tk.TOP, fill=tk.X)            tk.Label(form_frame, text="姓名:").grid(row=0, column=0, sticky="w")            name_entry = Entry(form_frame)            name_entry.grid(row=0, column=1)            tk.Label(form_frame, text="年龄:").grid(row=1, column=0, sticky="w")            age_entry = Entry(form_frame)            age_entry.grid(row=1, column=1)            Button(form_frame, text="保存").grid(row=2, column=1, sticky="e")            # 日志区域            log_text = Text(content, height=10)            log_text.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)            log_text.insert(tk.END, f"导航 {item} 被点击\n")            # 图片预览区域            image_label = Label(content)            image_label.pack(side=tk.RIGHT, fill=tk.BOTH, expand=True)            try:                image = Image.open("../../assets/img.png")  # 替换为您的图片路径                photo = ImageTk.PhotoImage(image)                image_label.config(image=photo)                image_label.image = photo  # 保持对图片的引用            except FileNotFoundError:                log_text.insert(tk.END, "图片未找到\n")            self.content_cache[item] = content  # 缓存内容if __name__ == "__main__":    app = Application()    app.mainloop()
 |