CustomTkinter/examples/example_background_image.py

66 lines
3.0 KiB
Python
Raw Normal View History

2023-04-10 15:11:14 +03:00
from __future__ import annotations
from typing import Any
2021-11-04 15:07:47 +03:00
import customtkinter
from PIL import Image
2021-11-04 15:07:47 +03:00
import os
customtkinter.set_appearance_mode("dark")
2021-11-04 15:07:47 +03:00
class App(customtkinter.CTk):
2022-11-28 01:51:42 +03:00
width = 900
height = 600
2021-11-04 15:07:47 +03:00
2023-04-10 15:11:14 +03:00
def __init__(self, *args: Any, **kwargs: Any):
super().__init__(*args, **kwargs)
2021-11-04 15:07:47 +03:00
2022-11-28 01:51:42 +03:00
self.title("CustomTkinter example_background_image.py")
self.geometry(f"{self.width}x{self.height}")
self.resizable(False, False)
2021-11-04 15:07:47 +03:00
2022-11-28 01:51:42 +03:00
# load and create background image
current_path = os.path.dirname(os.path.realpath(__file__))
self.bg_image = customtkinter.CTkImage(Image.open(current_path + "/test_images/bg_gradient.jpg"),
2022-11-28 01:51:42 +03:00
size=(self.width, self.height))
self.bg_image_label = customtkinter.CTkLabel(self, image=self.bg_image)
self.bg_image_label.grid(row=0, column=0)
# create login frame
self.login_frame = customtkinter.CTkFrame(self, corner_radius=0)
self.login_frame.grid(row=0, column=0, sticky="ns")
self.login_label = customtkinter.CTkLabel(self.login_frame, text="CustomTkinter\nLogin Page",
font=customtkinter.CTkFont(size=20, weight="bold"))
self.login_label.grid(row=0, column=0, padx=30, pady=(150, 15))
self.username_entry = customtkinter.CTkEntry(self.login_frame, width=200, placeholder_text="username")
self.username_entry.grid(row=1, column=0, padx=30, pady=(15, 15))
self.password_entry = customtkinter.CTkEntry(self.login_frame, width=200, show="*", placeholder_text="password")
self.password_entry.grid(row=2, column=0, padx=30, pady=(0, 15))
self.login_button = customtkinter.CTkButton(self.login_frame, text="Login", command=self.login_event, width=200)
self.login_button.grid(row=3, column=0, padx=30, pady=(15, 15))
# create main frame
self.main_frame = customtkinter.CTkFrame(self, corner_radius=0)
self.main_frame.grid_columnconfigure(0, weight=1)
self.main_label = customtkinter.CTkLabel(self.main_frame, text="CustomTkinter\nMain Page",
font=customtkinter.CTkFont(size=20, weight="bold"))
self.main_label.grid(row=0, column=0, padx=30, pady=(30, 15))
self.back_button = customtkinter.CTkButton(self.main_frame, text="Back", command=self.back_event, width=200)
self.back_button.grid(row=1, column=0, padx=30, pady=(15, 15))
def login_event(self):
print("Login pressed - username:", self.username_entry.get(), "password:", self.password_entry.get())
self.login_frame.grid_forget() # remove login frame
self.main_frame.grid(row=0, column=0, sticky="nsew", padx=100) # show main frame
def back_event(self):
self.main_frame.grid_forget() # remove main frame
self.login_frame.grid(row=0, column=0, sticky="ns") # show login frame
2021-11-04 15:07:47 +03:00
if __name__ == "__main__":
app = App()
2022-11-28 01:18:10 +03:00
app.mainloop()