分享一段自己写的代码,可方便技术人员快速计算蒸汽的消耗量与蒸汽流速,快速匹配合适的蒸汽管径
import tkinter as tk
from tkinter import ttk, messagebox
import re # 导入正则表达式模块
import CoolProp.CoolProp as CP
import math
def is_valid_input(text):
"""验证输入是否为数字"""
return re.match(r'^[0-9]*\.?[0-9]*$', text) is not None
def get_steam_properties(pressure=None, temperature=None):
try:
if temperature is not None:
if pressure is not None:
# If both pressure and temperature are provided, we assume the user is querying for overheated steam
properties = {
"温度 (°C)": round(temperature, 4),
"压力 (Mpa)": round(pressure, 4),
"密度 (kg/m^3)": round(CP.PropsSI("D", "P", pressure * 1e6, "T", temperature + 273.15, "Water"), 4),
"比容 (m^3/kg)": round(1 / CP.PropsSI("D", "P", pressure * 1e6, "T", temperature + 273.15, "Water"), 4),
"比焓 (kJ/kg)": round(CP.PropsSI("H", "P", pressure * 1e6, "T", temperature + 273.15, "Water") / 1000, 4)
}
else:
# If only temperature is provided, we assume the user is querying for saturated steam
properties = {
"温度 (°C)": round(temperature, 4),
"压力 (Mpa)": "N/A",
"密度 (kg/m^3)": "N/A",
"比容 (m^3/kg)": "N/A",
"比焓 (kJ/kg)": round(CP.PropsSI("H", "T", temperature + 273.15, "Q", 1, "Water") / 1000, 4)
}
else:
temperature = CP.PropsSI("T", "P", pressure * 1e6, "Q", 1, "Water") - 273.15
density = CP.PropsSI("D", "P", pressure * 1e6, "Q", 1, "Water")
specific_volume = 1 / density
enthalpy = CP.PropsSI("H", "P", pressure * 1e6, "Q", 1, "Water") / 1000 # Convert to kJ/kg
properties = {
"温度 (°C)": round(temperature, 4),
"压力 (Mpa)": round(pressure, 4),
"密度 (kg/m^3)": round(density, 4),
"比容 (m^3/kg)": round(specific_volume, 4),
"比焓 (kJ/kg)": round(enthalpy, 4)
}
return properties
except Exception as e:
messagebox.showerror("错误", f"获取数据失败:{e}")
return None
def on_submit():
try:
pressure = float(pressure_entry.get())
steam_properties = get_steam_properties(pressure=pressure)
if steam_properties:
result_text.config(state="normal")
result_text.delete("1.0", tk.END)
for prop, value in steam_properties.items():
result_text.insert(tk.END, f"{prop}: {value}\n")
result_text.config(state="disabled")
except ValueError:
messagebox.showerror("错误", "请输入有效的压力值。")
def on_submit_overheated():
try:
pressure = float(pressure_entry.get())
temperature = float(temperature_entry.get())
steam_properties = get_steam_properties(pressure=pressure, temperature=temperature)
if steam_properties:
steam_properties["压力 (Mpa)"] = round(pressure, 4) # 将压力设置为用户输入的值
result_text.config(state="normal")
result_text.delete("1.0", tk.END)
for prop, value in steam_properties.items():
result_text.insert(tk.END, f"{prop}: {value}\n")
result_text.config(state="disabled")
except ValueError:
messagebox.showerror("错误", "请输入有效的数据。")
def calculate_steam_consumption():
try:
steam_enthalpy_text = result_text.get("1.0", tk.END).splitlines()[4] # 获取已查询到的蒸汽比焓行的文本
steam_enthalpy = float(steam_enthalpy_text.split(":")[1].strip()) # 提取比焓值并转换为浮点数
steam_heat = float(steam_heat_entry.get()) # 获取输入的蒸汽热量值(kW)
condensate_temp = float(condensate_temp_entry.get())
condensate_enthalpy = CP.PropsSI("H", "T", condensate_temp + 273.15, "Q", 0, "Water") / 1000 # 获取凝水比焓值(kJ/kg)
steam_consumption = steam_heat / ((steam_enthalpy - condensate_enthalpy) / 3600) # 计算蒸汽耗量(kg/h)
steam_consumption_label.config(text=f"蒸汽耗量: {steam_consumption:.2f} kg/h")
# 从结果文本框中获取密度值
density_text = result_text.get("1.0", tk.END).splitlines()[2] # 获取已查询到的密度行的文本
density = float(density_text.split(":")[1].strip()) # 提取密度值并转换为浮点数
# 如果输入了管径,则计算蒸汽流速
if diameter_entry.get():
diameter = float(diameter_entry.get()) # 获取输入的管径大小
area = math.pi * (diameter / 1000 / 2) ** 2 # 计算管道截面积,直径单位为毫米,需要转换为米
steam_volume_flow_rate = steam_consumption / density # 计算蒸汽体积流量(m³/h)
steam_velocity = steam_volume_flow_rate / area / 3600 # 计算蒸汽流速(m/s)
steam_velocity_label.config(text=f"蒸汽流速: {steam_velocity:.2f} m/s")
else:
steam_velocity_label.config(text="") # 如果未输入管径,则清空流速标签
except ValueError:
messagebox.showerror("错误", "请输入有效的数据。")
# 创建GUI
root = tk.Tk()
root.title("蒸汽物性与耗量查询器")
# 设置网格等宽
root.grid_columnconfigure(0, weight=1)
root.grid_rowconfigure(0, weight=1)
root.grid_rowconfigure(1, weight=1)
# 设置窗口的初始位置
screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()
window_width = 400
window_height = 250
x = (screen_width - window_width) // 2
y = (screen_height - window_height) // 2
root.geometry(f"{window_width}x{window_height}+{x}+{y}")
# 选择蒸汽类型界面
selection_frame = tk.Frame(root)
selection_frame.grid(row=0, column=0, sticky="nsew", padx=10, pady=10)
steam_type_var = tk.StringVar(value="饱和蒸汽")
steam_type_label = tk.Label(selection_frame, text="蒸汽类型:")
steam_type_label.grid(row=0, column=0, padx=5, pady=5)
steam_type_combobox = ttk.Combobox(selection_frame, textvariable=steam_type_var, values=["饱和蒸汽", "过热蒸汽"], width=10)
steam_type_combobox.grid(row=0, column=2, padx=5, pady=5)
# 物性查询界面
properties_frame = tk.Frame(root)
properties_frame.grid(row=1, column=0, sticky="nsew", padx=10, pady=10)
pressure_label = tk.Label(properties_frame, text="压力 (Mpa):")
pressure_label.grid(row=0, column=0, padx=5, pady=5, sticky="w") # 调整了压力输入框的位置
pressure_entry = tk.Entry(properties_frame, width=10)
pressure_entry.grid(row=0, column=1, padx=(0, 200), pady=5, sticky="w")
# 隐藏温度输入栏
temperature_label = tk.Label(properties_frame, text="温度 (°C):")
temperature_label.grid(row=1, column=0, padx=5, pady=5, sticky="w")
temperature_label.grid_remove()
temperature_entry = tk.Entry(properties_frame, width=10)
temperature_entry.grid(row=1, column=1, padx=(0, 200), pady=5, sticky="w")
temperature_entry.grid_remove()
# 根据选择隐藏蒸汽的温度栏
def on_steam_type_change(*args):
if steam_type_var.get() == "过热蒸汽":
temperature_label.grid(row=1, column=0, padx=5, pady=5, sticky="w")
temperature_entry.grid(row=1, column=1, padx=5, pady=5, sticky="w")
query_button.config(command=on_submit_overheated)
else:
temperature_label.grid_remove()
temperature_entry.grid_remove()
query_button.config(command=on_submit)
steam_type_var.trace_add("write", on_steam_type_change)
query_button = tk.Button(properties_frame, text="查询", command=on_submit)
query_button.grid(row=2, column=0, columnspan=2, padx=(0, 100), pady=5)
result_text_frame = tk.Frame(properties_frame, bd=1, relief="sunken")
result_text_frame.grid(row=3, column=0, columnspan=2, padx=5, pady=5)
result_text = tk.Text(result_text_frame, width=50, height=5)
result_text.pack(fill="both", expand=True)
# 质量流量计算界面
consumption_frame = tk.LabelFrame(root, text="蒸汽耗量计算器")
consumption_frame.grid(row=0, column=1, rowspan=2, sticky="nsew", padx=10, pady=10)
steam_heat_label = tk.Label(consumption_frame, text="蒸汽热量 (kW):")
steam_heat_label.grid(row=0, column=0, padx=5, pady=5)
steam_heat_entry = tk.Entry(consumption_frame, width=10)
steam_heat_entry.grid(row=0, column=1, padx=5, pady=5)
condensate_temp_label = tk.Label(consumption_frame, text="凝水温度 (°C):")
condensate_temp_label.grid(row=1, column=0, padx=5, pady=5)
condensate_temp_entry = tk.Entry(consumption_frame, width=10)
condensate_temp_entry.insert(0, "95") # 设置默认值为95°C
condensate_temp_entry.grid(row=1, column=1, padx=5, pady=5)
diameter_label = tk.Label(consumption_frame, text="管径 (mm):")
diameter_label.grid(row=2, column=0, padx=5, pady=5)
diameter_entry = tk.Entry(consumption_frame, width=10)
diameter_entry.grid(row=2, column=1, padx=5, pady=5)
calculate_button = tk.Button(consumption_frame, text="计算", command=calculate_steam_consumption)
calculate_button.grid(row=3, column=0, columnspan=2, padx=5, pady=5)
steam_consumption_label = tk.Label(consumption_frame, text="")
steam_consumption_label.grid(row=4, column=0, columnspan=2, padx=5, pady=5)
steam_velocity_label = tk.Label(consumption_frame, text="")
steam_velocity_label.grid(row=5, column=0, columnspan=2, padx=5, pady=5)
root.iconbitmap("C:\\Users\\PC3\\AppData\\Local\\Programs\\Python\\Python311\\Scripts\\9528.ico")
root.mainloop()
代码中的ICO文件可以自己定义,这样方便大家能够更换自己喜欢的ICO图片