|
发表于 2017-12-28 16:01:45
|
显示全部楼层
- #登录界面
- from tkinter import *
- from tkinter import messagebox
- import os
- import pymysql, pickle
- class LoginDemo():
- def __init__(self):
- #根窗口设置
- self.root = Tk()
- self.root.title('基于树莓派的环境管理系统')
- self.root.resizable(False, False)
- self.root.geometry('+500+250')
- #标题
- self.lb_toptitle = Label(self.root, text='树莓派环境监控平台', bg='blue', font=('楷体', 25))
- self.lb_toptitle.grid(row=0, column=0, padx=15, pady=2)
- #登录Frame
- self.lframe = LabelFrame(self.root)
- self.lframe.grid(row=1, column=0, padx=15, pady=2)
- #登录Frame内部布局
- self.lb_usr = Label(self.lframe, text='用户名:', font=('宋体', 15))
- self.lb_passwd = Label(self.lframe, text='密 码:', font=('宋体', 15))
- self.lb_usr.grid(row=0, column=0, padx=15, pady=2, sticky=E)
- self.lb_passwd.grid(row=1, column=0, padx=15, pady=2, sticky=E)
-
- self.en_usr_var = StringVar()
- self.en_passwd_var = StringVar()
- self.en_user = Entry(self.lframe, textvariable=self.en_usr_var)
- self.en_passwd = Entry(self.lframe, textvariable=self.en_passwd_var, show='*')
- self.en_user.grid(row=0, column=1, padx=15, pady=2)
- self.en_passwd.grid(row=1, column=1, padx=15, pady=2)
-
- with open("inforecord.txt", 'r') as self.f:
- self.showusrname = self.f.readline()
- self.showpssword = self.f.readline()
- self.ckb_status = self.f.readline()
- self.showusrname = self.showusrname.strip('\n')
- self.showpssword = self.showpssword.strip('\n')
- self.ckb_status = self.ckb_status.strip('\n')
- self.f.close()
- self.ckb_var = IntVar()
- self.ckb = Checkbutton(self.lframe, text='记住密码', underline=0,
- variable=self.ckb_var, command=self.func_ckb, font=('宋体', 15))
- self.ckb.grid(row=2, column=1, padx=15, pady=2, sticky=W)
- if self.ckb_status == '1':
- self.en_user.insert(INSERT, '%s' % self.showusrname)
- self.en_passwd.insert(INSERT, '%s' % self.showpssword)
- self.ckb.select()
-
- self.bt = Button(self.lframe, text='登录', bd=4, width=10, command=self.func_bt, font=('宋体', 20))
- self.bt.grid(row=3, column=0, padx=14, pady=2)
-
- self.bt = Button(self.lframe, text='密码提示', bd=4, width=10, command=self.func_prompt, font=('宋体', 20))
- self.bt.grid(row=3, column=1, padx=14, pady=2)
- self.root.mainloop()
-
- def func_ckb(self):
- if self.ckb_var.get():
- self.f = open("inforecord.txt", 'w+')
- self.f.truncate()
- self.f.writelines(self.en_usr_var.get() + '\n')
- self.f.writelines(self.en_passwd_var.get() + '\n')
- self.f.writelines(str(self.ckb_var.get()) + '\n')
- self.f.close()
-
- def func_bt(self):
- #连接用户数据数据库
- self.conn_ip = '192.168.1.104'
- self.conn_db = pymysql.connect(host = self.conn_ip, port = 3306, user = 'remotedata', passwd = '123', database='UserDatadb')
- #使用cursor()方法创建一个游标对象 cursor
- self.cursor = self.conn_db.cursor()
- self.sql = "SELECT * FROM userdata where username = '%s'" % self.en_usr_var.get()
- self.cursor.execute(self.sql)
- self.conn_db.commit()
- self.get_data = self.cursor.fetchone()
- if self.get_data:
- if self.en_passwd_var.get() == self.get_data[2]:
- title = '提示'
- message = '登录成功'
- messagebox.showinfo(title, message)
- self.conn_db.close()
- self.root.withdraw()
- os.system("python ControlDemo1.py")
- else:
- title = '警告'
- message = '用户名或密码错误'
- messagebox.showerror(title, message)
- else:
- title = '警告'
- message = '用户名不存在!'
- messagebox.showerror(title, message)
-
- def func_prompt(self):
- title = '提示'
- message = '就不提示,气死你!'
- messagebox.showinfo(title, message)
-
-
- if __name__ == "__main__":
- LoginDemo()
复制代码 |
|