from tkinter import *
import webbrowser
import pickle
import os
import glob
import os.path
from tkinter.messagebox import showinfo
class Login:
def __init__(self):
# 首先进行相关文件处理
if os.path.exists('User_dict.pkl'):
file1 = open('User_dict.pkl', 'rb')
self.temp_dict = pickle.load(file1)
file1.close()
elif not os.path.exists('User_dict.pkl'):
self.temp_dict = {'username': 'password'}
file1 = open('User_dict.pkl', 'wb')
pickle.dump(self.temp_dict, file1)
file1.close()
self.judgeframe3 = 0
# 然后生成图形界面
self.root = Tk()
self.root.title('欢迎登录')
self.labelframe2 = LabelFrame(self.root, text="登录程序",fg="blue", padx=30, pady=30)
self.labelframe2.grid(row=0, column=1, padx=10, pady=10)
self.label2 = Label(self.labelframe2, text="请用户选择登录或者注册!", padx=5, pady=5)
self.label2.grid(row=0, column=1, padx=5, pady=5)
self.button1 = Button(self.labelframe2, text=' 账户注册 ', bg='#ccacb3',command=self.newAccount)
self.button1.grid(row=1, column=0)
self.button2 = Button(self.labelframe2, text=' 账户登录 ', bg='#94b5aa',command=self.hadAccount)
self.button2.grid(row=1, column=2)
mainloop()
# ---------------------------------新建账户后的行为----------------------------------------
def newAccount(self):
print('点击成功')
self.creatAccount()
def creatAccount(self):
if self.judgeframe3 == 1:
self.labelframe3.grid_forget()
self.judgeframe3 = 0
self.labelframe3 = LabelFrame(self.root, text="注册登录",fg="blue",padx=40, pady=50)
self.labelframe3.grid(row=2, column=1, padx=10, pady=10)
self.judgeframe3 = 1
self.label_username = Label(self.labelframe3, text='用户名:', padx=5, pady=5)
self.label_username.grid(row=2, column=1)
self.label_password1 = Label(self.labelframe3, text='请输入密码:', padx=5, pady=5)
self.label_password1.grid(row=3, column=1)
self.label_password2 = Label(self.labelframe3, text='请再次输入密码:', padx=5, pady=5)
self.label_password2.grid(row=4, column=1)
self.entry_username = Entry(self.labelframe3)
self.entry_username.grid(row=2, column=2, padx=10, pady=5)
self.entry_password1 = Entry(self.labelframe3, show='*')
self.entry_password1.grid(row=3, column=2, padx=10, pady=5)
self.entry_password2 = Entry(self.labelframe3, show='*')
self.entry_password2.grid(row=4, column=2, padx=10, pady=5)
self.regist_button = Button(self.labelframe3, text=' 立即注册 ', bg='#18778f',command=self.regist_new_Account)
self.regist_button.grid(row=5, column=2, padx=10, pady=5)
# 点击注册后的行为
def regist_new_Account(self):
self.user_name = self.entry_username.get()
self.password1 = self.entry_password1.get()
self.password2 = self.entry_password2.get()
if self.user_name in self.temp_dict:
self.label3 = Label(self.labelframe3, text="账号已存在", padx=5, pady=5, fg='red')
self.label3.grid(row=5, column=1, padx=5, pady=5)
else:
if self.password1 == self.password2:
self.password = self.password1
if self.password:
self.dict1 = {self.user_name: self.password}
self.temp_dict.update(self.dict1)
file1 = open('User_dict.pkl', 'wb')
pickle.dump(self.temp_dict, file1)
file1.close()
self.label3 = Label(self.labelframe3, text="注册成功!\n请返回登陆", padx=5, pady=5, fg='red')
self.label3.grid(row=5, column=1, padx=5, pady=5)
else:
self.label3 = Label(self.labelframe3, text="密码不能为空!", padx=5, pady=5, fg='red')
self.label3.grid(row=5, column=1, padx=5, pady=5)
else:
self.label3 = Label(self.labelframe3, text="前后两次密码输入不一致", padx=5, pady=5, fg='red')
self.label3.grid(row=5, column=1, padx=5, pady=5)
# ------------------------------------点击登陆后的行为--------------------------------------------------
def hadAccount(self):
print('成功点击登录')
if self.judgeframe3 == 1:
self.labelframe3.grid_forget()
self.judgeframe3 = 0
self.labelframe3 = LabelFrame(self.root, text="账号密码登录",fg="blue", padx=50, pady=50)
self.labelframe3.grid(row=2, column=1, padx=10, pady=10)
self.label_username0 = Label(self.labelframe3, text='用户名:', padx=5, pady=5)
self.label_username0.grid(row=2, column=1)
self.label_password0 = Label(self.labelframe3, text='请输入密码:', padx=5, pady=5)
self.label_password0.grid(row=3, column=1)
self.entry_username_0 = Entry(self.labelframe3)
self.entry_username_0.grid(row=2, column=2, padx=10, pady=5)
self.entry_password_0 = Entry(self.labelframe3, show='*')
self.entry_password_0.grid(row=3, column=2, padx=10, pady=5)
self.judgeframe3 = 1
self.regist_button = Button(self.labelframe3, text='立即登录',bg='#18778f', command=self.check_login)
self.regist_button.grid(row=5, column=2, padx=10, pady=5)
def check_login(self):
self.user_name = self.entry_username_0.get()
self.input_password = self.entry_password_0.get()
if self.user_name in self.temp_dict:
if self.temp_dict[self.user_name] == self.input_password:
showinfo(title='登录成功', message='欢迎进入')
win = Tk()
win.geometry('900x500')
win.title('测试')
path = os.getcwd()
def mouse_x_y(event):
tup = (event.x, event.y)
print(tup)
def event_1(text):
t1 = Toplevel()
t1.title(text)
t1.geometry('300x300')
bm1 = PhotoImage(file=path + '/临淄区.png/')
lab1 = Label(t1, image=bm1)
lab1.place(x=0, y=0)
t1.mainloop()
def event_2(text):
t2 = Toplevel()
t2.title(text)
t2.geometry('300x300')
bm2 = PhotoImage(file=path + '/周村区.png/')
lab2 = Label(t2, image=bm2)
lab2.place(x=0, y=0)
t2.mainloop()
def event_3(text):
t3 = Toplevel()
t3.title(text)
t3.geometry('300x300')
bm3 = PhotoImage(file=path + '/张店区.png/')
lab3 = Label(t3, image=bm3)
lab3.place(x=0, y=0)
t3.mainloop()
def event_4(text):
t4 = Toplevel()
t4.title(text)
t4.geometry('300x300')
bm4 = PhotoImage(file=path + '/淄川区.png/')
lab4 = Label(t4, image=bm4)
lab4.place(x=0, y=0)
t4.mainloop()
def event_5(text):
t5 = Toplevel()
t5.title(text)
t5.geometry('300x300')
bm5 = PhotoImage(file=path + '/博山区.png/')
lab5 = Label(t5, image=bm5)
lab5.place(x=0, y=0)
t5.mainloop()
def event_6(text):
t6 = Toplevel()
t6.title(text)
t6.geometry('300x300')
bm6 = PhotoImage(file=path + '/高青县.png/')
lab6 = Label(t6, image=bm6)
lab6.place(x=0, y=0)
t6.mainloop()
def event_7(text):
t7 = Toplevel()
t7.title(text)
t7.geometry('300x300')
bm7 = PhotoImage(file=path + '/恒台县.png/')
lab7 = Label(t7, image=bm7)
lab7.place(x=0, y=0)
t7.mainloop()
def event_8(text):
t8 = Toplevel()
t8.title(text)
t8.geometry('300x300')
bm8 = PhotoImage(file=path + '/沂源县.png/')
lab8 = Label(t8, image=bm8)
lab8.place(x=0, y=0)
t8.mainloop()
def mouse_click(event): # 按键区域判定
if 207 < event.x < 252 and 143 < event.y < 158:
event_1('临淄区')
if 85 < event.x < 130 and 182 < event.y < 197:
event_2('周村区')
if 135 < event.x < 180 and 170 < event.y < 185:
event_3('张店区')
if 126 < event.x < 171 and 249 < event.y < 264:
event_4('淄川区')
if 105 < event.x < 132 and 325 < event.y < 340:
event_5('博山区')
if 56 < event.x < 101 and 40 < event.y < 55:
event_6('高青县')
if 116 < event.x < 161 and 102 < event.y < 117:
event_7('恒台县')
if 174 < event.x < 229 and 415 < event.y < 430:
event_8('沂源县')
def main():
# 背景插入
bm = PhotoImage(file=path + '/背景.png/')
bj = Label(win, image=bm)
bj.place(x=0, y=0) # 背景显示
# 地图插入
bm0 = PhotoImage(file=path + '/淄博地图.png/')
images = Label(win, image=bm0)
images.place(x=300, y=0) # 地图显示
win.bind_all("<Button-1>", mouse_click) # Motion
# win.bind_all("<Button-1>",mouse_click2)
win.mainloop()
if __name__ == '__main__':
main()
else:
self.label3 = Label(self.labelframe3, text="密码错误!", padx=5, pady=5, fg='red')
self.label3.grid(row=5, column=1, padx=5, pady=5)
else:
self.label3 = Label(self.labelframe3, text="用户名不存在!", padx=5, pady=5, fg='red')
self.label3.grid(row=5, column=1, padx=5, pady=5)
login= Login()