Guest User

Change Button Image

a guest
Aug 3rd, 2022
28
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.05 KB | None | 0 0
  1. #=======================================================================
  2. ########################################################################
  3. #=========================== Left Frame File ===========================
  4.  
  5. import customtkinter as ctk
  6.  
  7.  
  8. class LeftFrame:
  9.  
  10.     def __init__(self, master, changeColorFunc = None):
  11.        
  12.         self.main_frame = ctk.CTkFrame(master)
  13.         self.main_frame.pack()
  14.         self.main_theme = ctk.get_appearance_mode()
  15.         self.is_dark_mode = True
  16.        
  17.         self.changeColor = lambda theme : changeColorFunc(theme)
  18.  
  19.         self.dark_mode_toggle = ctk.CTkSwitch(self.main_frame, command=self.changeTheme)
  20.         self.dark_mode_toggle.pack()
  21.  
  22.     def changeTheme(self):
  23.  
  24.         if self.is_dark_mode:
  25.             self.main_theme = "Light"
  26.             self.is_dark_mode = False
  27.             ctk.set_appearance_mode(self.main_theme)
  28.             self.changeColor(self.main_theme)
  29.  
  30.         else:
  31.             self.main_theme = "Dark"
  32.             self.is_dark_mode = True
  33.             ctk.set_appearance_mode(self.main_theme)
  34.             self.changeColor(self.main_theme)
  35.  
  36.  
  37. #========================================================================
  38. #########################################################################
  39. #=========================== Right Frame File ===========================
  40.  
  41. import customtkinter as ctk
  42. from PIL import ImageTk, Image
  43.  
  44. class RightFrame:
  45.  
  46.     def __init__(self, master = None):
  47.        
  48.         self.main_frame = ctk.CTkFrame(master)
  49.         self.main_frame.pack()
  50.  
  51.         #============ Images for button ============
  52.  
  53.         self.github_logo_black = ImageTk.PhotoImage(Image.open(f"{self.PATH}/src/images/ui/github32_b.png"))
  54.         self.github_logo_white = ImageTk.PhotoImage(Image.open(f"{self.PATH}/src/images/ui/github32_w.png"))  # unused
  55.  
  56.  
  57.         #=========== Button ============
  58.  
  59.         self.btn_github = ctk.CTkButton(self.main_frame,
  60.                                           text="GitHub",
  61.                                           image=self.github_logo_black,
  62.                                           command=self.changeButtonColor)
  63.         self.btn_github.pack()
  64.         self.btn_github.configure(image=self.github_logo_black)
  65.  
  66.  
  67.     def changeButtonColor(self, mode):
  68.  
  69.         if mode == "Dark":
  70.             self.btn_github.configure(image=self.github_logo_white)
  71.  
  72.         elif mode == "Light":
  73.             self.btn_github.configure(image=self.github_logo_black)
  74.  
  75. #========================================================================
  76. #########################################################################
  77. #=========================== Main Window File ===========================
  78.  
  79. import customtkinter as ctk
  80. from left_frame import LeftFrame
  81. from right_frame import RightFrame
  82.  
  83. class App:
  84.  
  85.     def __init__(self):
  86.        
  87.         self.main = ctk.CTk()
  88.  
  89.         self.right = RightFrame(self.main)
  90.         self.left = LeftFrame(self.main, changeColorFunc=self.right.changeButtonColor)
  91.  
  92.  
  93.         self.main.mainloop()
  94.  
Advertisement
Add Comment
Please, Sign In to add comment