CustomTkinter/examples/example_background_image.py

72 lines
2.8 KiB
Python
Raw Normal View History

2021-11-04 15:07:47 +03:00
import tkinter
import tkinter.messagebox
import customtkinter
from PIL import Image, ImageTk
import os
2022-01-22 22:28:59 +03:00
customtkinter.set_appearance_mode("Dark") # Modes: "System" (standard), "Dark", "Light"
2022-01-08 02:29:53 +03:00
customtkinter.set_default_color_theme("blue") # Themes: "blue" (standard), "green", "dark-blue"
2021-11-04 15:07:47 +03:00
PATH = os.path.dirname(os.path.realpath(__file__))
class App(customtkinter.CTk):
2021-11-04 15:07:47 +03:00
APP_NAME = "CustomTkinter background gradient image"
2021-11-04 15:07:47 +03:00
WIDTH = 900
HEIGHT = 600
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
2021-11-04 15:07:47 +03:00
self.title(App.APP_NAME)
self.geometry(f"{App.WIDTH}x{App.HEIGHT}")
2021-11-04 15:07:47 +03:00
self.minsize(App.WIDTH, App.HEIGHT)
self.maxsize(App.WIDTH, App.HEIGHT)
self.protocol("WM_DELETE_WINDOW", self.on_closing)
self.bind("<Command-q>", self.on_closing)
self.bind("<Command-w>", self.on_closing)
self.createcommand('tk::mac::Quit', self.on_closing)
# load image with PIL and convert to PhotoImage
image = Image.open(PATH + "/test_images/bg_gradient.jpg").resize((self.WIDTH, self.HEIGHT))
self.bg_image = ImageTk.PhotoImage(image)
2021-11-04 15:07:47 +03:00
self.image_label = tkinter.Label(master=self, image=self.bg_image)
2021-11-04 15:07:47 +03:00
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,
fg_color=("gray70", "gray35"), text="CustomTkinter\ninterface example")
self.label_1.place(relx=0.5, rely=0.3, anchor=tkinter.CENTER)
2022-02-04 23:28:37 +03:00
self.entry_1 = customtkinter.CTkEntry(master=self.frame, corner_radius=20, width=200, placeholder_text="username")
self.entry_1.place(relx=0.5, rely=0.52, anchor=tkinter.CENTER)
2022-02-04 23:28:37 +03:00
self.entry_2 = customtkinter.CTkEntry(master=self.frame, corner_radius=20, width=200, show="*", placeholder_text="password")
self.entry_2.place(relx=0.5, rely=0.6, anchor=tkinter.CENTER)
2021-11-04 15:07:47 +03:00
self.button_2 = customtkinter.CTkButton(master=self.frame, text="Login",
2022-01-22 14:00:29 +03:00
corner_radius=6, command=self.button_event, width=200)
2021-11-04 15:07:47 +03:00
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())
2021-11-04 15:07:47 +03:00
def on_closing(self, event=0):
self.destroy()
def start(self):
self.mainloop()
if __name__ == "__main__":
app = App()
app.start()