鱼C论坛

 找回密码
 立即注册
查看: 1243|回复: 1

[已解决]判断问题

[复制链接]
发表于 2020-12-5 20:40:52 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
import tkinter as tk
import tkinter.messagebox as msg
import pickle

class Login:
    urse_data = {}
    try:
        with open('D:\\python库\\urse_data.pkl', 'rb') as data:
            data = pickle.load(data)
            urse_data = data
    except:
        with open('D:\\python库\\urse_data.pkl', 'wb') as data:
            pickle.dump(urse_data,data)

    def __init__(self,root):
        self.main_window = root
        self.main_window.title('welcoe to Login window')
        self.main_window.geometry('450x300')

        self.main_frame = tk.Frame(self.main_window)
        self.main_frame.pack(padx=10,pady=10)
        self.img = tk.PhotoImage(file='C:\\Users\\Admin\\Desktop\welcome.gif')
        imglabel = tk.Label(self.main_frame, image=self.img)
        imglabel.grid(row=1, column=2)

        tk.Button(text='log on',command=self.log_on, width=6, height=2).place(x=100,y=230)    # 登录组件
        tk.Button(text='register', command=self.register,width=6, height=2).place(x=260, y=230) # 注册组件

        tk.Label(self.main_window, text='ID',font=('微软雅黑', 13)).place(x=80, y=140)  # ID 标签
        tk.Label(self.main_window,text='Password', font=('微软雅黑', 13)).place(x=80, y=170) # 密码标签

        self.id_ = tk.Entry(self.main_window)  # id 输入框
        self.password = tk.Entry(self.main_window)  # 密码输入框
        self.id_.place(x=170, y=140)
        self.password.place(x=170, y=170)

    def log_on(self):
        if self.id_.get() == '':
            error_label = tk.Label(self.main_window, text='请输入你的账号后登录', font=('微软雅黑字体', 10), fg='red')
        else:
            if self.password.get() == '':
                error_label = tk.Label(self.main_window, text='请输入你的密码后登录', font=('微软雅黑字体', 10), fg='red')
                error_label.place(x=170, y=190)
            else:
                try:
                    if self.password.get() == self.urse_data[self.password.get()]:
                        msg.showwarning(title='提示', message='登录成功')
                    else:
                        error_label = tk.Label(self.main_window, text='账号或密码错误', font=('微软雅黑字体', 10), fg='red')

                except KeyError:
                    error_label = tk.Label(self.main_window, text='账号或密码错误', font=('微软雅黑字体', 10),fg='red')

                finally:
                    error_label.place(x=170, y=190)
        error_label.place(x=170, y=190)


    def register(self):
        self.top_window =  tk.Toplevel()
        self.top_window.title('欢迎注册')
        self.top_window.geometry('560x300')
        global img
        img = tk.PhotoImage(file=r'C:\Users\Admin\Desktop\timg.gif')
        tk.Label(self.top_window, image=img).pack(padx=80, pady=10, anchor='w')   # 图片标签
        tk.Label(self.top_window, text='New ID', font=('微软雅黑', 13)).place(x=20, y=125)  # 新 id 标签
        tk.Label(self.top_window, text='New Password', font=('微软雅黑', 13)).place(x=20, y=155)  # 新 密码标签
        tk.Label(self.top_window, text='Confirm pass', font=('微软雅黑', 13)).place(x=20,y=185)

        self.new_id = tk.Entry(self.top_window,validate='key',validatecommand=self.cheking)  # id 输入框
        self.new_id.place(x=150, y=125)

        self.new_password = tk.Entry(self.top_window)   # 密码输入框
        self.new_password.place(x=150 , y=155)

        self.confirm_password = tk.Entry(self.top_window)  # 确认密码输入框
        self.confirm_password.place(x=150, y=187)

        self.msg_value = tk.StringVar()   # 提示注册格式合法性变量
        tk.Label(self.top_window, textvariable=self.msg_value).place(x=300, y=125)  # 提示标签

    def cheking(self): # 验证函数
        number = '1234567890'
        letter = 'QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm'
        symbol = "*-+.?/.>,<;':\"[]{}|\+=_-)(*&^%$#@!~`"
        self.msg_value.set('')
        for i in self.new_id.get():
            if i in letter or i in symbol or i in number:
                continue
            else:
                self.msg_value.set('ID不合法,ID只能含有字母数组英文字符')

        return True


root = tk.Tk()
longin = Login(root)

root.mainloop()

在验证用户输入格式是否合法的时候有个小问题:
当第一个字符为汉字或者其他不合法的字符,不会进行判断,只有第二次输入字符的时候才给予出提示
最佳答案
2020-12-5 22:14:56
======你输入任意字符(例如按键'w')的时候,键盘按键事件发生,输入框立即获得事件调用函数,此时你输入的内容还没反馈到输入框内
我猜测的过程如下:按下'w'键→弹起'w'键→输入框获得按键信息,立马调用checking函数→输入框获得字符w
因此你第一次不管按什么键,你的self.new_id.get()都是一个空的字符串,当然不会提示错误了
=========解决办法
self.new_id = tk.Entry(self.top_window)  # id 输入框
self.new_id.bind('<KeyRelease>',self.cheking)  # 事件绑定为放开键盘按键

def cheking(self,event): # 验证函数

改下绑定方式就可以解决了。。。
捕1PNG.PNG
捕2.PNG
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2020-12-5 22:14:56 | 显示全部楼层    本楼为最佳答案   
======你输入任意字符(例如按键'w')的时候,键盘按键事件发生,输入框立即获得事件调用函数,此时你输入的内容还没反馈到输入框内
我猜测的过程如下:按下'w'键→弹起'w'键→输入框获得按键信息,立马调用checking函数→输入框获得字符w
因此你第一次不管按什么键,你的self.new_id.get()都是一个空的字符串,当然不会提示错误了
=========解决办法
self.new_id = tk.Entry(self.top_window)  # id 输入框
self.new_id.bind('<KeyRelease>',self.cheking)  # 事件绑定为放开键盘按键

def cheking(self,event): # 验证函数

改下绑定方式就可以解决了。。。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-18 15:00

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表