scrollable_frame_example.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. import customtkinter
  2. import os
  3. from PIL import Image
  4. class ScrollableCheckBoxFrame(customtkinter.CTkScrollableFrame):
  5. def __init__(self, master, item_list, command=None, **kwargs):
  6. super().__init__(master, **kwargs)
  7. self.command = command
  8. self.checkbox_list = []
  9. for i, item in enumerate(item_list):
  10. self.add_item(item)
  11. def add_item(self, item):
  12. checkbox = customtkinter.CTkCheckBox(self, text=item)
  13. if self.command is not None:
  14. checkbox.configure(command=self.command)
  15. checkbox.grid(row=len(self.checkbox_list), column=0, pady=(0, 10))
  16. self.checkbox_list.append(checkbox)
  17. def remove_item(self, item):
  18. for checkbox in self.checkbox_list:
  19. if item == checkbox.cget("text"):
  20. checkbox.destroy()
  21. self.checkbox_list.remove(checkbox)
  22. return
  23. def get_checked_items(self):
  24. return [checkbox.cget("text") for checkbox in self.checkbox_list if checkbox.get() == 1]
  25. class ScrollableRadiobuttonFrame(customtkinter.CTkScrollableFrame):
  26. def __init__(self, master, item_list, command=None, **kwargs):
  27. super().__init__(master, **kwargs)
  28. self.command = command
  29. self.radiobutton_variable = customtkinter.StringVar()
  30. self.radiobutton_list = []
  31. for i, item in enumerate(item_list):
  32. self.add_item(item)
  33. def add_item(self, item):
  34. radiobutton = customtkinter.CTkRadioButton(self, text=item, value=item, variable=self.radiobutton_variable)
  35. if self.command is not None:
  36. radiobutton.configure(command=self.command)
  37. radiobutton.grid(row=len(self.radiobutton_list), column=0, pady=(0, 10))
  38. self.radiobutton_list.append(radiobutton)
  39. def remove_item(self, item):
  40. for radiobutton in self.radiobutton_list:
  41. if item == radiobutton.cget("text"):
  42. radiobutton.destroy()
  43. self.radiobutton_list.remove(radiobutton)
  44. return
  45. def get_checked_item(self):
  46. return self.radiobutton_variable.get()
  47. class ScrollableLabelButtonFrame(customtkinter.CTkScrollableFrame):
  48. def __init__(self, master, command=None, **kwargs):
  49. super().__init__(master, **kwargs)
  50. self.grid_columnconfigure(0, weight=1)
  51. self.command = command
  52. self.radiobutton_variable = customtkinter.StringVar()
  53. self.label_list = []
  54. self.button_list = []
  55. def add_item(self, item, image=None):
  56. label = customtkinter.CTkLabel(self, text=item, image=image, compound="left", padx=5, anchor="w")
  57. button = customtkinter.CTkButton(self, text="Command", width=100, height=24)
  58. if self.command is not None:
  59. button.configure(command=lambda: self.command(item))
  60. label.grid(row=len(self.label_list), column=0, pady=(0, 10), sticky="w")
  61. button.grid(row=len(self.button_list), column=1, pady=(0, 10), padx=5)
  62. self.label_list.append(label)
  63. self.button_list.append(button)
  64. def remove_item(self, item):
  65. for label, button in zip(self.label_list, self.button_list):
  66. if item == label.cget("text"):
  67. label.destroy()
  68. button.destroy()
  69. self.label_list.remove(label)
  70. self.button_list.remove(button)
  71. return
  72. class App(customtkinter.CTk):
  73. def __init__(self):
  74. super().__init__()
  75. self.title("CTkScrollableFrame example")
  76. self.grid_rowconfigure(0, weight=1)
  77. self.columnconfigure(2, weight=1)
  78. # create scrollable checkbox frame
  79. self.scrollable_checkbox_frame = ScrollableCheckBoxFrame(master=self, width=200, command=self.checkbox_frame_event,
  80. item_list=[f"item {i}" for i in range(50)])
  81. self.scrollable_checkbox_frame.grid(row=0, column=0, padx=15, pady=15, sticky="ns")
  82. self.scrollable_checkbox_frame.add_item("new item")
  83. # create scrollable radiobutton frame
  84. self.scrollable_radiobutton_frame = ScrollableRadiobuttonFrame(master=self, width=500, command=self.radiobutton_frame_event,
  85. item_list=[f"item {i}" for i in range(100)],
  86. label_text="ScrollableRadiobuttonFrame")
  87. self.scrollable_radiobutton_frame.grid(row=0, column=1, padx=15, pady=15, sticky="ns")
  88. self.scrollable_radiobutton_frame.configure(width=200)
  89. self.scrollable_radiobutton_frame.remove_item("item 3")
  90. # create scrollable label and button frame
  91. current_dir = os.path.dirname(os.path.abspath(__file__))
  92. self.scrollable_label_button_frame = ScrollableLabelButtonFrame(master=self, width=300, command=self.label_button_frame_event, corner_radius=0)
  93. self.scrollable_label_button_frame.grid(row=0, column=2, padx=0, pady=0, sticky="nsew")
  94. for i in range(20): # add items with images
  95. self.scrollable_label_button_frame.add_item(f"image and item {i}", image=customtkinter.CTkImage(Image.open(os.path.join(current_dir, "test_images", "chat_light.png"))))
  96. def checkbox_frame_event(self):
  97. print(f"checkbox frame modified: {self.scrollable_checkbox_frame.get_checked_items()}")
  98. def radiobutton_frame_event(self):
  99. print(f"radiobutton frame modified: {self.scrollable_radiobutton_frame.get_checked_item()}")
  100. def label_button_frame_event(self, item):
  101. print(f"label button frame clicked: {item}")
  102. if __name__ == "__main__":
  103. customtkinter.set_appearance_mode("dark")
  104. app = App()
  105. app.mainloop()