test04.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import tkinter as tk
  2. from tkinter import Menu, Text, Label, Entry, Button
  3. from PIL import Image, ImageTk # 需要安装 Pillow 库
  4. class Application(tk.Tk):
  5. def __init__(self):
  6. super().__init__()
  7. self.content_frame = None
  8. self.navigation_buttons = None
  9. self.navigation_frame = None
  10. self.title("Tkinter 复杂 UI 示例")
  11. self.geometry("800x600")
  12. self.create_menu()
  13. self.create_navigation()
  14. self.create_content_area()
  15. self.content_cache = {} # 缓存内容
  16. def create_menu(self):
  17. menubar = Menu(self)
  18. self.config(menu=menubar)
  19. menu1 = Menu(menubar, tearoff=0)
  20. menu1.add_command(label="选项 1")
  21. menu1.add_command(label="选项 2")
  22. menubar.add_cascade(label="菜单 1", menu=menu1)
  23. menu1 = Menu(menubar, tearoff=0)
  24. menu1.add_command(label="选项 1")
  25. menu1.add_command(label="选项 2")
  26. menubar.add_cascade(label="菜单 2", menu=menu1)
  27. # 添加其他菜单...
  28. def create_navigation(self):
  29. self.navigation_frame = tk.Frame(self, width=150, bg="#f0f0f0")
  30. self.navigation_frame.pack(side=tk.LEFT, fill=tk.Y)
  31. navigation_items = ["导航 1", "导航 2", "导航 3", "导航 4", "导航 5", "导航 6", "导航 7", "导航 8", "导航 9", "导航 10"]
  32. # self.navigation_buttons = []
  33. for item in navigation_items:
  34. button = tk.Button(self.navigation_frame, text=item, command=lambda i=item: self.show_content(i))
  35. button.pack(fill=tk.X, pady=5)
  36. # self.navigation_buttons.append(button)
  37. def create_content_area(self):
  38. self.content_frame = tk.Frame(self)
  39. self.content_frame.pack(side=tk.RIGHT, fill=tk.BOTH, expand=True)
  40. def show_content(self, item):
  41. print(f"---> {item},{self.content_cache}")
  42. for key, value in self.content_cache.items():
  43. if key != item:
  44. value.pack_forget()
  45. if item in self.content_cache:
  46. content = self.content_cache[item]
  47. content.pack(fill=tk.BOTH, expand=True)
  48. else:
  49. content = tk.Frame(self.content_frame)
  50. content.pack(fill=tk.BOTH, expand=True)
  51. # 表单区域
  52. form_frame = tk.Frame(content)
  53. form_frame.pack(side=tk.TOP, fill=tk.X)
  54. tk.Label(form_frame, text="姓名:").grid(row=0, column=0, sticky="w")
  55. name_entry = Entry(form_frame)
  56. name_entry.grid(row=0, column=1)
  57. tk.Label(form_frame, text="年龄:").grid(row=1, column=0, sticky="w")
  58. age_entry = Entry(form_frame)
  59. age_entry.grid(row=1, column=1)
  60. Button(form_frame, text="保存").grid(row=2, column=1, sticky="e")
  61. # 日志区域
  62. log_text = Text(content, height=10)
  63. log_text.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
  64. log_text.insert(tk.END, f"导航 {item} 被点击\n")
  65. # 图片预览区域
  66. image_label = Label(content)
  67. image_label.pack(side=tk.RIGHT, fill=tk.BOTH, expand=True)
  68. try:
  69. image = Image.open("../../assets/img.png") # 替换为您的图片路径
  70. photo = ImageTk.PhotoImage(image)
  71. image_label.config(image=photo)
  72. image_label.image = photo # 保持对图片的引用
  73. except FileNotFoundError:
  74. log_text.insert(tk.END, "图片未找到\n")
  75. self.content_cache[item] = content # 缓存内容
  76. if __name__ == "__main__":
  77. app = Application()
  78. app.mainloop()