Python 利用StringVar接收用户输入, 制作用户名和密码的验证登录窗口

Python3 使用tkinter制作了一个简单的验证登陆窗口
messagebox为弹窗提醒


import tkinter as tk
from tkinter import messagebox

class verification_window(tk.Frame):

    # 调用时初始化
    def __init__(self):
        global root
        root = tk.Tk()
        # 窗口大小设置为150x150
        root.geometry('150x150+885+465')

        root.resizable(0, 0)  # 窗口大小固定

        super().__init__()
        self.username = tk.StringVar()
        self.password = tk.StringVar()
        self.pack()
        self.main_window()

        root.mainloop()

    # 窗口布局
    def main_window(self):
        global root
        username_label=tk.Label(root,text='Username:',font=('Arial',12)).place(x=35,y=10)
        username_input = tk.StringVar
        username_entry=tk.Entry(root,textvariable=self.username).place(x=2,y=35)

        password_label=tk.Label(root,text='Password:',font=('Arial',12)).place(x=35,y=58)
        password_input = tk.StringVar
        password_entry=tk.Entry(root,textvariable=self.password,show='*').place(x=2,y=83)

        # 在按下CONFIRM按钮时调用验证函数
        conformation_button = tk.Button(root,text='CONFIRM',command=self.verification,fg='white',bg='black', activeforeground='white', activebackground='navy',width=8,height=1)
        conformation_button.place(x=6,y=112)

        quit_button = tk.Button(root, text='QUIT', command=root.quit, fg='white', bg='black', activeforeground='white', activebackground='red', width=8, height=1)
        quit_button.place(x=78,y=112)

    # 验证函数
    def verification(self):
        global root

        # 检查用户名和密码 是否在user_dict字典中
        user_dict = {987654321:112233,123456789:332211}
        if user_dict.get(int(self.username.get())) == int(self.password.get()):
            # 成功提醒
            messagebox.showinfo(title='Correct', message=f'{int(self.username.get())}, welcome!')  

            # 验证成功后打开main_gui窗口
            #root.withdraw()
            #from gui import main_gui
            #main_gui.app()

        else:
            messagebox.showerror(title='Wrong inputs!', message='Please enter correct username or password.')  # 错误提醒

if __name__ == '__main__':
    verification_window()

效果图
主界面

错误提示

验证成功

  • 14
    点赞
  • 95
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值