Spaces:
Sleeping
Sleeping
Upload 细胞给药助手-GUI (1).py
Browse files- 细胞给药助手-GUI (1).py +125 -0
细胞给药助手-GUI (1).py
ADDED
|
@@ -0,0 +1,125 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import tkinter as tk
|
| 2 |
+
from tkinter import filedialog, messagebox, ttk
|
| 3 |
+
import csv
|
| 4 |
+
|
| 5 |
+
def calculate():
|
| 6 |
+
try:
|
| 7 |
+
total_vol = float(total_volume_entry.get())
|
| 8 |
+
mw = float(mw_entry.get())
|
| 9 |
+
ug_ml = float(ugml_entry.get())
|
| 10 |
+
mm_input = float(mm_entry.get())
|
| 11 |
+
|
| 12 |
+
# 单位换算
|
| 13 |
+
if mw > 0:
|
| 14 |
+
mmol_per_l = (ug_ml / 1000) / mw * 1000
|
| 15 |
+
mmol_label.config(text=f"≈ {mmol_per_l:.3f} mM")
|
| 16 |
+
ugml_back = mm_input * mw
|
| 17 |
+
ugml_label.config(text=f"≈ {ugml_back:.2f} µg/mL")
|
| 18 |
+
|
| 19 |
+
# 解析 stock_solutions
|
| 20 |
+
stock_lines = stock_text.get("1.0", "end").strip().split("\n")
|
| 21 |
+
stock_solutions = {}
|
| 22 |
+
for line in stock_lines:
|
| 23 |
+
try:
|
| 24 |
+
c, v = line.strip().split(":")
|
| 25 |
+
stock_solutions[float(c.strip())] = float(v.strip())
|
| 26 |
+
except:
|
| 27 |
+
messagebox.showwarning("格式错误", f"格式错误:{line}")
|
| 28 |
+
return
|
| 29 |
+
|
| 30 |
+
# 目标浓度
|
| 31 |
+
concs = [float(x.strip()) for x in conc_entry.get().split(",") if x.strip()]
|
| 32 |
+
results = []
|
| 33 |
+
|
| 34 |
+
for fc in concs:
|
| 35 |
+
best_stock = None
|
| 36 |
+
min_vol = float('inf')
|
| 37 |
+
selected_volume = 0
|
| 38 |
+
|
| 39 |
+
if fc == 0:
|
| 40 |
+
results.append([fc, "无", 0, total_vol, "对照组"])
|
| 41 |
+
continue
|
| 42 |
+
|
| 43 |
+
for sc, max_vol in stock_solutions.items():
|
| 44 |
+
if sc >= fc:
|
| 45 |
+
v1 = (fc * total_vol) / sc
|
| 46 |
+
if v1 <= max_vol and v1 < min_vol:
|
| 47 |
+
min_vol = v1
|
| 48 |
+
best_stock = sc
|
| 49 |
+
selected_volume = round(v1, 2)
|
| 50 |
+
|
| 51 |
+
if best_stock is None:
|
| 52 |
+
results.append([fc, "无法满足", "-", "-", "浓度过高或储备液不足"])
|
| 53 |
+
else:
|
| 54 |
+
media_vol = round(total_vol - selected_volume, 2)
|
| 55 |
+
results.append([fc, f"{best_stock} mM", selected_volume, media_vol, "✓"])
|
| 56 |
+
|
| 57 |
+
# 展示结果
|
| 58 |
+
for row in result_tree.get_children():
|
| 59 |
+
result_tree.delete(row)
|
| 60 |
+
for row in results:
|
| 61 |
+
result_tree.insert("", "end", values=row)
|
| 62 |
+
|
| 63 |
+
except Exception as e:
|
| 64 |
+
messagebox.showerror("错误", str(e))
|
| 65 |
+
|
| 66 |
+
def export_csv():
|
| 67 |
+
path = filedialog.asksaveasfilename(defaultextension=".csv", filetypes=[("CSV 文件", "*.csv")])
|
| 68 |
+
if not path:
|
| 69 |
+
return
|
| 70 |
+
with open(path, "w", newline='') as f:
|
| 71 |
+
writer = csv.writer(f)
|
| 72 |
+
writer.writerow(["终浓度 (mM)", "使用储备液", "加药体积 (µL)", "培养基体积 (µL)", "状态"])
|
| 73 |
+
for row_id in result_tree.get_children():
|
| 74 |
+
writer.writerow(result_tree.item(row_id)['values'])
|
| 75 |
+
|
| 76 |
+
root = tk.Tk()
|
| 77 |
+
root.title("🧪 自动配药计算工具 GUI 版")
|
| 78 |
+
|
| 79 |
+
tk.Label(root, text="每孔总体积 (µL)").grid(row=0, column=0)
|
| 80 |
+
total_volume_entry = tk.Entry(root)
|
| 81 |
+
total_volume_entry.insert(0, "2000")
|
| 82 |
+
total_volume_entry.grid(row=0, column=1)
|
| 83 |
+
|
| 84 |
+
tk.Label(root, text="分子量 (g/mol)").grid(row=1, column=0)
|
| 85 |
+
mw_entry = tk.Entry(root)
|
| 86 |
+
mw_entry.insert(0, "194.19")
|
| 87 |
+
mw_entry.grid(row=1, column=1)
|
| 88 |
+
|
| 89 |
+
tk.Label(root, text="µg/mL 浓度").grid(row=2, column=0)
|
| 90 |
+
ugml_entry = tk.Entry(root)
|
| 91 |
+
ugml_entry.insert(0, "0.0")
|
| 92 |
+
ugml_entry.grid(row=2, column=1)
|
| 93 |
+
|
| 94 |
+
mmol_label = tk.Label(root, text="≈ 0.000 mM")
|
| 95 |
+
mmol_label.grid(row=2, column=2)
|
| 96 |
+
|
| 97 |
+
tk.Label(root, text="mM 浓度").grid(row=3, column=0)
|
| 98 |
+
mm_entry = tk.Entry(root)
|
| 99 |
+
mm_entry.insert(0, "0.0")
|
| 100 |
+
mm_entry.grid(row=3, column=1)
|
| 101 |
+
|
| 102 |
+
ugml_label = tk.Label(root, text="≈ 0.00 µg/mL")
|
| 103 |
+
ugml_label.grid(row=3, column=2)
|
| 104 |
+
|
| 105 |
+
tk.Label(root, text="储备液设置 (mM:µL)").grid(row=4, column=0)
|
| 106 |
+
stock_text = tk.Text(root, height=4, width=30)
|
| 107 |
+
stock_text.insert("1.0", "2: 1000\n5: 1000\n20: 1000")
|
| 108 |
+
stock_text.grid(row=4, column=1, columnspan=2)
|
| 109 |
+
|
| 110 |
+
tk.Label(root, text="目标浓度列表 (mM, 逗号分隔)").grid(row=5, column=0)
|
| 111 |
+
conc_entry = tk.Entry(root, width=30)
|
| 112 |
+
conc_entry.insert(0, "0,0.5,1,2,5,10")
|
| 113 |
+
conc_entry.grid(row=5, column=1, columnspan=2)
|
| 114 |
+
|
| 115 |
+
tk.Button(root, text="计算", command=calculate).grid(row=6, column=1)
|
| 116 |
+
tk.Button(root, text="导出 CSV", command=export_csv).grid(row=6, column=2)
|
| 117 |
+
|
| 118 |
+
cols = ["终浓度 (mM)", "使用储备液", "加药体积 (µL)", "培养基体积 (µL)", "状态"]
|
| 119 |
+
result_tree = ttk.Treeview(root, columns=cols, show="headings", height=8)
|
| 120 |
+
for col in cols:
|
| 121 |
+
result_tree.heading(col, text=col)
|
| 122 |
+
result_tree.column(col, width=120)
|
| 123 |
+
result_tree.grid(row=7, column=0, columnspan=3, pady=10)
|
| 124 |
+
|
| 125 |
+
root.mainloop()
|