CustomTkinter/examples/example_background_image.py

67 lines
2.5 KiB
Python
Raw Normal View History

2021-11-04 15:07:47 +03:00
import tkinter.messagebox
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):
2021-11-04 15:07:47 +03:00
APP_NAME = "CustomTkinter example_background_image.py"
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.resizable(False, False)
2021-11-04 15:07:47 +03:00
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))
2021-11-04 15:07:47 +03:00
self.image_label = customtkinter.CTkLabel(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, corner_radius=6,
2022-05-22 20:56:18 +03:00
fg_color=("gray70", "gray25"), text="CustomTkinter\ninterface example")
self.label_1.place(relx=0.5, rely=0.3, anchor=tkinter.CENTER)
2022-05-22 20:56:18 +03:00
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)
2022-05-22 20:56:18 +03:00
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)
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()