鱼C论坛

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

判断问题

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

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

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

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()

在验证用户输入格式是否合法的时候有个小问题:
当第一个字符为汉字或者其他不合法的字符,不会进行判断,只有第二次输入字符的时候才给予出提示
捕1PNG.PNG
捕2.PNG
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-12-6 11:08:00 | 显示全部楼层
  1. import tkinter as tk
  2. import tkinter.messagebox as msg
  3. import pickle

  4. class Login:
  5.     urse_data = {}

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

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

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

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

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

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

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

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


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

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

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

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

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

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

  70.         return True


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

  73. root.mainloop()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-30 19:47

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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