对登录验证功能进行GUI设计

Python的GUI设计简单功能实践

根据要用的功能进行导包操作

from tkinter import ttk
import tkinter as tk
import tkinter.messagebox
import random
import json
import time
import os

好了,开始设计窗口属性与变化

window=tk.Tk()
#设置窗口属性
window.title("登陆验证")
window.iconbitmap("images\\title.ico")
window.geometry("500x400+500+200")

canvas=tk.Canvas(window,width="400",height="150",bg="green")
#引入图片
images_file=tk.PhotoImage(file="images\\head.gif")
images = canvas.create_image(200, 0, anchor='n', image=images_file)
canvas.pack(side="top")
#欢迎介绍
tk.Label(window, text='WELCOME',font=('Arial', 16)).pack()
#登陆验证
tk.Label(window, text='Username:', font=('Arial', 14)).place(x=10, y=195)
tk.Label(window, text='Password:', font=('Arial', 14)).place(x=10, y=235)
#用户登陆输入框
# 用户名
entry_name = tk.Entry(window,width="30",font=('Arial', 12))
entry_name.place(x=120,y=195)
# 用户密码
entry_pwd = tk.Entry(window,width="30",font=('Arial', 12), show='*')
entry_pwd.place(x=120,y=235)

# login and sign up 按钮
btn_login = tk.Button(window, text='Login', command=login)
btn_login.place(x=150, y=280)
btn_sign_up = tk.Button(window, text='Sign up', command=sign_up)
btn_sign_up.place(x=220, y=280

#循环显示窗口
window.mainloop()

这大致的框架就这样做好了,现在考虑按钮的实现方法了,登录就对文件进行操作,这里的文件写入写出可以不用json用pickle,但是我要修改数据就用pickle了,我们可以在未注册直接登录的时候预先加入易得admin账号密码,还要加上各种判断。对于注册就要个弹窗直接获取写入就行了

def sign_up():
    def sign_to():
        #获取注册窗口的注册信息
        new_name=entry_new_name.get()
        new_pwd=entry_pwd.get()
        new_pwd_confirm=entry_pwd_confirm.get()

	with open('info', 'r') as fd1:
            usr_info = json.load(fd1)
        if new_pwd != new_pwd_confirm:
            tkinter.messagebox.showerror('Error', '密码不一致,请重新输入')
        # 如果用户名已经在我们的数据文件中,则提示Error, The user has already signed up!
        elif new_name in usr_info:
            tkinter.messagebox.showerror('Error', '该用户已经存在了')
        else:
            #没有谱之后可以写入文件
            usr_info[new_name] = [new_pwd,1000]
            with open('info', 'w') as fd1:
                json.dump(usr_info, fd1)
            tkinter.messagebox.showinfo('Welcome', '你已经注册成功了')
            # 然后销毁窗口。
            window_sign_up.destroy()
    # 定义长在窗口上的窗口
    window_sign_up = tk.Toplevel(window)
    window_sign_up.geometry('300x200+580+250')
    window_sign_up.title('注册')
    
    tk.Label(window_sign_up, text='Username: ').place(x=10, y=10)  # 将`User name:`放置在坐标(10,10)。
    entry_new_name = tk.Entry(window_sign_up)  
    entry_new_name.place(x=130, y=10)  # `entry`放置在坐标(130,10).
    tk.Label(window_sign_up, text='Password: ').place(x=10, y=50)
    entry_pwd = tk.Entry(window_sign_up,show='*')
    entry_pwd.place(x=130, y=50)
    tk.Label(window_sign_up, text='Confirm password: ').place(x=10, y=90)
    entry_pwd_confirm = tk.Entry(window_sign_up,show='*')
    entry_pwd_confirm.place(x=130, y=90)
    # 注册按钮
    btn_comfirm_sign_up = tk.Button(window_sign_up, text='Sign up', command=sign_to)
    btn_comfirm_sign_up.place(x=180, y=120)

现后可以进行登录,另外加两个游戏猜猜猜

#登陆注册的方法
def login():
    # 这两行代码就是获取用户输入的usr_name和usr_pwd
    usr_name = entry_name.get()
    usr_pwd = entry_pwd.get()

    try:
        with open('info', 'r') as f:
            usrs_info = json.load(f)
    except FileNotFoundError:
        # 这里就是我们在没有读取到``的时候,程序会创建一个``这个文件,并将管理员
        # 的用户和密码写入,即用户名为`admin`密码为`admin`。
        with open('info', 'w') as f:
            usrs_info = {"admin": ["admin",1000]}
            json.dump(usrs_info, f)
            f.close()  # 必须先关闭,否则json.load()会出现EOFError: Ran out of input

    def finger_guess():
        # 定义长在窗口上的窗口游戏
        window_sign_up_g1 = tk.Toplevel(window)
        window_sign_up_g1.geometry('350x200+580+250')
        window_sign_up_g1.title('猜拳')
        #定义随时变量
        usrs_info_change=1000

        tk.Label(window_sign_up_g1,text='输出信息', font=('Arial', 10)).place(x=120, y=5)
        g1_message_1 = tk.StringVar()
        g1_message_1.set('输入赌注,开始你的出招')
        g1_0 = tk.Entry(window_sign_up_g1,textvariable=g1_message_1,width=45)
        g1_0.place(x=20, y=30)
        tk.Label(window_sign_up_g1, text='赌注:', font=('Arial', 10)).place(x=20, y=90)
        g1_1 = tk.Entry(window_sign_up_g1,width="20")
        g1_1.place(x=50, y=90)

        def button_click(value,money):
            ls = ["石头", "剪刀", "布"]
            computer = random.choice(ls)
            try:
                stake= int(g1_1.get())
                if stake > money:
                    tkinter.messagebox.showerror('Error', '你没有那么多钱!')
                    return
                if ls[value] == computer:
                    g1_message_1.set("你和电脑出的都是{}".format(computer))
                elif ls[value] == "石头":
                    if computer == "剪刀":
                        money += stake
                        g1_message_1.set("你出的是石头,电脑出的剪刀,你赢了!金币:%s" % (money))
                    else:
                        money -= stake
                        g1_message_1.set("你出的是石头,电脑出的布,你输了!金币:%s" % (money))
                        if money <= 0:
                            g1_message_1.set("你已经输光了,充点钱再来吧")
                            time.sleep(2)
                            window_sign_up_g1.destroy()
                elif ls[value] == "剪刀":
                    if computer == "石头":
                        money -= stake
                        g1_message_1.set("你出的是剪刀,电脑出的石头,你输了!金币:%s" % (money))
                        if money <= 0:
                            g1_message_1.set("你已经输光了,充点钱再来吧")
                            time.sleep(2)
                            window_sign_up_g1.destroy()
                    else:
                        money += stake
                        g1_message_1.set("你出的是剪刀,电脑出的布,你赢了!金币:%s" % (money))
                elif ls[value] == "布":
                    if computer == "石头":
                        money += stake
                        g1_message_1.set("你出的是布,电脑出的石头,你赢了!金币:%s" % (money))
                    else:
                        money -= stake
                        g1_message_1.set("你出的是布,电脑出的剪刀,你输了!金币:%s" % (money))
                        if money <= 0:
                            g1_message_1.set("你已经输光了,充点钱再来吧")
                            time.sleep(2)
                            window_sign_up_g1.destroy()
            except:
                g1_message_1.set("未知的错误!")
            #进行一次保留一次
            with open("info.tap", mode="w", encoding="utf-8") as f2:
                usrs_info_change=money
                usrs_info[usr_name][1]=usrs_info_change
                json.dump(usrs_info,f2)
            os.remove("info")
            os.rename("info.tap", "info"

        btn_1 = tk.Button(window_sign_up_g1,text="石头",command=lambda : button_click(value=0,money=usrs_info[usr_name][1]))
        btn_2 = tk.Button(window_sign_up_g1,text="剪子",command=lambda : button_click(value=1,money=usrs_info[usr_name][1]))
        btn_3 = tk.Button(window_sign_up_g1,text="布",command=lambda : button_click(value=2,money=usrs_info[usr_name][1]))
        btn_1.place(x=50,y=140)
        btn_2.place(x=150, y=140)
        btn_3.place(x=240, y=140)


    def digit_guess():
        # 定义长在窗口上的窗口游戏
        window_sign_up_g2 = tk.Toplevel(window)
        window_sign_up_g2.geometry('350x220+580+250')
        window_sign_up_g2.title('猜数字')
        # 定义随时变量
        usrs_info_change_d = 1000

        tk.Label(window_sign_up_g2, text='输出信息', font=('Arial', 10)).place(x=120, y=5)
        g2_message_2 = tk.StringVar()
        g2_message_2.set('输入赌注,开始你的出招')
        g2_0 = tk.Entry(window_sign_up_g2, textvariable=g2_message_2, width=45)
        g2_0.place(x=20, y=30)
        tk.Label(window_sign_up_g2, text='赌注:', font=('Arial', 10)).place(x=20, y=90)
        g2_2 = tk.Entry(window_sign_up_g2, width="20")
        g2_2.place(x=50, y=90)

        def button_click_d(value_d,money_d):
            computer_d=random.randint(1,10)
            try:
                stake_d = int(g2_2.get())
                if stake_d > money_d:
                    tkinter.messagebox.showerror('Error', '你没有那么多钱!')
                    return
                if int(value_d)==computer_d:
                    g2_message_2.set("哈哈,你真厉害,你猜对了,就是{},金币+{},还有{}".format(computer_d,stake_d*9,money_d))
                    money_d+=(stake_d*9)
                else:
                    g2_message_2.set("真可惜,你猜错了,真正的是{},金币-{},还有{}".format(computer_d,stake_d,money_d))
                    money_d-=stake_d
                    if money_d<=0:
                        g2_message_2.set("你已经输光了,充点钱再来吧")
                        time.sleep(2)
                        window_sign_up_g2.destroy()
            except:
                g2_message_2.set("未知的错误!")
                #进行一次保留一次
            with open("info.tap", mode="w", encoding="utf-8") as f2_d:
                usrs_info_change_d=money_d
                usrs_info[usr_name][1]=usrs_info_change_d
                json.dump(usrs_info,f2_d)
            os.remove("info")
            os.rename("info.tap", "info")

        btn_1_d = tk.Button(window_sign_up_g2, text="1",command=lambda: button_click_d(value_d=0, money_d=usrs_info[usr_name][1]))
        btn_2_d = tk.Button(window_sign_up_g2, text="2",command=lambda: button_click_d(value_d=1, money_d=usrs_info[usr_name][1]))
        btn_3_d = tk.Button(window_sign_up_g2, text="3",command=lambda: button_click_d(value_d=2, money_d=usrs_info[usr_name][1]))
        btn_4_d = tk.Button(window_sign_up_g2, text="4",command=lambda: button_click_d(value_d=3, money_d=usrs_info[usr_name][1]))
        btn_5_d = tk.Button(window_sign_up_g2, text="5",command=lambda: button_click_d(value_d=4, money_d=usrs_info[usr_name][1]))
        btn_6_d = tk.Button(window_sign_up_g2, text="6",command=lambda: button_click_d(value_d=5, money_d=usrs_info[usr_name][1]))
        btn_7_d = tk.Button(window_sign_up_g2, text="7",command=lambda: button_click_d(value_d=6, money_d=usrs_info[usr_name][1]))
        btn_8_d = tk.Button(window_sign_up_g2, text="8",command=lambda: button_click_d(value_d=7, money_d=usrs_info[usr_name][1]))
        btn_9_d = tk.Button(window_sign_up_g2, text="9", command=lambda: button_click_d(value_d=8, money_d=usrs_info[usr_name][1]))
        btn_10_d = tk.Button(window_sign_up_g2, text="10",command=lambda: button_click_d(value_d=9, money_d=usrs_info[usr_name][1]))
        btn_1_d.place(x=30, y=140)
        btn_2_d.place(x=70, y=140)
        btn_3_d.place(x=110, y=140)
        btn_4_d.place(x=150, y=140)
        btn_5_d.place(x=190, y=140)
        btn_6_d.place(x=30, y=170)
        btn_7_d.place(x=70, y=170)
        btn_8_d.place(x=110, y=170)
        btn_9_d.place(x=150, y=170)
        btn_10_d.place(x=190, y=170)

    # 如果用户名和密码与文件中的匹配成功,则会登录成功。
    if usr_name in usrs_info:
        if usr_pwd == usrs_info[usr_name][0]:
            window_sign_up = tk.Toplevel(window)
            window_sign_up.geometry('500x400+500+200')
            window_sign_up.iconbitmap("images\\title.ico")
            window_sign_up.title('开始游戏')
            
            frame=ttk.Notebook(window_sign_up)
            f1=ttk.Frame(frame,height=200,width=200)
            tk.Label(f1, text='''简介内容,懒得写''').pack()
            f2=ttk.Frame(frame, height=200, width=200)
            tk.Label(f2, text='''简介内容,蓝底鞋''').pack()
            frame.add(f1,text="猜拳")
            frame.add(f2,text="猜数字")
            frame.pack()

            btn_guess_digit=tk.Button(window_sign_up,text="猜数字",command=digit_guess)
            btn_guess_finger=tk.Button(window_sign_up, text="猜拳", command=finger_guess)
            btn_guess_digit.place(x=250,y=300)
            btn_guess_finger.place(x=150,y=300)

        # 如果用户名匹配成功,而密码输入错误,则会弹出
        else:
            tkinter.messagebox.showerror('Error', '你的密码不正确请重新输入')
    else:  # 如果发现用户名不存在
        is_sign_up = tkinter.messagebox.askyesno('Welcome', '你还没有注册呢,是否注册')
        # 提示需不需要注册新用户
        if is_sign_up:
            sign_up()

完成,基本功能都实现了,文件里的字典值存的列表,有密码和分数,其实可以在优化,背景图和图标可以找别的图片进行代替背景图
图标:.ico格式

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值