testUI.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. import ttkbootstrap as ttk
  2. import os
  3. import tkinter as tk
  4. from tkhtmlview import HTMLLabel
  5. # from tkinterweb import HtmlFrame
  6. def on_menu_item_click(label):
  7. if label == "Item 1":
  8. display_html_file("data/1.html")
  9. elif label == "Item 2":
  10. display_html_file("data/2.html")
  11. elif label == "Sub Item 1":
  12. display_text_and_entries()
  13. elif label == "about":
  14. display_html_file("data/about.html")
  15. else:
  16. print(f"Clicked on {label}")
  17. def display_html_file(filepath):
  18. # 清空当前窗口内容,但保留菜单栏
  19. for widget in window.winfo_children():
  20. if not isinstance(widget, ttk.Menu):
  21. widget.destroy()
  22. if os.path.exists(filepath):
  23. with open(filepath, 'r', encoding='utf-8') as file:
  24. html_content = file.read()
  25. # 创建 HTMLLabel 来显示 HTML 内容
  26. global html_label
  27. html_label = HTMLLabel(window, html=html_content,wrap=ttk.CHAR)
  28. # tkinterweb
  29. # html_label = HtmlFrame(window)
  30. #
  31. # html_label.load_file("C:\\Users\\wang\\PycharmProjects\\PythonProject\\data\\1.html")
  32. # html_label.add_html(html_content)
  33. #html_label.load_html(html_content,base_url="C:\\Users\\wang\\PycharmProjects\\PythonProject\\data")
  34. html_label.pack(side=ttk.TOP, fill=ttk.BOTH, expand=True)
  35. html_label.fit_height()
  36. else:
  37. # 创建文本区域用于显示错误信息
  38. global text_area
  39. text_area = ttk.Text(window, wrap=ttk.WORD, font=('Arial', 12))
  40. text_area.pack(side=ttk.TOP, fill=ttk.BOTH, expand=True)
  41. text_area.insert(ttk.END, f"File {filepath} not found.")
  42. def display_text_and_entries():
  43. # 清空当前窗口内容,但保留菜单栏
  44. for widget in window.winfo_children():
  45. if not isinstance(widget, ttk.Menu):
  46. widget.destroy()
  47. global entry_list
  48. entry_list = []
  49. global label_list
  50. label_list = []
  51. # 创建10行界面
  52. for i in range(1, 11):
  53. frame = ttk.Frame(window)
  54. frame.pack(pady=5, fill=ttk.X)
  55. title_label = ttk.Label(frame, text=f"Title {i}", font=('Arial', 12))
  56. title_label.pack(side=ttk.LEFT, padx=5)
  57. entry = ttk.Entry(frame, font=('Arial', 12), width=20)
  58. entry.pack(side=ttk.LEFT, padx=5)
  59. entry_list.append(entry)
  60. # 添加一个新的Label来显示内容
  61. label = ttk.Label(frame, text=f"text {i}", font=('Arial', 12))
  62. label.pack(side=ttk.LEFT, padx=5)
  63. label_list.append(label)
  64. # 创建按钮
  65. fill_button = ttk.Button(window, text="Fill All with Hello", command=fill_entries_with_hello)
  66. fill_button.pack(pady=10)
  67. def fill_entries_with_hello():
  68. for entry in entry_list:
  69. entry.delete(0, ttk.END)
  70. entry.insert(0, "Hello")
  71. def display_text_area():
  72. # 清空当前窗口内容,但保留菜单栏
  73. for widget in window.winfo_children():
  74. if not isinstance(widget, ttk.Menu):
  75. widget.destroy()
  76. global text_area
  77. text_area = ttk.Text(window, wrap=ttk.WORD, font=('Arial', 12))
  78. text_area.pack(side=ttk.TOP, fill=ttk.BOTH, expand=True)
  79. # 创建窗口并设置主题为 'flatly'
  80. window = ttk.Window(themename='flatly')
  81. # 设置窗口大小为 2000x1200
  82. window.geometry("2000x1200")
  83. # 初始显示 text_area
  84. display_text_area()
  85. # 创建菜单栏
  86. menu_bar = ttk.Menu(window)
  87. # 创建第一个菜单及其子菜单
  88. first_menu = ttk.Menu(menu_bar, tearoff=0)
  89. for i in range(1, 11):
  90. menu_label = f"Item {i}"
  91. first_menu.add_command(label=menu_label, command=lambda label=menu_label: on_menu_item_click(label))
  92. menu_bar.add_cascade(label="Menu 1", menu=first_menu)
  93. # 创建第二个菜单及其子菜单
  94. second_menu = ttk.Menu(menu_bar, tearoff=0)
  95. second_menu.add_command(label="Sub Item 1", command=lambda: on_menu_item_click("Sub Item 1"))
  96. second_menu.add_command(label="Sub Item 2", command=lambda: on_menu_item_click("Sub Item 2"))
  97. menu_bar.add_cascade(label="Menu 2", menu=second_menu)
  98. # 创建第三个菜单及其子菜单
  99. third_menu = ttk.Menu(menu_bar, tearoff=0)
  100. third_menu.add_command(label="关于本软件", command=lambda: on_menu_item_click("about"))
  101. #third_menu.add_command(label="Sub Item 2", command=lambda: on_menu_item_click("Sub Item 2"))
  102. menu_bar.add_cascade(label="关于", menu=third_menu)
  103. # 设置窗口的菜单栏
  104. window.config(menu=menu_bar)
  105. # 运行窗口主循环
  106. window.mainloop()