文章目录
- 前言
- python-登录界面-demo
前言
如果您觉得有用的话,记得给博主点个赞,评论,收藏一键三连啊,写作不易啊^ _ ^。
而且听说点赞的人每天的运气都不会太差,实在白嫖的话,那欢迎常来啊!!!
python-登录界面-demo
示例:
demo源码:
import tkinter as tk
from tkinter import messagebox
# 定义按钮样式
button_style = {'bg': '#000000', # 背景色'fg': 'white', # 前景色(文字颜色)'font': ('Arial', 10), # 字体和字号'relief': 'raised', # 边框样式:raised表示凸起效果'borderwidth': 2, # 边框宽度'width': 7, # 按钮宽度'height': 1, # 按钮高度
}
# 定义按钮样式
show_button_style = {'bg': '#000000', # 背景色'fg': 'white', # 前景色(文字颜色)'font': ('Arial', 13), # 字体和字号'relief': 'raised', # 边框样式:raised表示凸起效果'borderwidth': 2, # 边框宽度'width': 15, # 按钮宽度'height': 1, # 按钮高度
}
# 创建主窗口
root = tk.Tk()
root.title("登录页面")
root.geometry("400x200")
root.configure(bg="#f0f0f0") # 设置背景颜色# 隐藏窗口
root.withdraw()
# 居中弹窗
root.update_idletasks()
x = (root.winfo_screenwidth() - root.winfo_width()) // 2
y = (root.winfo_screenheight() - root.winfo_height()) // 3
root.geometry(f"+{x}+{y}")
# 显示窗口
root.deiconify()
# 创建一个框架来放置用户名和密码
frame = tk.Frame(root)
frame.pack(pady=20)# 创建用户名标签和输入框
username_label = tk.Label(frame, text="用户名:")
username_label.grid(row=0, column=0, padx=5, pady=20)
username_entry = tk.Entry(frame)
username_entry.grid(row=0, column=1, padx=5, pady=5)# 创建密码标签和输入框
password_label = tk.Label(frame, text="密码:")
password_label.grid(row=1, column=0, padx=5, pady=5)
password_entry = tk.Entry(frame, show="*")
password_entry.grid(row=1, column=1, padx=5, pady=5)# 定义一个按钮点击事件处理函数
def on_button_click(action):if action == "shutdown":messagebox.showinfo("关机", "按钮被点击了!")elif action == "restart":messagebox.showinfo("重启", "按钮被点击了!")elif action == "cancel":messagebox.showinfo("撤销", "按钮被点击了!")# 定义显示成功登录窗口的函数
def show_success_window():success_window = tk.Toplevel(root)success_window.title("辅助工具")success_window.geometry("400x300")success_window.configure(bg="#f0f0f0") # 设置背景颜色# 隐藏窗口success_window.withdraw()# 居中弹窗success_window.update_idletasks()x = (success_window.winfo_screenwidth() - success_window.winfo_width()) // 2y = (success_window.winfo_screenheight() - success_window.winfo_height()) // 3success_window.geometry(f"+{x}+{y}")# 显示窗口success_window.deiconify()# 创建一个框架来放置按钮frame = tk.Frame(success_window, bg="#f0f0f0")frame.pack(padx=20, pady=30)# 创建按钮并放置在框架中shutdown_button = tk.Button(frame, text="关机", command=lambda: on_button_click("shutdown"), **show_button_style)shutdown_button.grid(row=0, column=0, sticky="ew", padx=10, pady=10)restart_button = tk.Button(frame, text="重启", command=lambda: on_button_click("restart"), **show_button_style)restart_button.grid(row=1, column=0, sticky="ew", padx=10, pady=10)open_address_cancel = tk.Button(frame, text="撤销", command=lambda: on_button_click("cancel"),**show_button_style)open_address_cancel.grid(row=2, column=0, sticky="ew", padx=10, pady=10)# 使按钮在水平方向上扩展以填充框架frame.grid_columnconfigure(0, weight=1)# 定义登录按钮点击事件处理函数
def login():username = username_entry.get()password = password_entry.get()if username == "yzy" and password == "yzy":show_success_window() # 显示成功登录窗口else:messagebox.showerror("错误", "用户名或密码错误!")# 创建登录按钮
login_button = tk.Button(root, text="登录", command=login, **button_style)
login_button.pack(pady=5)# 创建作者标签
author_label = tk.Label(root, text="author: 杨镇宇", bg="#66b3ff")
author_label.pack(side=tk.RIGHT, pady=0)
# 启动主事件循环
root.mainloop()