CustomTkinter/examples/example_background_image.py

67 lines
2.5 KiB
Python

import tkinter.messagebox
import customtkinter
from PIL import Image
import os
customtkinter.set_appearance_mode("dark")
class App(customtkinter.CTk):
APP_NAME = "CustomTkinter example_background_image.py"
WIDTH = 900
HEIGHT = 600
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.title(App.APP_NAME)
self.geometry(f"{App.WIDTH}x{App.HEIGHT}")
self.minsize(App.WIDTH, App.HEIGHT)
self.maxsize(App.WIDTH, App.HEIGHT)
self.resizable(False, False)
self.protocol("WM_DELETE_WINDOW", self.on_closing)
# load image with PIL and convert to PhotoImage
current_path = os.path.dirname(os.path.realpath(__file__))
self.bg_image = customtkinter.CTkImage(Image.open(current_path + "/test_images/bg_gradient.jpg"),
size=(self.WIDTH, self.HEIGHT))
self.image_label = customtkinter.CTkLabel(master=self, image=self.bg_image)
self.image_label.place(relx=0.5, rely=0.5, anchor=tkinter.CENTER)
self.frame = customtkinter.CTkFrame(master=self,
width=300,
height=App.HEIGHT,
corner_radius=0)
self.frame.place(relx=0.5, rely=0.5, anchor=tkinter.CENTER)
self.label_1 = customtkinter.CTkLabel(master=self.frame, width=200, height=60, corner_radius=6,
fg_color=("gray70", "gray25"), text="CustomTkinter\ninterface example")
self.label_1.place(relx=0.5, rely=0.3, anchor=tkinter.CENTER)
self.entry_1 = customtkinter.CTkEntry(master=self.frame, corner_radius=6, width=200, placeholder_text="username")
self.entry_1.place(relx=0.5, rely=0.52, anchor=tkinter.CENTER)
self.entry_2 = customtkinter.CTkEntry(master=self.frame, corner_radius=6, width=200, show="*", placeholder_text="password")
self.entry_2.place(relx=0.5, rely=0.6, anchor=tkinter.CENTER)
self.button_2 = customtkinter.CTkButton(master=self.frame, text="Login",
corner_radius=6, command=self.button_event, width=200)
self.button_2.place(relx=0.5, rely=0.7, anchor=tkinter.CENTER)
def button_event(self):
print("Login pressed - username:", self.entry_1.get(), "password:", self.entry_2.get())
def on_closing(self, event=0):
self.destroy()
def start(self):
self.mainloop()
if __name__ == "__main__":
app = App()
app.start()