-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhealth_app.py
More file actions
118 lines (90 loc) · 4.17 KB
/
Copy pathhealth_app.py
File metadata and controls
118 lines (90 loc) · 4.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import tkinter as tk
from tkinter import ttk
from datetime import datetime
# 假資料產生器
import random
class HealthApp:
def __init__(self, root):
self.root = root
self.root.title("Health Application")
self.root.geometry("400x500")
self.create_widgets()
def create_widgets(self):
title = tk.Label(self.root, text="量測助手", font=("Arial", 24))
title.pack(pady=20)
self.hr_label = tk.Label(self.root, text="心率: -- bpm", font=("Arial", 18))
self.bp_label = tk.Label(self.root, text="血壓:-- / -- mmHg", font=("Arial", 18))
self.spo2_label = tk.Label(self.root, text="血氧:-- %", font=("Arial", 18))
self.hr_label.pack(pady=5)
self.bp_label.pack(pady=5)
self.spo2_label.pack(pady=5)
measure_btn = tk.Button(self.root, text="開始測量", font=("Arial", 16), command=self.measure)
measure_btn.pack(pady=20)
history_label = tk.Label(self.root, text="歷史紀錄", font=("Arial", 16))
history_label.pack()
self.history_box = tk.Text(self.root, height=10, width=40)
self.history_box.pack(pady=10)
def measure(self):
# 模擬測量數據
hr = random.randint(60, 100)
systolic = random.randint(110, 130)
diastolic = random.randint(70, 85)
spo2 = random.randint(95, 99)
self.hr_label.config(text=f"心率: {hr} bpm")
self.bp_label.config(text=f"血壓:{systolic} / {diastolic} mmHg")
self.spo2_label.config(text=f"血氧:{spo2} %")
now = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
record = f"[{now}] 心率: {hr} bpm, 血壓: {systolic}/{diastolic} mmHg, 血氧: {spo2} %\n"
self.history_box.insert(tk.END, record)
self.history_box.see(tk.END)
if __name__ == "__main__":
root = tk.Tk()
app = HealthApp(root)
root.mainloop()
# class HealthApp:
# def __init__(self, root):
# self.root = root
# self.root.title("Health App")
# self.create_widgets()
# def create_widgets(self):
# # Create a label for the title
# title_label = ttk.Label(self.root, text="Health Tracker", font=("Arial", 24))
# title_label.pack(pady=10)
# # Create a frame for input fields
# input_frame = ttk.Frame(self.root)
# input_frame.pack(pady=10)
# # Create input fields for health data
# self.weight_var = tk.DoubleVar()
# weight_label = ttk.Label(input_frame, text="Weight (kg):")
# weight_label.grid(row=0, column=0, padx=5, pady=5)
# weight_entry = ttk.Entry(input_frame, textvariable=self.weight_var)
# weight_entry.grid(row=0, column=1, padx=5, pady=5)
# self.height_var = tk.DoubleVar()
# height_label = ttk.Label(input_frame, text="Height (cm):")
# height_label.grid(row=1, column=0, padx=5, pady=5)
# height_entry = ttk.Entry(input_frame, textvariable=self.height_var)
# height_entry.grid(row=1, column=1, padx=5, pady=5)
# self.bmi_var = tk.StringVar()
# bmi_label = ttk.Label(input_frame, text="BMI:")
# bmi_label.grid(row=2, column=0, padx=5, pady=5)
# bmi_entry = ttk.Entry(input_frame, textvariable=self.bmi_var, state='readonly')
# bmi_entry.grid(row=2, column=1, padx=5, pady=5)
# # Create a button to calculate BMI
# calculate_button = ttk.Button(self.root, text="Calculate BMI", command=self.calculate_bmi)
# calculate_button.pack(pady=10)
# def calculate_bmi(self):
# try:
# weight = self.weight_var.get()
# height = self.height_var.get() / 100 # Convert cm to m
# bmi = weight / (height ** 2)
# self.bmi_var.set(f"{bmi:.2f}")
# self.log_health_data(weight, height * 100) # Log in kg and cm
# except Exception as e:
# self.bmi_var.set("Error")
# def log_health_data(self, weight, height):
# with open("health_log.txt", "a") as log_file:
# log_file.write(f"{datetime.now()}: Weight: {weight} kg, Height: {height} cm\n")
# if __name__ == "__main__":
# root = tk.Tk()
# app = HealthApp(root)
# root.mainloop()