diff --git a/equation_solver.py b/equation_solver.py index 99528c6..82115b4 100644 --- a/equation_solver.py +++ b/equation_solver.py @@ -59,8 +59,7 @@ def solve_equation(eq): try: result = sp.optimize.lsq_linear(matrix, constant, bounds=(0, None)).x.tolist() except ValueError: - print('无法配平') - return None + raise ValueError('无法配平') # 将结果写入化学方程式,最后一个生成物的系数需要计算得到 last_substance = eq['right'][-1] diff --git a/main.py b/main.py index b92af5a..76b65a4 100644 --- a/main.py +++ b/main.py @@ -5,19 +5,68 @@ import parser import equation_solver +import tkinter +from tkinter import messagebox +from ttkbootstrap import Style + +from validity_check import IllegalAtomException # 按间距中的绿色按钮以运行脚本。 if __name__ == '__main__': - while True: - eq = input('请输入化学方程式:(输入exit退出)') - if eq == 'exit': - break + # 新建样式 + style = Style('flatly') + # 创建窗口 + window = style.master + window.title('化学方程式自动配平器') + window.geometry('500x140') + # 创建输入框 + input_label = tkinter.Label(window, text='输入方程式:') + input_label.place(x=10, y=10) + input_entry = tkinter.Entry(window) + input_entry.place(x=100, y=10, width=300, height=30) + # 创建输出框 + output_label = tkinter.Label(window, text='输出方程式:') + output_label.place(x=10, y=50) + output_entry = tkinter.Entry(window) + output_entry.place(x=100, y=50, width=300, height=30) + # 创建按钮 + button = tkinter.Button(window, text='配平') + button.place(x=420, y=10, width=60, height=70) + # 创建菜单 + menu = tkinter.Menu(window) + menu.add_command(label='关于', + command=lambda: messagebox.showinfo('关于', '化学方程式自动配平器\n作者:lucas8485\n版本:1.0')) + menu.add_command(label='帮助', command=lambda: messagebox.showinfo('帮助', '''输入方程式,点击配平按钮,即可得到配平后的方程式。 + 注意:方程式中的元素必须是化学元素的符号, + 不能是化学元素的全称,且大小写敏感。 + 如果有分隔符,请不要输入分隔符。''')) + window.config(menu=menu) + + # 事件绑定 + def balance(): + eq = input_entry.get() try: - eq = parser.parse_equation(eq) - eq = equation_solver.solve_equation(eq) - if eq is not None: - print('化学方程式:', parser.format_equation(eq)) - except Exception as e: - print(e.args[0]) + parsed_eq = parser.parse_equation(eq) + output_entry.delete(0, 'end') + except IllegalAtomException: + # 弹出错误提示 + tkinter.messagebox.showerror('错误', '输入的方程式有误!') + return + except ValueError: + # 弹出错误提示 + tkinter.messagebox.showerror('错误', '输入的方程式有误!') + return + try: + balanced_eq = equation_solver.solve_equation(parsed_eq) + except ValueError: + tkinter.messagebox.showerror('错误', '输入的方程式无法配平!') + return + output_entry.delete(0, tkinter.END) + output_entry.insert(0, parser.format_equation(balanced_eq)) + + + button.bind('', lambda event: balance()) + # 进入消息循环 + window.mainloop() # 访问 https://www.jetbrains.com/help/pycharm/ 获取 PyCharm 帮助 diff --git a/parser.py b/parser.py index b61f0de..fa08e04 100644 --- a/parser.py +++ b/parser.py @@ -154,12 +154,14 @@ def parse_equation(eq): :param eq: 化学方程式 :return: 化学方程式的字典表示 """ - eq = eq.replace(' ', '') - eq = eq.replace('->', '=') - eq = eq.replace('=', '=>') - eq = eq.split('=>') - left = eq[0] - right = eq[1] + equation = eq.replace(' ', '') + equation = equation.replace('->', '=') + equation = equation.replace('=', '=>') + equation = equation.split('=>') + if len(equation) != 2: + raise ValueError('化学方程式错误') + left = equation[0] + right = equation[1] left = left.split('+') right = right.split('+') left = [parse_molecule(molecule) for molecule in left] diff --git a/requirements.txt b/requirements.txt index adfd4e9..da073bf 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,2 +1,3 @@ numpy~=1.23.5 -scipy~=1.9.3 \ No newline at end of file +scipy~=1.9.3 +ttkbootstrap \ No newline at end of file