image_example.py 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. import customtkinter
  2. import os
  3. from PIL import Image
  4. class App(customtkinter.CTk):
  5. def __init__(self):
  6. super().__init__()
  7. self.title("image_example.py")
  8. self.geometry("700x450")
  9. # set grid layout 1x2
  10. self.grid_rowconfigure(0, weight=1)
  11. self.grid_columnconfigure(1, weight=1)
  12. # load images with light and dark mode image
  13. image_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), "test_images")
  14. self.logo_image = customtkinter.CTkImage(Image.open(os.path.join(image_path, "CustomTkinter_logo_single.png")), size=(26, 26))
  15. self.large_test_image = customtkinter.CTkImage(Image.open(os.path.join(image_path, "large_test_image.png")), size=(500, 150))
  16. self.image_icon_image = customtkinter.CTkImage(Image.open(os.path.join(image_path, "image_icon_light.png")), size=(20, 20))
  17. self.home_image = customtkinter.CTkImage(light_image=Image.open(os.path.join(image_path, "home_dark.png")),
  18. dark_image=Image.open(os.path.join(image_path, "home_light.png")), size=(20, 20))
  19. self.chat_image = customtkinter.CTkImage(light_image=Image.open(os.path.join(image_path, "chat_dark.png")),
  20. dark_image=Image.open(os.path.join(image_path, "chat_light.png")), size=(20, 20))
  21. self.add_user_image = customtkinter.CTkImage(light_image=Image.open(os.path.join(image_path, "add_user_dark.png")),
  22. dark_image=Image.open(os.path.join(image_path, "add_user_light.png")), size=(20, 20))
  23. # create navigation frame
  24. self.navigation_frame = customtkinter.CTkFrame(self, corner_radius=0)
  25. self.navigation_frame.grid(row=0, column=0, sticky="nsew")
  26. self.navigation_frame.grid_rowconfigure(4, weight=1)
  27. self.navigation_frame_label = customtkinter.CTkLabel(self.navigation_frame, text=" Image Example", image=self.logo_image,
  28. compound="left", font=customtkinter.CTkFont(size=15, weight="bold"))
  29. self.navigation_frame_label.grid(row=0, column=0, padx=20, pady=20)
  30. self.home_button = customtkinter.CTkButton(self.navigation_frame, corner_radius=0, height=40, border_spacing=10, text="Home",
  31. fg_color="transparent", text_color=("gray10", "gray90"), hover_color=("gray70", "gray30"),
  32. image=self.home_image, anchor="w", command=self.home_button_event)
  33. self.home_button.grid(row=1, column=0, sticky="ew")
  34. self.frame_2_button = customtkinter.CTkButton(self.navigation_frame, corner_radius=0, height=40, border_spacing=10, text="Frame 2",
  35. fg_color="transparent", text_color=("gray10", "gray90"), hover_color=("gray70", "gray30"),
  36. image=self.chat_image, anchor="w", command=self.frame_2_button_event)
  37. self.frame_2_button.grid(row=2, column=0, sticky="ew")
  38. self.frame_3_button = customtkinter.CTkButton(self.navigation_frame, corner_radius=0, height=40, border_spacing=10, text="Frame 3",
  39. fg_color="transparent", text_color=("gray10", "gray90"), hover_color=("gray70", "gray30"),
  40. image=self.add_user_image, anchor="w", command=self.frame_3_button_event)
  41. self.frame_3_button.grid(row=3, column=0, sticky="ew")
  42. self.appearance_mode_menu = customtkinter.CTkOptionMenu(self.navigation_frame, values=["Light", "Dark", "System"],
  43. command=self.change_appearance_mode_event)
  44. self.appearance_mode_menu.grid(row=6, column=0, padx=20, pady=20, sticky="s")
  45. # create home frame
  46. self.home_frame = customtkinter.CTkFrame(self, corner_radius=0, fg_color="transparent")
  47. self.home_frame.grid_columnconfigure(0, weight=1)
  48. self.home_frame_large_image_label = customtkinter.CTkLabel(self.home_frame, text="", image=self.large_test_image)
  49. self.home_frame_large_image_label.grid(row=0, column=0, padx=20, pady=10)
  50. self.home_frame_button_1 = customtkinter.CTkButton(self.home_frame, text="", image=self.image_icon_image)
  51. self.home_frame_button_1.grid(row=1, column=0, padx=20, pady=10)
  52. self.home_frame_button_2 = customtkinter.CTkButton(self.home_frame, text="CTkButton", image=self.image_icon_image, compound="right")
  53. self.home_frame_button_2.grid(row=2, column=0, padx=20, pady=10)
  54. self.home_frame_button_3 = customtkinter.CTkButton(self.home_frame, text="CTkButton", image=self.image_icon_image, compound="top")
  55. self.home_frame_button_3.grid(row=3, column=0, padx=20, pady=10)
  56. self.home_frame_button_4 = customtkinter.CTkButton(self.home_frame, text="CTkButton", image=self.image_icon_image, compound="bottom", anchor="w")
  57. self.home_frame_button_4.grid(row=4, column=0, padx=20, pady=10)
  58. # create second frame
  59. self.second_frame = customtkinter.CTkFrame(self, corner_radius=0, fg_color="transparent")
  60. # create third frame
  61. self.third_frame = customtkinter.CTkFrame(self, corner_radius=0, fg_color="transparent")
  62. # select default frame
  63. self.select_frame_by_name("home")
  64. def select_frame_by_name(self, name):
  65. # set button color for selected button
  66. self.home_button.configure(fg_color=("gray75", "gray25") if name == "home" else "transparent")
  67. self.frame_2_button.configure(fg_color=("gray75", "gray25") if name == "frame_2" else "transparent")
  68. self.frame_3_button.configure(fg_color=("gray75", "gray25") if name == "frame_3" else "transparent")
  69. # show selected frame
  70. if name == "home":
  71. self.home_frame.grid(row=0, column=1, sticky="nsew")
  72. else:
  73. self.home_frame.grid_forget()
  74. if name == "frame_2":
  75. self.second_frame.grid(row=0, column=1, sticky="nsew")
  76. else:
  77. self.second_frame.grid_forget()
  78. if name == "frame_3":
  79. self.third_frame.grid(row=0, column=1, sticky="nsew")
  80. else:
  81. self.third_frame.grid_forget()
  82. def home_button_event(self):
  83. self.select_frame_by_name("home")
  84. def frame_2_button_event(self):
  85. self.select_frame_by_name("frame_2")
  86. def frame_3_button_event(self):
  87. self.select_frame_by_name("frame_3")
  88. def change_appearance_mode_event(self, new_appearance_mode):
  89. customtkinter.set_appearance_mode(new_appearance_mode)
  90. if __name__ == "__main__":
  91. app = App()
  92. app.mainloop()