在上文中我只给了界面的代码,注释了数据库部分的代码,本来想将数据可视化界面做好了再搞一个完整的登录和数据可视化界面的,但是由于有其它的事要做,所以就将之前的代码包装了一下,这样整段代码只包含三个函数(create_root,new_root,sql_information),比较清晰和有可重用性,与大家分享讨论:
from tkinter import *
import pymysql
# 创建根窗口,并添加组件
def create_root():
root = Tk()
root.title('登录')
root.resizable(0, 0) # 设置窗口大小不可变
canvas = Canvas(root) # 添加画布
canvas.pack(side='top', fill=BOTH)
canvas.create_window(100, 50, window=Label(root, font=('宋体', 10), text='用户名', justify='left', padx=5, pady=4)) # 其中100,50为相对于画布的偏移量,左上角为0,0
canvas.create_window(100, 90,
window=Label(root, font=('宋体', 10), width=5, text='密码 ', justify='left', padx=5, pady=4))
# 账号密码输入框
zh_entry = Entry(root, borderwidth=3)
password_entry = Entry(root, borderwidth=3, show='*')
canvas.create_window(210, 50, window=zh_entry)
canvas.create_window(210, 90, window=password_entry)
canvas.create_window(330, 90, window=Label(root, text='忘记密码', fg='red', font=('宋体', 10)))
# 创建画布背景图
photo = PhotoImage(file='bg.png')
canvas.create_image(200, 150, image=photo)
# button点击事件
def callback():
user = int(zh_entry.get())
password = int(password_entry.get())
user_information = sql_information('select * from user_information')
if user == user_information[0][0] and password == user_information[0][1]: # 从数据库中提取数据时,会以元组的形式返回每一行的数据,即每一行构成一个元组,并且所有的行构成一个大的元组,即嵌套元组。
student_information = sql_information('select * from student_information')
root.state("iconic") # 隐藏窗口,相当于窗口最小化
new_root(student_information)
else:
pass #这里账号密码错误的事件我没写,自己有需要的可以写相应的响应事件
# 创建登录按钮
canvas.create_window(190, 200, window=Button(root, width=15, command=callback, bg='#87CEEB', text='立即登录'))
mainloop()
# 创建新窗口
def new_root(student_information):
student_root = Toplevel()
student_root.title('学生管理系统')
student_root.resizable(0, 0)
head_string = ('学号', '姓名', '年级', '年龄', '家庭住址')
for i in range(len(student_information[0])):
listbox = Listbox(student_root, width=20, height=20, bd=4, relief='flat', bg='#E0FFFF')
listbox.pack(side=LEFT, fill=BOTH)
listbox.insert(END, head_string[i])
for each in student_information:
listbox.insert(END, each[i])
# 进行数据库连接,传入sql语句,返回需要的信息
def sql_information(sql):
connection = pymysql.connect('localhost', 'root', '*******', "tang_crawler") #这里我把密码覆盖了,自己连接时填上自己的密码就行了
cursor = connection.cursor()
try:
cursor.execute(sql)
user_information = cursor.fetchall()
except Exception as e:
print(e)
finally:
if connection:
cursor.close()
if cursor:
connection.close()
return user_information
if __name__ == '__main__':
create_root()
登录界面为:
点击登录后,数据界面为:
这里的数据可视化界面不好,一个是没找到一个好的UI设计图,还有就是tkinter本身感觉不适合做这种。所以只是简单将数据列了出来。