test-ui-3.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. import tkinter as tk
  2. from tkinter import ttk
  3. def show_tab(index):
  4. notebook.select(index) # 使用 notebook 的 select 方法切换到指定索引的 tab
  5. root = tk.Tk()
  6. root.title("自定义按钮切换 Notebook 内容")
  7. # 创建一个 Style 实例
  8. style = ttk.Style()
  9. # 为我们的 Notebook 创建一个新的样式名称
  10. notebook_style_name = "NoTabs.TNotebook"
  11. style.configure(notebook_style_name) # 可以进行其他配置,如果需要
  12. # 移除这个新样式的 Tab 布局
  13. style.layout(notebook_style_name + ".Tab", [])
  14. notebook = ttk.Notebook(root, style=notebook_style_name) # 将自定义样式应用到这个 Notebook
  15. notebook.pack(fill="both", expand=True)
  16. # 创建不同的内容 Frame
  17. tab1_content = ttk.Frame(notebook)
  18. label1 = tk.Label(tab1_content, text="这是选项卡 1 的内容", padx=50, pady=50)
  19. label1.pack(expand=True, fill="both")
  20. notebook.add(tab1_content, text="标签一") # 虽然设置了 text,但我们会隐藏标签
  21. tab2_content = ttk.Frame(notebook)
  22. label2 = tk.Label(tab2_content, text="这是选项卡 2 的内容", padx=50, pady=50)
  23. label2.pack(expand=True, fill="both")
  24. notebook.add(tab2_content, text="标签二") # 同样设置了 text
  25. tab3_content = ttk.Frame(notebook)
  26. label3 = tk.Label(tab3_content, text="这是选项卡 3 的内容", padx=50, pady=50)
  27. label3.pack(expand=True, fill="both")
  28. notebook.add(tab3_content, text="标签三") # 同样设置了 text
  29. # 创建自定义按钮
  30. button1 = tk.Button(root, text="显示内容 1", command=lambda: show_tab(0))
  31. button1.pack(side="left", padx=5)
  32. button2 = tk.Button(root, text="显示内容 2", command=lambda: show_tab(1))
  33. button2.pack(side="left", padx=5)
  34. button3 = tk.Button(root, text="显示内容 3", command=lambda: show_tab(2))
  35. button3.pack(side="left", padx=5)
  36. root.mainloop()
  37. # import tkinter as tk
  38. # from tkinter import ttk
  39. #
  40. # def add_new_tab():
  41. # global tab_count
  42. # tab_count += 1
  43. # new_tab = ttk.Frame(notebook)
  44. # label = tk.Label(new_tab, text=f"这是动态添加的选项卡 {tab_count}", padx=50, pady=50)
  45. # label.pack(expand=True, fill="both")
  46. # notebook.add(new_tab, text=f"动态标签 {tab_count}")
  47. #
  48. # root = tk.Tk()
  49. # root.title("动态添加 Notebook 标签")
  50. #
  51. # notebook = ttk.Notebook(root)
  52. # notebook.pack(fill="both", expand=True)
  53. #
  54. # # 初始添加一些标签
  55. # tab1 = ttk.Frame(notebook)
  56. # label1 = tk.Label(tab1, text="初始选项卡 1", padx=50, pady=50)
  57. # label1.pack(expand=True, fill="both")
  58. # notebook.add(tab1, text="标签一")
  59. #
  60. # tab2 = ttk.Frame(notebook)
  61. # label2 = tk.Label(tab2, text="初始选项卡 2", padx=50, pady=50)
  62. # label2.pack(expand=True, fill="both")
  63. # notebook.add(tab2, text="标签二")
  64. #
  65. # tab_count = 2 # 记录已经添加的标签数量
  66. #
  67. # # 添加一个按钮,点击时动态添加新标签
  68. # add_button = tk.Button(root, text="添加新标签", command=add_new_tab)
  69. # add_button.pack(pady=10)
  70. #
  71. # root.mainloop()