这个发明是一个很奇怪的发明。。。直播的时候哥们要做的。。。掉了我不少头发。。。
还好做出来了,首先展示运行效果:
视频运行效果:
教你如何用python制作一阶线性齐次方程模拟器 python项目小发明
首先我们先补补一阶线性齐次方程的通解怎么求:
这样一个式子,其通解为:
所以这个模型的难点是求积分。。。
查资料可知,积分可以这样求:
x = symbols("x")
f=eval(list_get)
A = integrate(f, x)
接下来展示前端代码:
class basedesk():#底板
def __init__(self, master):
self.master = master
self.master.title("一阶微分方程模拟器")
self.master.configure(bg='#B1FFF9')
self.master.geometry("1000x600")
mainwindow(self.master)
class mainwindow():#主界面
def __init__(self, master):
self.master = master
self.window = tk.Frame(self.master, bg='#e5ffe5')
self.window.place(x=0,y=0,width=1000,height=600)
self.window.showname_label=tk.Label(self.window,text="一阶微分方程模拟器",fg='#26734d', bg='#ffe5ff',font=("Helvetic",60,"bold"),relief=RAISED).place(x=0, y=10,width=1000, height=150)
self.window.enter_btn=tk.Button(self.window,text="开始",bg='#ffffe5',fg='#333399',font=("Helvetic", 60, "bold"),command=self.changetofunction).place(x=360, y=300,width=250, height=150)
def changetofunction(self,):
self.window.destroy()
functionwindow(self.master)
class functionwindow():
def __init__(self, master):
self.master = master
self.window = tk.Frame(self.master, bg='#e5f9ff')
self.window.place(x=0, y=0, width=1000, height=600)
self.window.left_label = tk.Label(self.window, text="y'+", font=("Helvetic", 20, "bold"), bg='#e5f9ff').place(x=10, y=100)
self.window.right_label = tk.Label(self.window, text="y=0", font=("Helvetic", 20, "bold"), bg='#e5f9ff').place(x=900, y=100)
self.window.entry1=tk.Entry(self.window, font=("Helvetica", 20))
self.window.entry1.place(x=90, y=100, width=800, height=40)
self.window.enter_btn=tk.Button(self.window,text="计算一阶线性齐次方程的通解",bg='#ffffe5',fg='#333399',font=("Helvetic", 30, "bold"),command=self.calculate).place(x=200, y=300,width=570, height=100)
后端逻辑:
def calculate(self):
list_get=self.window.entry1.get()
if list_get=='':
messagebox.showerror('错误','输入的一元方程不能为空')
return
try:
x = symbols("x")
f=eval(list_get)
A = integrate(f, x)
answer='y=C*e^-('+str(A)+')'
messagebox.showinfo('计算结果',answer)
except:
messagebox.showerror("错误",'输入格式有误')
最后展示完整代码:
#一阶微分方程模拟器
from tkinter import *
from tkinter import messagebox
import tkinter as tk
from sympy import *
class basedesk():#底板
def __init__(self, master):
self.master = master
self.master.title("一阶微分方程模拟器")
self.master.configure(bg='#B1FFF9')
self.master.geometry("1000x600")
mainwindow(self.master)
class mainwindow():#主界面
def __init__(self, master):
self.master = master
self.window = tk.Frame(self.master, bg='#e5ffe5')
self.window.place(x=0,y=0,width=1000,height=600)
self.window.showname_label=tk.Label(self.window,text="一阶微分方程模拟器",fg='#26734d', bg='#ffe5ff',font=("Helvetic",60,"bold"),relief=RAISED).place(x=0, y=10,width=1000, height=150)
self.window.enter_btn=tk.Button(self.window,text="开始",bg='#ffffe5',fg='#333399',font=("Helvetic", 60, "bold"),command=self.changetofunction).place(x=360, y=300,width=250, height=150)
def changetofunction(self,):
self.window.destroy()
functionwindow(self.master)
class functionwindow():
def __init__(self, master):
self.master = master
self.window = tk.Frame(self.master, bg='#e5f9ff')
self.window.place(x=0, y=0, width=1000, height=600)
self.window.left_label = tk.Label(self.window, text="y'+", font=("Helvetic", 20, "bold"), bg='#e5f9ff').place(x=10, y=100)
self.window.right_label = tk.Label(self.window, text="y=0", font=("Helvetic", 20, "bold"), bg='#e5f9ff').place(x=900, y=100)
self.window.entry1=tk.Entry(self.window, font=("Helvetica", 20))
self.window.entry1.place(x=90, y=100, width=800, height=40)
self.window.enter_btn=tk.Button(self.window,text="计算一阶线性齐次方程的通解",bg='#ffffe5',fg='#333399',font=("Helvetic", 30, "bold"),command=self.calculate).place(x=200, y=300,width=570, height=100)
def calculate(self):
list_get=self.window.entry1.get()
if list_get=='':
messagebox.showerror('错误','输入的一元方程不能为空')
return
try:
x = symbols("x")
f=eval(list_get)
A = integrate(f, x)
answer='y=C*e^-('+str(A)+')'
messagebox.showinfo('计算结果',answer)
except:
messagebox.showerror("错误",'输入格式有误')
if __name__ == '__main__':#主函数
root = tk.Tk()
root.resizable(False, False)
basedesk(root)
root.mainloop()
如何把python代码生成exe小程序https://mp.csdn.net/mp_blog/creation/editor/122758084