import customtkinter as ctk
from PIL import Image, ImageTk
from tkinter import messagebox
import time
class LoginApp(ctk.CTk):
def __init__(self):
super().__init__()
self.title("登录页面")
self.geometry("600x400")
self.resizable(False, False)
# 设置背景颜色为白色
self.configure(bg="white")
# 创建帧来分隔图片和登录信息
self.frame_image = ctk.CTkFrame(self, width=300, fg_color="white")
self.frame_image.place(relx=0, rely=0, relheight=1, relwidth=0.5)
self.frame_login = ctk.CTkFrame(self, fg_color="white")
self.frame_login.place(relx=0.5, rely=0, relheight=1, relwidth=0.5)
# 加载图片
self.image_paths = ["img.jpg", "img2.jpg"]
self.images = [ImageTk.PhotoImage(Image.open(img).resize((300, 400), Image.LANCZOS)) for img in self.image_paths]
self.image_index = 0
# 使用 CTkCanvas 显示图片
self.canvas_image = ctk.CTkCanvas(self.frame_image, width=300, height=400, bg="white", highlightthickness=0)
self.canvas_image.pack(fill="both", expand=True)
# 显示初始图片
self.image_on_canvas = self.canvas_image.create_image(0, 0, anchor="nw", image=self.images[self.image_index])
# 创建一个主框架用于垂直布局
self.main_frame = ctk.CTkFrame(self.frame_login, fg_color="white")
self.main_frame.pack(expand=True)
# 使用 CTkCanvas 显示 logo 图片
self.logo_image = ImageTk.PhotoImage(Image.open("logo.jpg").resize((50, 50), Image.LANCZOS))
self.canvas_logo = ctk.CTkCanvas(self.main_frame, width=50, height=50, bg="white", highlightthickness=0)
self.canvas_logo.pack(pady=(20, 10))
self.canvas_logo.create_image(0, 0, anchor="nw", image=self.logo_image)
# 用户名标签和输入框
self.frame_username = ctk.CTkFrame(self.main_frame, fg_color="white")
self.frame_username.pack(pady=10, anchor="w")
self.label_username = ctk.CTkLabel(self.frame_username, text="用户名:", fg_color="white", anchor="w")
self.label_username.pack(side="left")
self.entry_username = ctk.CTkEntry(self.frame_username, placeholder_text="请输入用户名", fg_color="white", border_color="black", border_width=0, height=25)
self.entry_username.pack(side="left", padx=10)
# 密码标签和输入框
self.frame_password = ctk.CTkFrame(self.main_frame, fg_color="white")
self.frame_password.pack(pady=10, anchor="w")
self.label_password = ctk.CTkLabel(self.frame_password, text="密码:", fg_color="white", anchor="w")
self.label_password.pack(side="left")
self.entry_password = ctk.CTkEntry(self.frame_password, show="*", placeholder_text="请输入密码", fg_color="white", border_color="black", border_width=0, height=25)
self.entry_password.pack(side="left", padx=10)
# 绑定回车键触发登录
self.entry_password.bind("<Return>", lambda event: self.login())
# 登录按钮
self.button_login = ctk.CTkButton(self.main_frame, text="登录", command=self.login, fg_color="#0099ff", text_color="white", width=200)
self.button_login.pack(pady=(10, 20))
# 加载状态标签
self.loading_label = ctk.CTkLabel(self.main_frame, text="", fg_color="white")
self.loading_label.pack(pady=(10, 0))
# 启动图片轮播
self.update_image()
# 示例用户名和密码
self.valid_username = "user"
self.valid_password = "password"
def update_image(self):
self.image_index = (self.image_index + 1) % len(self.images)
self.canvas_image.itemconfig(self.image_on_canvas, image=self.images[self.image_index])
self.after(3000, self.update_image)
def login(self):
username = self.entry_username.get()
password = self.entry_password.get()
# 显示加载状态
self.loading_label.configure(text="登录中...")
self.update()
# 模拟延迟(假装进行网络请求)
self.after(1000, self.verify_login, username, password)
def verify_login(self, username, password):
# 检查用户名和密码
if username == self.valid_username and password == self.valid_password:
messagebox.showinfo("登录成功", "欢迎进入系统!")
else:
messagebox.showerror("登录失败", "用户名或密码错误!")
self.loading_label.configure(text="") # 清除加载状态
self.entry_username.delete(0, ctk.END)
self.entry_password.delete(0, ctk.END)
# 启动应用
if __name__ == "__main__":
app = LoginApp()
app.mainloop()
nuitka 打包
nuitka --standalone --enable-plugin=tk-inter .\login.py