import tkinter as tk from tkinter import messagebox import matplotlib.pyplot as plt

保存气体种类和按钮颜色信息

gas_info = [ {"name": "绿", "color": "green", "num": 2}, {"name": "蓝", "color": "blue", "num": 3}, {"name": "黄", "color": "yellow", "num": 4}, {"name": "红", "color": "red", "num": 5} ]

def create_gas_window(gas): gas_window = tk.Toplevel() gas_window.title(f"{gas['name']}气体输入")

pressure_label = tk.Label(gas_window, text="恒定气压:")
pressure_label.pack()

pressure_entry = tk.Entry(gas_window)
pressure_entry.pack()

gas_entries = []
for i in range(gas["num"]):
    label = tk.Label(gas_window, text=f"气体{i+1}的物质的量:")
    label.pack()

    entry = tk.Entry(gas_window)
    entry.pack()

    gas_entries.append(entry)

def calculate():
    gas_values = [float(entry.get()) for entry in gas_entries]
    total_moles = sum(gas_values)
    molar_fraction = [value / total_moles for value in gas_values]
    total_pressure = float(pressure_entry.get())
    partial_pressures = [molar_fraction[i] * total_pressure for i in range(len(molar_fraction))]

    messagebox.showinfo("计算结果", f"{len(gas_values)}种气体的分压力为:{partial_pressures}")

    gas_window.destroy()

    # 创建条形统计图
    gases = ["气体" + str(i + 1) for i in range(gas["num"])]
    plt.bar(gases, partial_pressures)
    plt.xlabel("气体")
    plt.ylabel("分压力 / atm")
    plt.title(f"{gas['name']}气体分压力统计")
    plt.show()

calculate_button = tk.Button(gas_window, text="计算", command=calculate)
calculate_button.pack()

创建主窗口

window = tk.Tk() window.title("气体种类选择")

def choose_gas(gas): create_gas_window(gas)

设置按钮之间的间距

button_padding = 10

计算按钮总宽度

total_button_width = sum(gas["num"] for gas in gas_info) * 100

将按钮的包装区域放置在主窗口的中间位置

window_width = total_button_width + (len(gas_info) + 1) * button_padding window.geometry(f"{window_width}x300")

创建气体种类选择按钮

for gas in gas_info: button = tk.Button(window, text=gas["num"], bg=gas["color"], fg="black", command=lambda g=gas: choose_gas(g)) button.pack(side=tk.LEFT, padx=button_padding)

启动GUI事件循环

window.mainloop(

请完善这段代码使它更完美:import tkinter as tkfrom tkinter import messageboximport matplotlibpyplot as plt# 保存气体种类和按钮颜色信息gas_info = name 绿 color green num 2 name 蓝 color blue num 3 name 黄 color yellow nu

原文地址: https://www.cveoy.top/t/topic/hFw2 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录