complex_example.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. import tkinter
  2. import tkinter.messagebox
  3. import customtkinter
  4. customtkinter.set_appearance_mode("System") # Modes: "System" (standard), "Dark", "Light"
  5. customtkinter.set_default_color_theme("blue") # Themes: "blue" (standard), "green", "dark-blue"
  6. class App(customtkinter.CTk):
  7. def __init__(self):
  8. super().__init__()
  9. # configure window
  10. self.title("CustomTkinter complex_example.py")
  11. self.geometry(f"{1100}x{580}")
  12. # configure grid layout (4x4)
  13. self.grid_columnconfigure(1, weight=1)
  14. self.grid_columnconfigure((2, 3), weight=0)
  15. self.grid_rowconfigure((0, 1, 2), weight=1)
  16. # create sidebar frame with widgets
  17. self.sidebar_frame = customtkinter.CTkFrame(self, width=140, corner_radius=0)
  18. self.sidebar_frame.grid(row=0, column=0, rowspan=4, sticky="nsew")
  19. self.sidebar_frame.grid_rowconfigure(4, weight=1)
  20. self.logo_label = customtkinter.CTkLabel(self.sidebar_frame, text="CustomTkinter", font=customtkinter.CTkFont(size=20, weight="bold"))
  21. self.logo_label.grid(row=0, column=0, padx=20, pady=(20, 10))
  22. self.sidebar_button_1 = customtkinter.CTkButton(self.sidebar_frame, command=self.sidebar_button_event)
  23. self.sidebar_button_1.grid(row=1, column=0, padx=20, pady=10)
  24. self.sidebar_button_2 = customtkinter.CTkButton(self.sidebar_frame, command=self.sidebar_button_event)
  25. self.sidebar_button_2.grid(row=2, column=0, padx=20, pady=10)
  26. self.sidebar_button_3 = customtkinter.CTkButton(self.sidebar_frame, command=self.sidebar_button_event)
  27. self.sidebar_button_3.grid(row=3, column=0, padx=20, pady=10)
  28. self.appearance_mode_label = customtkinter.CTkLabel(self.sidebar_frame, text="Appearance Mode:", anchor="w")
  29. self.appearance_mode_label.grid(row=5, column=0, padx=20, pady=(10, 0))
  30. self.appearance_mode_optionemenu = customtkinter.CTkOptionMenu(self.sidebar_frame, values=["Light", "Dark", "System"],
  31. command=self.change_appearance_mode_event)
  32. self.appearance_mode_optionemenu.grid(row=6, column=0, padx=20, pady=(10, 10))
  33. self.scaling_label = customtkinter.CTkLabel(self.sidebar_frame, text="UI Scaling:", anchor="w")
  34. self.scaling_label.grid(row=7, column=0, padx=20, pady=(10, 0))
  35. self.scaling_optionemenu = customtkinter.CTkOptionMenu(self.sidebar_frame, values=["80%", "90%", "100%", "110%", "120%"],
  36. command=self.change_scaling_event)
  37. self.scaling_optionemenu.grid(row=8, column=0, padx=20, pady=(10, 20))
  38. # create main entry and button
  39. self.entry = customtkinter.CTkEntry(self, placeholder_text="CTkEntry")
  40. self.entry.grid(row=3, column=1, columnspan=2, padx=(20, 0), pady=(20, 20), sticky="nsew")
  41. self.main_button_1 = customtkinter.CTkButton(master=self, fg_color="transparent", border_width=2, text_color=("gray10", "#DCE4EE"))
  42. self.main_button_1.grid(row=3, column=3, padx=(20, 20), pady=(20, 20), sticky="nsew")
  43. # create textbox
  44. self.textbox = customtkinter.CTkTextbox(self, width=250)
  45. self.textbox.grid(row=0, column=1, padx=(20, 0), pady=(20, 0), sticky="nsew")
  46. # create tabview
  47. self.tabview = customtkinter.CTkTabview(self, width=250)
  48. self.tabview.grid(row=0, column=2, padx=(20, 0), pady=(20, 0), sticky="nsew")
  49. self.tabview.add("CTkTabview")
  50. self.tabview.add("Tab 2")
  51. self.tabview.add("Tab 3")
  52. self.tabview.tab("CTkTabview").grid_columnconfigure(0, weight=1) # configure grid of individual tabs
  53. self.tabview.tab("Tab 2").grid_columnconfigure(0, weight=1)
  54. self.optionmenu_1 = customtkinter.CTkOptionMenu(self.tabview.tab("CTkTabview"), dynamic_resizing=False,
  55. values=["Value 1", "Value 2", "Value Long Long Long"])
  56. self.optionmenu_1.grid(row=0, column=0, padx=20, pady=(20, 10))
  57. self.combobox_1 = customtkinter.CTkComboBox(self.tabview.tab("CTkTabview"),
  58. values=["Value 1", "Value 2", "Value Long....."])
  59. self.combobox_1.grid(row=1, column=0, padx=20, pady=(10, 10))
  60. self.string_input_button = customtkinter.CTkButton(self.tabview.tab("CTkTabview"), text="Open CTkInputDialog",
  61. command=self.open_input_dialog_event)
  62. self.string_input_button.grid(row=2, column=0, padx=20, pady=(10, 10))
  63. self.label_tab_2 = customtkinter.CTkLabel(self.tabview.tab("Tab 2"), text="CTkLabel on Tab 2")
  64. self.label_tab_2.grid(row=0, column=0, padx=20, pady=20)
  65. # create radiobutton frame
  66. self.radiobutton_frame = customtkinter.CTkFrame(self)
  67. self.radiobutton_frame.grid(row=0, column=3, padx=(20, 20), pady=(20, 0), sticky="nsew")
  68. self.radio_var = tkinter.IntVar(value=0)
  69. self.label_radio_group = customtkinter.CTkLabel(master=self.radiobutton_frame, text="CTkRadioButton Group:")
  70. self.label_radio_group.grid(row=0, column=2, columnspan=1, padx=10, pady=10, sticky="")
  71. self.radio_button_1 = customtkinter.CTkRadioButton(master=self.radiobutton_frame, variable=self.radio_var, value=0)
  72. self.radio_button_1.grid(row=1, column=2, pady=10, padx=20, sticky="n")
  73. self.radio_button_2 = customtkinter.CTkRadioButton(master=self.radiobutton_frame, variable=self.radio_var, value=1)
  74. self.radio_button_2.grid(row=2, column=2, pady=10, padx=20, sticky="n")
  75. self.radio_button_3 = customtkinter.CTkRadioButton(master=self.radiobutton_frame, variable=self.radio_var, value=2)
  76. self.radio_button_3.grid(row=3, column=2, pady=10, padx=20, sticky="n")
  77. # create slider and progressbar frame
  78. self.slider_progressbar_frame = customtkinter.CTkFrame(self, fg_color="transparent")
  79. self.slider_progressbar_frame.grid(row=1, column=1, padx=(20, 0), pady=(20, 0), sticky="nsew")
  80. self.slider_progressbar_frame.grid_columnconfigure(0, weight=1)
  81. self.slider_progressbar_frame.grid_rowconfigure(4, weight=1)
  82. self.seg_button_1 = customtkinter.CTkSegmentedButton(self.slider_progressbar_frame)
  83. self.seg_button_1.grid(row=0, column=0, padx=(20, 10), pady=(10, 10), sticky="ew")
  84. self.progressbar_1 = customtkinter.CTkProgressBar(self.slider_progressbar_frame)
  85. self.progressbar_1.grid(row=1, column=0, padx=(20, 10), pady=(10, 10), sticky="ew")
  86. self.progressbar_2 = customtkinter.CTkProgressBar(self.slider_progressbar_frame)
  87. self.progressbar_2.grid(row=2, column=0, padx=(20, 10), pady=(10, 10), sticky="ew")
  88. self.slider_1 = customtkinter.CTkSlider(self.slider_progressbar_frame, from_=0, to=1, number_of_steps=4)
  89. self.slider_1.grid(row=3, column=0, padx=(20, 10), pady=(10, 10), sticky="ew")
  90. self.slider_2 = customtkinter.CTkSlider(self.slider_progressbar_frame, orientation="vertical")
  91. self.slider_2.grid(row=0, column=1, rowspan=5, padx=(10, 10), pady=(10, 10), sticky="ns")
  92. self.progressbar_3 = customtkinter.CTkProgressBar(self.slider_progressbar_frame, orientation="vertical")
  93. self.progressbar_3.grid(row=0, column=2, rowspan=5, padx=(10, 20), pady=(10, 10), sticky="ns")
  94. # create scrollable frame
  95. self.scrollable_frame = customtkinter.CTkScrollableFrame(self, label_text="CTkScrollableFrame")
  96. self.scrollable_frame.grid(row=1, column=2, padx=(20, 0), pady=(20, 0), sticky="nsew")
  97. self.scrollable_frame.grid_columnconfigure(0, weight=1)
  98. self.scrollable_frame_switches = []
  99. for i in range(100):
  100. switch = customtkinter.CTkSwitch(master=self.scrollable_frame, text=f"CTkSwitch {i}")
  101. switch.grid(row=i, column=0, padx=10, pady=(0, 20))
  102. self.scrollable_frame_switches.append(switch)
  103. # create checkbox and switch frame
  104. self.checkbox_slider_frame = customtkinter.CTkFrame(self)
  105. self.checkbox_slider_frame.grid(row=1, column=3, padx=(20, 20), pady=(20, 0), sticky="nsew")
  106. self.checkbox_1 = customtkinter.CTkCheckBox(master=self.checkbox_slider_frame)
  107. self.checkbox_1.grid(row=1, column=0, pady=(20, 0), padx=20, sticky="n")
  108. self.checkbox_2 = customtkinter.CTkCheckBox(master=self.checkbox_slider_frame)
  109. self.checkbox_2.grid(row=2, column=0, pady=(20, 0), padx=20, sticky="n")
  110. self.checkbox_3 = customtkinter.CTkCheckBox(master=self.checkbox_slider_frame)
  111. self.checkbox_3.grid(row=3, column=0, pady=20, padx=20, sticky="n")
  112. # set default values
  113. self.sidebar_button_3.configure(state="disabled", text="Disabled CTkButton")
  114. self.checkbox_3.configure(state="disabled")
  115. self.checkbox_1.select()
  116. self.scrollable_frame_switches[0].select()
  117. self.scrollable_frame_switches[4].select()
  118. self.radio_button_3.configure(state="disabled")
  119. self.appearance_mode_optionemenu.set("Dark")
  120. self.scaling_optionemenu.set("100%")
  121. self.optionmenu_1.set("CTkOptionmenu")
  122. self.combobox_1.set("CTkComboBox")
  123. self.slider_1.configure(command=self.progressbar_2.set)
  124. self.slider_2.configure(command=self.progressbar_3.set)
  125. self.progressbar_1.configure(mode="indeterminnate")
  126. self.progressbar_1.start()
  127. self.textbox.insert("0.0", "CTkTextbox\n\n" + "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua.\n\n" * 20)
  128. self.seg_button_1.configure(values=["CTkSegmentedButton", "Value 2", "Value 3"])
  129. self.seg_button_1.set("Value 2")
  130. def open_input_dialog_event(self):
  131. dialog = customtkinter.CTkInputDialog(text="Type in a number:", title="CTkInputDialog")
  132. print("CTkInputDialog:", dialog.get_input())
  133. def change_appearance_mode_event(self, new_appearance_mode: str):
  134. customtkinter.set_appearance_mode(new_appearance_mode)
  135. def change_scaling_event(self, new_scaling: str):
  136. new_scaling_float = int(new_scaling.replace("%", "")) / 100
  137. customtkinter.set_widget_scaling(new_scaling_float)
  138. def sidebar_button_event(self):
  139. print("sidebar_button click")
  140. if __name__ == "__main__":
  141. app = App()
  142. app.mainloop()