用代码实现以下功能:创建tkinter窗口,实现用户登陆功能(可以更改和写入帐户密码),登陆成功后将csv内数据以表格形式显示,并可以选择csv文件内的信息(如薪资,地区,经验)进行可视化图表处理,并将图表和表格分为两个不同的页面。
时间: 2024-02-24 11:54:45 浏览: 19
好的,以下是代码实现:
```python
import tkinter as tk
from tkinter import messagebox
import csv
import pandas as pd
import matplotlib.pyplot as plt
# 创建tkinter窗口
window = tk.Tk()
window.title("用户登陆")
window.geometry("300x200")
# 创建账户密码标签和输入框
tk.Label(window, text="账户:").place(x=50, y=30)
tk.Label(window, text="密码:").place(x=50, y=70)
var_user = tk.StringVar()
var_pwd = tk.StringVar()
entry_user = tk.Entry(window, textvariable=var_user)
entry_user.place(x=100, y=30)
entry_pwd = tk.Entry(window, textvariable=var_pwd, show="*")
entry_pwd.place(x=100, y=70)
# 登录验证函数
def login():
user = var_user.get()
pwd = var_pwd.get()
with open("accounts.csv", "r") as f:
reader = csv.reader(f)
for row in reader:
if row[0] == user and row[1] == pwd:
# 登录成功后打开数据文件并显示表格
df = pd.read_csv("data.csv")
df = df[["姓名", "薪资", "地区", "经验"]]
top = tk.Toplevel(window)
top.title("数据展示")
top.geometry("400x400")
table = tk.Text(top)
table.pack()
table.insert("end", df.to_string(index=False))
# 创建可视化图表并显示在另一个页面
def show_chart(option):
plt.clf()
if option == "薪资":
df["薪资"].plot(kind="bar")
elif option == "地区":
df["地区"].value_counts().plot(kind="pie")
elif option == "经验":
df["经验"].value_counts().plot(kind="line")
plt.title(option)
plt.show()
chart_page = tk.Frame(top)
chart_page.pack(side="bottom")
tk.Button(chart_page, text="薪资", command=lambda: show_chart("薪资")).pack(side="left")
tk.Button(chart_page, text="地区", command=lambda: show_chart("地区")).pack(side="left")
tk.Button(chart_page, text="经验", command=lambda: show_chart("经验")).pack(side="left")
return
messagebox.showerror(title="错误", message="账户或密码错误!")
# 创建登录和注册按钮
tk.Button(window, text="登录", command=login).place(x=100, y=120)
tk.Button(window, text="注册").place(x=170, y=120)
# 进入主循环
window.mainloop()
```
注:本代码示例使用了Pandas和Matplotlib库,需要事先安装。同时,账户密码和数据文件的名称和格式请根据具体需求自行调整。