You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

225 lines
7.0 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

import tkinter as tk
import pymysql
import matplotlib.pyplot as plt
con = pymysql.connect(
host='localhost',
user='root',
password='root',
database='health',
charset='utf8mb4',
autocommit=True
)
cursor = con.cursor() # 创建游标对象
def button1():
cursor.execute("select * from health")
result = cursor.fetchall()
# 创建一个新的窗口
result_window = tk.Toplevel(root)
result_window.title("查询结果")
# 创建一个文本框用于显示查询结果
result_text = tk.Text(result_window)
result_text.pack(pady=10)
# 将查询结果存储到一个变量中
result_str = ""
for row in result:
result_str += str(row) + "\n"
# 将查询结果显示在文本框中
result_text.insert(tk.END, result_str)
result_text.config(state=tk.DISABLED)
def button2():
root = tk.Tk()
root.title("new")
root.geometry("500x500")
screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()
l1 = tk.Label(root, text="姓名:", width=30, height=2)
l1.pack()
entry1 = tk.Entry(root)
entry1.pack()
l2 = tk.Label(root, text="性别:", width=30, height=2)
l2.pack()
entry2 = tk.Entry(root)
entry2.pack()
l3 = tk.Label(root, text="年龄:", width=30, height=2)
l3.pack()
entry3 = tk.Entry(root)
entry3.pack()
l4 = tk.Label(root, text="身高:", width=30, height=2)
l4.pack()
entry4 = tk.Entry(root)
entry4.pack()
l5 = tk.Label(root, text="体重:", width=30, height=2)
l5.pack()
entry5 = tk.Entry(root)
entry5.pack()
# 定义按钮的响应函数
def button():
# con = pymysql.connect(
# host='localhost',
# user='root',
# password='root',
# database='health',
# charset='utf8mb4',
# autocommit=True
# )
# cursor = con.cursor() # 创建游标对象
cursor.execute("select * from health")
result = cursor.fetchall()
l1.config(text="新建成功!")
name = entry1.get()
sex = entry2.get()
age = entry3.get()
height = entry4.get()
weight = entry5.get()
if not name or not age or not sex or not height or not weight:
l1.config(text='添加失败,请填写完整信息')
l1.after(2000, lambda: l1.config(text="姓名:")) # 2秒消失
return
try:
cursor.execute("INSERT INTO health (name,age,sex,height,weight) VALUES (%s, %s, %s, %s, %s)", (name,age,sex,height,weight))
con.commit()
l1.config(text='添加成功!')
except Exception as e:
l1.config(text='添加失败!')
# 创建按钮
button = tk.Button(root, text="确认新建", command=button)
button.pack()
def button3():
root = tk.Tk()
root.title("new")
root.geometry("500x500")
screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()
l1 = tk.Label(root, text="姓名:", width=30, height=2)
l1.pack()
entry1 = tk.Entry(root)
entry1.pack()
l2 = tk.Label(root, text="月份:", width=30, height=2)
l2.pack()
entry2 = tk.Entry(root)
entry2.pack()
l3 = tk.Label(root, text="身高:", width=30, height=2)
l3.pack()
entry3 = tk.Entry(root)
entry3.pack()
l4 = tk.Label(root, text="体重:", width=30, height=2)
l4.pack()
entry4 = tk.Entry(root)
entry4.pack()
# 定义按钮的响应函数
def button():
try:
# 从输入框获取数据
name = entry1.get()
month = entry2.get()
height = entry3.get()
weight = entry4.get()
# 检查是否所有输入框都已填写
if not all([name, month, height, weight]):
l1.config(text='添加失败,请填写完整信息')
# 2秒后重置文本
l1.after(2000, lambda: l1.config(text="姓名:"))
return
# 执行插入操作
cursor.execute("INSERT INTO month (name, month, height, weight) VALUES (%s, %s, %s, %s)",
(name, month, height, weight))
con.commit() # 提交事务
l1.config(text='添加成功!')
except Exception as e:
l1.config(text=f'添加失败:{e}')
button = tk.Button(root, text="确认新建", command=button)
button.pack()
def button4():
def show_query_result():
user_input = entry.get()
if user_input: # 确保用户输入不为空
# 根据用户输入查询 name
cursor.execute("SELECT name, height, weight FROM month WHERE name LIKE %s", ('%' + user_input + '%',))
result = cursor.fetchall() # 获取查询结果
# 清空结果标签的旧内容
result_label.config(text="")
if result:
# 此处 result 已包含 name, height, weight可以直接使用
身高 = [row[1] for row in result] # 假设 height 是第二列
体重 = [row[2] for row in result] # 假设 weight 是第三列
plt.scatter(身高, 体重)
plt.title("身高 和 体重", fontproperties="SimHei")
plt.xlabel('身高', fontproperties="SimHei")
plt.ylabel('体重', fontproperties="SimHei")
plt.show()
else:
result_label.config(text="没有找到匹配的数据。")
else:
result_label.config(text="请输入查询的名字。")
query_window = tk.Toplevel(root)
query_window.title("查询健康信息")
query_window.geometry("300x200")
l0 = tk.Label(query_window, text="请输入需要查询的用户:", width=30, height=2)
l0.pack()
entry = tk.Entry(query_window)
entry.pack()
get_input_button = tk.Button(query_window, text="确认查询", command=show_query_result)
get_input_button.pack()
result_label = tk.Label(query_window, text="")
result_label.pack()
def button5():
root.destroy()
#这是主界面
root = tk.Tk()
root.title("Health")
root.geometry("500x500")
screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()
x = (screen_width - 500) // 2
y = (screen_height - 500) // 2
root.geometry("+{}+{}".format(x, y))
button1 = tk.Button(root, text="用户列表", command=button1)
button1.pack(pady=10)
button2 = tk.Button(root, text="新建用户信息", command=button2)
button2.pack(pady=10)
button3 = tk.Button(root, text="新建用户健康指数", command=button3)
button3.pack(pady=10)
button4 = tk.Button(root, text="查询健康指数", command=button4)
button4.pack(pady=10)
button5 = tk.Button(root, text="退出程序", command=button5)
button5.pack(pady=10)
root.mainloop()