二力平衡指的是,一个物体在两个相反力的作用下保持静止或者匀速运动.
二力平衡模拟器则可以体现这一点:
二力平衡模拟器
用到的库有:
from tkinter import *
import tkinter as tk
import threading,os,time
主函数:
if __name__ == '__main__':#主函数
root = tk.Tk()
root.resizable(False, False)
basedesk(root)
root.mainloop()
os._exit(0)
主界面代码:
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.sca1 = tk.Scale(self.window, orient=HORIZONTAL,length=300, tickinterval=25, )
self.window.sca1.place(x=150,y=450,width=700,height=90)
self.window.sca1.set(50)
self.thread=threading.Thread(target=self.detector)
self.thread.start()
self.init_canvas()
self.window.angle_canvas.create_text((550, 100),text='F1=50',font=("Purisa", 35))
self.window.angle_canvas.create_text((250, 220), text='F2=50', font=("Purisa", 35))
self.window.angle_canvas.create_line((350, 150, 550, 150),width=5,arrow=tk.LAST)
self.window.angle_canvas.create_line((0, 200, 700, 200),width=5,fill='blue')
self.window.angle_canvas.create_line((250, 100, 450 , 100), width=5, fill='green')
self.window.angle_canvas.create_line((250, 200, 450 , 200), width=5, fill='green')
self.window.angle_canvas.create_line((450 , 200, 450 , 100), width=5, fill='green')
self.window.angle_canvas.create_line((250, 200, 250, 100), width=5, fill='green')
self.window.angle_canvas.create_line((350, 150, 150, 150),arrow=tk.LAST)
self.window.show_label = tk.Label(self.window, text="设置推力F:", font=("Helvetic", 20, "bold"), bg='#e5f9ff')
self.window.show_label.place(x=150, y=380)
self.window.enter_btn=tk.Button(self.window,text="重启",bg='#ffffe5',fg='#333399',font=("Helvetic", 20, "bold"),command=self.restart).place(x=550, y=380,width=250, height=50)
def restart(self):
self.window.destroy()
time.sleep(0.1)
functionwindow(self.master)
def detector(self):
state='stop'
pos=0
num2=0
power=0
while True:
try:
num2+=1
num=self.window.sca1.get()
if num2==1:
self.num_record = num
if num >50 :
if num2 %(51**2-(num-50)**2)!=0:
continue
state='right'
power-=((50 - num)/100)
pos+=power
self.window.angle_canvas.destroy()
self.init_canvas()
self.window.angle_canvas.create_text((550+pos, 100), text='F1=' + str(num), font=("Purisa", 35))
self.window.angle_canvas.create_text((250+pos, 220), text='F2=50', font=("Purisa", 35))
self.window.angle_canvas.create_line((350+pos, 150, 550+pos, 150), width=5, arrow=tk.LAST)
self.window.angle_canvas.create_line((0, 200, 700, 200), width=5, fill='blue')
self.window.angle_canvas.create_line((250+pos, 100, 450+pos, 100), width=5, fill='red')
self.window.angle_canvas.create_line((250+pos, 200, 450+pos, 200), width=5, fill='red')
self.window.angle_canvas.create_line((450+pos, 200, 450+pos, 100), width=5, fill='red')
self.window.angle_canvas.create_line((250+pos, 200, 250+pos, 100), width=5, fill='red')
self.window.angle_canvas.create_line((350+pos, 150, 150+pos, 150), arrow=tk.LAST)
continue
elif num <50 :
if num2 % (51 ** 2 - (num - 50) ** 2) != 0:
continue
state='left'
power-=((50 - num)/100)
pos += power
self.window.angle_canvas.destroy()
self.init_canvas()
self.window.angle_canvas.create_text((550 + pos, 100), text='F1=' + str(num), font=("Purisa", 35))
self.window.angle_canvas.create_text((250 + pos, 220), text='F2=50', font=("Purisa", 35))
self.window.angle_canvas.create_line((350 + pos, 150, 550 + pos, 150), width=5, arrow=tk.LAST)
self.window.angle_canvas.create_line((0, 200, 700, 200), width=5, fill='blue')
self.window.angle_canvas.create_line((250 + pos, 100, 450 + pos, 100), width=5, fill='green')
self.window.angle_canvas.create_line((250 + pos, 200, 450 + pos, 200), width=5, fill='green')
self.window.angle_canvas.create_line((450 + pos, 200, 450 + pos, 100), width=5, fill='green')
self.window.angle_canvas.create_line((250 + pos, 200, 250 + pos, 100), width=5, fill='green')
self.window.angle_canvas.create_line((350 + pos, 150, 150 + pos, 150), arrow=tk.LAST)
elif num == 50:
if state!='stop':
if num2 % (51 ** 2 - (num - 50) ** 2) != 0:
continue
power-=((50 - num)/100)
if state=='left':
pos-=power
elif state=='right':
pos+=power
self.window.angle_canvas.destroy()
self.init_canvas()
self.window.angle_canvas.create_text((550 + pos, 100),
text='F1=' + str(
num), font=(
"Purisa", 35))
self.window.angle_canvas.create_text((250 + pos, 220),
text='F2=50',
font=(
"Purisa", 35))
self.window.angle_canvas.create_line(
(350 + pos, 150, 550 + pos, 150), width=5,
arrow=tk.LAST)
self.window.angle_canvas.create_line((0, 200, 700, 200),
width=5,
fill='blue')
self.window.angle_canvas.create_line(
(250 + pos, 100, 450 + pos, 100), width=5,
fill='green')
self.window.angle_canvas.create_line(
(250 + pos, 200, 450 + pos, 200), width=5,
fill='green')
self.window.angle_canvas.create_line(
(450 + pos, 200, 450 + pos, 100), width=5,
fill='green')
self.window.angle_canvas.create_line(
(250 + pos, 200, 250 + pos, 100), width=5,
fill='green')
self.window.angle_canvas.create_line(
(350 + pos, 150, 150 + pos, 150), arrow=tk.LAST)
except:
break
self.num_record=num
def init_canvas(self):
self.window.angle_canvas = tk.Canvas(self.window, bg='white')
self.window.angle_canvas.place(x=150,y=50,width=700,height=300)
完整代码:
from tkinter import *
import tkinter as tk
import threading,os,time
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.sca1 = tk.Scale(self.window, orient=HORIZONTAL,length=300, tickinterval=25, )
self.window.sca1.place(x=150,y=450,width=700,height=90)
self.window.sca1.set(50)
self.thread=threading.Thread(target=self.detector)
self.thread.start()
self.init_canvas()
self.window.angle_canvas.create_text((550, 100),text='F1=50',font=("Purisa", 35))
self.window.angle_canvas.create_text((250, 220), text='F2=50', font=("Purisa", 35))
self.window.angle_canvas.create_line((350, 150, 550, 150),width=5,arrow=tk.LAST)
self.window.angle_canvas.create_line((0, 200, 700, 200),width=5,fill='blue')
self.window.angle_canvas.create_line((250, 100, 450 , 100), width=5, fill='green')
self.window.angle_canvas.create_line((250, 200, 450 , 200), width=5, fill='green')
self.window.angle_canvas.create_line((450 , 200, 450 , 100), width=5, fill='green')
self.window.angle_canvas.create_line((250, 200, 250, 100), width=5, fill='green')
self.window.angle_canvas.create_line((350, 150, 150, 150),arrow=tk.LAST)
self.window.show_label = tk.Label(self.window, text="设置推力F:", font=("Helvetic", 20, "bold"), bg='#e5f9ff')
self.window.show_label.place(x=150, y=380)
self.window.enter_btn=tk.Button(self.window,text="重启",bg='#ffffe5',fg='#333399',font=("Helvetic", 20, "bold"),command=self.restart).place(x=550, y=380,width=250, height=50)
def restart(self):
self.window.destroy()
time.sleep(0.1)
functionwindow(self.master)
def detector(self):
state='stop'
pos=0
num2=0
power=0
while True:
try:
num2+=1
num=self.window.sca1.get()
if num2==1:
self.num_record = num
if num >50 :
if num2 %(51**2-(num-50)**2)!=0:
continue
state='right'
power-=((50 - num)/100)
pos+=power
self.window.angle_canvas.destroy()
self.init_canvas()
self.window.angle_canvas.create_text((550+pos, 100), text='F1=' + str(num), font=("Purisa", 35))
self.window.angle_canvas.create_text((250+pos, 220), text='F2=50', font=("Purisa", 35))
self.window.angle_canvas.create_line((350+pos, 150, 550+pos, 150), width=5, arrow=tk.LAST)
self.window.angle_canvas.create_line((0, 200, 700, 200), width=5, fill='blue')
self.window.angle_canvas.create_line((250+pos, 100, 450+pos, 100), width=5, fill='red')
self.window.angle_canvas.create_line((250+pos, 200, 450+pos, 200), width=5, fill='red')
self.window.angle_canvas.create_line((450+pos, 200, 450+pos, 100), width=5, fill='red')
self.window.angle_canvas.create_line((250+pos, 200, 250+pos, 100), width=5, fill='red')
self.window.angle_canvas.create_line((350+pos, 150, 150+pos, 150), arrow=tk.LAST)
continue
elif num <50 :
if num2 % (51 ** 2 - (num - 50) ** 2) != 0:
continue
state='left'
power-=((50 - num)/100)
pos += power
self.window.angle_canvas.destroy()
self.init_canvas()
self.window.angle_canvas.create_text((550 + pos, 100), text='F1=' + str(num), font=("Purisa", 35))
self.window.angle_canvas.create_text((250 + pos, 220), text='F2=50', font=("Purisa", 35))
self.window.angle_canvas.create_line((350 + pos, 150, 550 + pos, 150), width=5, arrow=tk.LAST)
self.window.angle_canvas.create_line((0, 200, 700, 200), width=5, fill='blue')
self.window.angle_canvas.create_line((250 + pos, 100, 450 + pos, 100), width=5, fill='green')
self.window.angle_canvas.create_line((250 + pos, 200, 450 + pos, 200), width=5, fill='green')
self.window.angle_canvas.create_line((450 + pos, 200, 450 + pos, 100), width=5, fill='green')
self.window.angle_canvas.create_line((250 + pos, 200, 250 + pos, 100), width=5, fill='green')
self.window.angle_canvas.create_line((350 + pos, 150, 150 + pos, 150), arrow=tk.LAST)
elif num == 50:
if state!='stop':
if num2 % (51 ** 2 - (num - 50) ** 2) != 0:
continue
power-=((50 - num)/100)
if state=='left':
pos-=power
elif state=='right':
pos+=power
self.window.angle_canvas.destroy()
self.init_canvas()
self.window.angle_canvas.create_text((550 + pos, 100),
text='F1=' + str(
num), font=(
"Purisa", 35))
self.window.angle_canvas.create_text((250 + pos, 220),
text='F2=50',
font=(
"Purisa", 35))
self.window.angle_canvas.create_line(
(350 + pos, 150, 550 + pos, 150), width=5,
arrow=tk.LAST)
self.window.angle_canvas.create_line((0, 200, 700, 200),
width=5,
fill='blue')
self.window.angle_canvas.create_line(
(250 + pos, 100, 450 + pos, 100), width=5,
fill='green')
self.window.angle_canvas.create_line(
(250 + pos, 200, 450 + pos, 200), width=5,
fill='green')
self.window.angle_canvas.create_line(
(450 + pos, 200, 450 + pos, 100), width=5,
fill='green')
self.window.angle_canvas.create_line(
(250 + pos, 200, 250 + pos, 100), width=5,
fill='green')
self.window.angle_canvas.create_line(
(350 + pos, 150, 150 + pos, 150), arrow=tk.LAST)
except:
break
self.num_record=num
def init_canvas(self):
self.window.angle_canvas = tk.Canvas(self.window, bg='white')
self.window.angle_canvas.place(x=150,y=50,width=700,height=300)
if __name__ == '__main__':#主函数
root = tk.Tk()
root.resizable(False, False)
basedesk(root)
root.mainloop()
os._exit(0)
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)