example_background_image.py 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import customtkinter
  2. from PIL import Image
  3. import os
  4. customtkinter.set_appearance_mode("dark")
  5. class App(customtkinter.CTk):
  6. width = 900
  7. height = 600
  8. def __init__(self, *args, **kwargs):
  9. super().__init__(*args, **kwargs)
  10. self.title("CustomTkinter example_background_image.py")
  11. self.geometry(f"{self.width}x{self.height}")
  12. self.resizable(False, False)
  13. # load and create background image
  14. current_path = os.path.dirname(os.path.realpath(__file__))
  15. self.bg_image = customtkinter.CTkImage(Image.open(current_path + "/test_images/bg_gradient.jpg"),
  16. size=(self.width, self.height))
  17. self.bg_image_label = customtkinter.CTkLabel(self, image=self.bg_image)
  18. self.bg_image_label.grid(row=0, column=0)
  19. # create login frame
  20. self.login_frame = customtkinter.CTkFrame(self, corner_radius=0)
  21. self.login_frame.grid(row=0, column=0, sticky="ns")
  22. self.login_label = customtkinter.CTkLabel(self.login_frame, text="CustomTkinter\nLogin Page",
  23. font=customtkinter.CTkFont(size=20, weight="bold"))
  24. self.login_label.grid(row=0, column=0, padx=30, pady=(150, 15))
  25. self.username_entry = customtkinter.CTkEntry(self.login_frame, width=200, placeholder_text="username")
  26. self.username_entry.grid(row=1, column=0, padx=30, pady=(15, 15))
  27. self.password_entry = customtkinter.CTkEntry(self.login_frame, width=200, show="*", placeholder_text="password")
  28. self.password_entry.grid(row=2, column=0, padx=30, pady=(0, 15))
  29. self.login_button = customtkinter.CTkButton(self.login_frame, text="Login", command=self.login_event, width=200)
  30. self.login_button.grid(row=3, column=0, padx=30, pady=(15, 15))
  31. # create main frame
  32. self.main_frame = customtkinter.CTkFrame(self, corner_radius=0)
  33. self.main_frame.grid_columnconfigure(0, weight=1)
  34. self.main_label = customtkinter.CTkLabel(self.main_frame, text="CustomTkinter\nMain Page",
  35. font=customtkinter.CTkFont(size=20, weight="bold"))
  36. self.main_label.grid(row=0, column=0, padx=30, pady=(30, 15))
  37. self.back_button = customtkinter.CTkButton(self.main_frame, text="Back", command=self.back_event, width=200)
  38. self.back_button.grid(row=1, column=0, padx=30, pady=(15, 15))
  39. def login_event(self):
  40. print("Login pressed - username:", self.username_entry.get(), "password:", self.password_entry.get())
  41. self.login_frame.grid_forget() # remove login frame
  42. self.main_frame.grid(row=0, column=0, sticky="nsew", padx=100) # show main frame
  43. def back_event(self):
  44. self.main_frame.grid_forget() # remove main frame
  45. self.login_frame.grid(row=0, column=0, sticky="ns") # show login frame
  46. if __name__ == "__main__":
  47. app = App()
  48. app.mainloop()