[Python] 纯文本查看 复制代码 import tkinter as tk
from sympy import Eq, symbols, solve
def solve_equation():
# 获取输入的方程和未知数
equation = equation_entry.get()
unknown_var = var_entry.get()
# 将方程和未知数转化成符号
x = symbols(unknown_var)
eq = Eq(eval(equation), 0)
# 解方程
solution = solve(eq, x)
# 显示结果
result_label.config(text=f"{unknown_var} 的解为: {solution}")
# 创建窗口
window = tk.Tk()
window.title("解方程")
window.geometry("300x200")
# 创建方程输入框和标签
equation_label = tk.Label(window, text="方程:")
equation_label.pack()
equation_entry = tk.Entry(window)
equation_entry.pack()
# 创建未知数输入框和标签
var_label = tk.Label(window, text="未知数:")
var_label.pack()
var_entry = tk.Entry(window)
var_entry.pack()
# 创建“解方程”按钮
solve_button = tk.Button(window, text="解方程", command=solve_equation)
solve_button.pack()
# 创建结果标签
result_label = tk.Label(window, text="")
result_label.pack()
# 运行窗口
window.mainloop()
|