jie1 commited on
Commit
8fcab71
1 Parent(s): c1085e0

Upload 2 files

Browse files
Files changed (2) hide show
  1. bg.jpg +0 -0
  2. login.py +147 -0
bg.jpg ADDED
login.py ADDED
@@ -0,0 +1,147 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import tkinter as tk
2
+ import tkinter.messagebox
3
+ import pickle
4
+ import os
5
+
6
+ from PIL import Image, ImageTk
7
+
8
+
9
+ class Login:
10
+ window = None
11
+ right = 0
12
+
13
+ # 登录函数
14
+ def usr_log_in(self):
15
+ # 输入框获取用户名密码
16
+ usr_name = self.var_usr_name.get()
17
+ usr_pwd = self.var_usr_pwd.get()
18
+ # 从本地字典获取用户信息,如果没有则新建本地数据库
19
+ try:
20
+ with open('usr_info.pickle', 'rb') as usr_file:
21
+ usrs_info = pickle.load(usr_file)
22
+ except FileNotFoundError:
23
+ with open('usr_info.pickle', 'wb') as usr_file:
24
+ usrs_info = {'admin': 'admin'}
25
+ pickle.dump(usrs_info, usr_file)
26
+ # 判断用户名和密码是否匹配
27
+ if usr_name in usrs_info:
28
+ if usr_pwd == usrs_info[usr_name]:
29
+ tk.messagebox.showinfo(title='welcome', message='欢迎您:' + usr_name)
30
+ self.window.destroy()
31
+ self.right = True
32
+ else:
33
+ tk.messagebox.showerror(message='密码错误')
34
+ # 用户名密码不能为空
35
+ elif usr_name == '' or usr_pwd == '':
36
+ tk.messagebox.showerror(message='用户名或密码为空')
37
+ # 不在数据库中弹出是否注册的框
38
+ else:
39
+ is_signup = tk.messagebox.askyesno('欢迎', '您还没有注册,是否现在注册')
40
+ if is_signup:
41
+ self.usr_sign_up()
42
+
43
+ # 注册函数
44
+ def usr_sign_up(self):
45
+ # 确认注册时的相应函数
46
+ def signtowcg():
47
+ # 获取输入框内的内容
48
+ nn = new_name.get()
49
+ np = new_pwd.get()
50
+ npf = new_pwd_confirm.get()
51
+
52
+ # 本地加载已有用户信息,如果没有则已有用户信息为空
53
+ try:
54
+ with open('usr_info.pickle', 'rb') as usr_file:
55
+ exist_usr_info = pickle.load(usr_file)
56
+ except FileNotFoundError:
57
+ exist_usr_info = {}
58
+
59
+ # 检查用户名存在、密码为空、密码前后不一致
60
+ if nn in exist_usr_info:
61
+ tk.messagebox.showerror('错误', '用户名已存在')
62
+ elif np == '' or nn == '':
63
+ tk.messagebox.showerror('错误', '用户名或密码为空')
64
+ elif np != npf:
65
+ tk.messagebox.showerror('错误', '密码前后不一致')
66
+ # 注册信息没有问题则将用户名密码写入数据库
67
+ else:
68
+ exist_usr_info[nn] = np
69
+ with open('usr_info.pickle', 'wb') as usr_file:
70
+ pickle.dump(exist_usr_info, usr_file)
71
+ tk.messagebox.showinfo('欢迎', '注册成功')
72
+ # 注册成功关闭注册框
73
+ window_sign_up.destroy()
74
+
75
+ # 新建注册界面
76
+ window_sign_up = tk.Toplevel(self.window)
77
+ window_sign_up.geometry('350x200')
78
+ window_sign_up.title('注册')
79
+ # 用户名变量及标签、输入框
80
+ new_name = tk.StringVar()
81
+ tk.Label(window_sign_up, text='用户名:').place(x=10, y=10)
82
+ tk.Entry(window_sign_up, textvariable=new_name).place(x=150, y=10)
83
+ # 密码变量及标签、输入框
84
+ new_pwd = tk.StringVar()
85
+ tk.Label(window_sign_up, text='请输入密码:').place(x=10, y=50)
86
+ tk.Entry(window_sign_up, textvariable=new_pwd, show='*').place(x=150, y=50)
87
+ # 重复密码变量及标签、输入框
88
+ new_pwd_confirm = tk.StringVar()
89
+ tk.Label(window_sign_up, text='请再次输入密码:').place(x=10, y=90)
90
+ tk.Entry(window_sign_up, textvariable=new_pwd_confirm, show='*').place(x=150, y=90)
91
+ # 确认注册按钮及位置
92
+ bt_confirm_sign_up = tk.Button(window_sign_up, text='确认注册',
93
+ command=signtowcg)
94
+ bt_confirm_sign_up.place(x=150, y=130)
95
+
96
+ # 退出的函数
97
+ def usr_sign_quit(self):
98
+ self.window.destroy()
99
+ self.right = False
100
+
101
+ def __init__(self):
102
+ # 窗口
103
+ self.window = tk.Tk()
104
+ self.window.title('花卉识别系统')
105
+ self.window.geometry('450x300')
106
+ self.topWidth = 450
107
+ self.topheight = 300
108
+
109
+ # 窗口居中
110
+ screenwidth = self.window.winfo_screenwidth()
111
+ screenheight = self.window.winfo_screenheight()
112
+ alignstr = '%dx%d+%d+%d' % (
113
+ self.topWidth, self.topheight, (screenwidth - self.topWidth) / 2, (screenheight - self.topheight) / 2)
114
+ self.window.geometry(alignstr)
115
+ ima = Image.open(os.path.dirname(os.path.realpath(__file__)) + '/bg.jpg').resize((500, 300))
116
+ ima = ImageTk.PhotoImage(ima)
117
+
118
+ # 画布放置图片
119
+ canvas = tk.Canvas(self.window, height=300, width=500, )
120
+ image = canvas.create_image(0, 0, anchor='nw', image=ima)
121
+ canvas.pack(side='top')
122
+
123
+ # 标签 用户名密码
124
+ tk.Label(self.window, text='用户名:').place(x=100, y=150)
125
+ tk.Label(self.window, text='密码:').place(x=100, y=190)
126
+
127
+ # 用户名输入框
128
+ self.var_usr_name = tk.StringVar()
129
+ self.entry_usr_name = tk.Entry(self.window, textvariable=self.var_usr_name)
130
+ self.entry_usr_name.place(x=160, y=150)
131
+ # self.entry_usr_name.place(x=140, y=230)
132
+
133
+ # 密码输入框
134
+ self.var_usr_pwd = tk.StringVar()
135
+ self.entry_usr_pwd = tk.Entry(self.window, textvariable=self.var_usr_pwd, show='*')
136
+ self.entry_usr_pwd.place(x=160, y=190)
137
+
138
+ # 登录 注册按钮
139
+ self.bt_login = tk.Button(self.window, text='登录', command=self.usr_log_in)
140
+ self.bt_login.place(x=140, y=230)
141
+ self.bt_logup = tk.Button(self.window, text='注册', command=self.usr_sign_up)
142
+ self.bt_logup.place(x=210, y=230)
143
+ self.bt_logquit = tk.Button(self.window, text='退出', command=self.usr_sign_quit)
144
+ self.bt_logquit.place(x=280, y=230)
145
+ # 主循环
146
+ self.window.mainloop()
147
+