test_ctk_toplevel.py 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import customtkinter
  2. customtkinter.set_appearance_mode("dark")
  3. class ToplevelWindow(customtkinter.CTkToplevel):
  4. def __init__(self, *args, closing_event=None, **kwargs):
  5. super().__init__(*args, **kwargs)
  6. self.protocol("WM_DELETE_WINDOW", self.closing)
  7. self.geometry("500x300")
  8. self.resizable(False, False)
  9. self.closing_event = closing_event
  10. self.label = customtkinter.CTkLabel(self, text="ToplevelWindow")
  11. self.label.pack(padx=20, pady=20)
  12. self.button_1 = customtkinter.CTkButton(self, text="set dark", command=lambda: customtkinter.set_appearance_mode("dark"))
  13. self.button_1.pack(side="top", padx=40, pady=40)
  14. def closing(self):
  15. self.destroy()
  16. if self.closing_event is not None:
  17. self.closing_event()
  18. class App(customtkinter.CTk):
  19. def __init__(self):
  20. super().__init__()
  21. self.geometry("500x400")
  22. self.resizable(False, False)
  23. self.button_1 = customtkinter.CTkButton(self, text="Open CTkToplevel", command=self.open_toplevel)
  24. self.button_1.pack(side="top", padx=40, pady=40)
  25. self.button_2 = customtkinter.CTkButton(self, text="iconify toplevel", command=lambda: self.toplevel_window.iconify())
  26. self.button_2.pack(side="top", padx=40, pady=40)
  27. self.button_3 = customtkinter.CTkButton(self, text="set light", command=lambda: customtkinter.set_appearance_mode("light"))
  28. self.button_3.pack(side="top", padx=40, pady=40)
  29. self.toplevel_window = None
  30. def open_toplevel(self):
  31. if self.toplevel_window is None: # create toplevel window only if not already open
  32. self.toplevel_window = ToplevelWindow(self, closing_event=self.toplevel_close_event)
  33. def toplevel_close_event(self):
  34. self.toplevel_window = None
  35. if __name__ == "__main__":
  36. app = App()
  37. app.mainloop()