> 文档中心 > 如何用python制作温度计模拟器 python项目小发明 【安安教具】-【物理】-【温度计】模拟器

如何用python制作温度计模拟器 python项目小发明 【安安教具】-【物理】-【温度计】模拟器

温度计测量水温的过程要满足如下条件:

1.不能碰烧杯底。

2.不能碰烧杯壁。

设计一款动手实践上面过程的软件,就是接下来要完成的~

我们先看一下效果:

如何用python制作温度计模拟器 python项目小发明

我们可以看到,烧杯和温度计要满足一定的位置关系才可以读取。

那么要如何实现这样一个功能呢?

首先我们先上网查找一些素材:

 

 完事用下面的语句进行插入:

 self.window.axis_canvas.im1 = Image.open("./烧杯.png") self.window.axis_canvas.img1 = ImageTk.PhotoImage(self.window.axis_canvas.im1) self.window.axis_canvas.imLabel1 = tk.Label(self.window.axis_canvas, image=self.window.axis_canvas.img1) self.window.axis_canvas.imLabel1.place(x=self.x1, y=self.y1, width=204, height=231) self.window.axis_canvas.imLabel1.bind("", self.key1) self.window.axis_canvas.im2 = Image.open("./温度计.jpg") self.window.axis_canvas.img2 = ImageTk.PhotoImage(self.window.axis_canvas.im2) self.window.axis_canvas.imLabel2 = tk.Label(self.window.axis_canvas, image=self.window.axis_canvas.img2) self.window.axis_canvas.imLabel2.place(x=self.x2, y=self.y2, width=49, height=240) self.window.axis_canvas.imLabel2.bind("", self.key2)

展示并介绍完整代码:

from tkinter import *from tkinter import messageboximport tkinter as tkimport threadingimport timeimport osfrom pymouse import PyMouseimport pyHookfrom PIL import Image,ImageTkdef onMouseEvent(event):    return event.Positiondef mouse_detector():    global MOUSE_POS    hm = pyHook.HookManager()    hm.MouseAll = onMouseEvent    while True: MOUSE_POS=list(PyMouse().position()) time.sleep(0.02)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.x1=50 self.y1=50 self.boardlock1 = True self.checklock1 = 0 self.x2=350 self.y2=50 self.boardlock2 = True self.checklock2 = 0 self.all_check=0 self.all_check2 = 0 self.init_canvas1() self.window.enter_btn=tk.Button(self.window,text="读取",bg='#ffffe5',fg='#333399',font=("Helvetic", 60, "bold"),command=self.start).place(x=660, y=500,width=250, height=80)    #由于需要保证点击的元件要放到最上面,故设置了两个初始化画布的方法    def init_canvas1(self): self.window.axis_canvas = tk.Canvas(self.window, bg='white') self.window.axis_canvas.place(x=50, y=50, width=900, height=430) self.window.axis_canvas.im1 = Image.open("./烧杯.png") self.window.axis_canvas.img1 = ImageTk.PhotoImage(self.window.axis_canvas.im1) self.window.axis_canvas.imLabel1 = tk.Label(self.window.axis_canvas, image=self.window.axis_canvas.img1) self.window.axis_canvas.imLabel1.place(x=self.x1, y=self.y1, width=204, height=231) self.window.axis_canvas.imLabel1.bind("", self.key1) self.window.axis_canvas.im2 = Image.open("./温度计.jpg") self.window.axis_canvas.img2 = ImageTk.PhotoImage(self.window.axis_canvas.im2) self.window.axis_canvas.imLabel2 = tk.Label(self.window.axis_canvas, image=self.window.axis_canvas.img2) self.window.axis_canvas.imLabel2.place(x=self.x2, y=self.y2, width=49, height=240) self.window.axis_canvas.imLabel2.bind("", self.key2)    def init_canvas2(self): self.window.axis_canvas = tk.Canvas(self.window, bg='white') self.window.axis_canvas.place(x=50, y=50, width=900, height=430) self.window.axis_canvas.im2 = Image.open("./温度计.jpg") self.window.axis_canvas.img2 = ImageTk.PhotoImage(self.window.axis_canvas.im2) self.window.axis_canvas.imLabel2 = tk.Label(self.window.axis_canvas, image=self.window.axis_canvas.img2) self.window.axis_canvas.imLabel2.place(x=self.x2, y=self.y2, width=49, height=240) self.window.axis_canvas.imLabel2.bind("", self.key2) self.window.axis_canvas.im1 = Image.open("./烧杯.png") self.window.axis_canvas.img1 = ImageTk.PhotoImage(self.window.axis_canvas.im1) self.window.axis_canvas.imLabel1 = tk.Label(self.window.axis_canvas, image=self.window.axis_canvas.img1) self.window.axis_canvas.imLabel1.place(x=self.x1, y=self.y1, width=204, height=231) self.window.axis_canvas.imLabel1.bind("", self.key1)    #用来检测是否可以读取数据    def start(self): if self.x1<self.x2<self.x1+50:     messagebox.showerror("错误",'温度计不可以碰烧杯左壁')     return if self.x1+120<self.x2<self.x1+170:     messagebox.showerror("错误",'温度计不可以碰烧杯右壁')     return if self.y2<self.y1<self.y2+50:     messagebox.showerror("错误", '温度计不可以碰烧杯底')     return if self.x1+50<self.x2self.y2+50>self.y1-80:     messagebox.showinfo("恭喜",'读取成功')     return messagebox.showerror("错误","温度计位置不对哦~")    def key1(self,event): if self.all_check==1:     return self.all_check = 1 if self.checklock1==0:     self.checklock1 = 1 - self.checklock1     self.control_x1 = event.x     self.control_y1 = event.y     boardthread1 = threading.Thread(target=self.mouse_detector1)     boardthread1.start() else:     self.checklock1 = 1 - self.checklock1     self.boardlock1 = False self.all_check = 0    def key2(self,event): if self.all_check==1:     return self.all_check = 1 if self.checklock2==0:     self.checklock2 = 1 - self.checklock2     self.control_x2 = event.x     self.control_y2 = event.y     boardthread2 = threading.Thread(target=self.mouse_detector2)     boardthread2.start() else:     self.checklock2 = 1 - self.checklock2     self.boardlock2 = False self.all_check = 0    def mouse_detector1(self): if self.all_check2==1:     return self.all_check2 = 1 global MOUSE_POS self.x1_real = MOUSE_POS[0]  -60 - self.master.winfo_x() self.y1_real = MOUSE_POS[1] -80 - self.master.winfo_y() record=[self.x1_real, self.y1_real] while self.boardlock1:     if MOUSE_POS[0]-60-self.master.winfo_x()-self.x1  202:  self.checklock1 = 1 - self.checklock1  self.all_check = 0  break     if MOUSE_POS[1] -80 - self.master.winfo_y() - self.y1  229:  self.checklock1 = 1 - self.checklock1  self.all_check = 0  break     self.x1_real=MOUSE_POS[0]-60-self.master.winfo_x()     self.y1_real = MOUSE_POS[1] -80 - self.master.winfo_y()     if record!=[self.x1_real, self.y1_real]:  self.x1=self.x1_real-self.control_x1  self.y1 = self.y1_real - self.control_y1  self.window.axis_canvas.destroy()  self.init_canvas2()     time.sleep(0.00001)     record = [self.x1_real, self.y1_real] self.boardlock1 = True self.all_check2 = 0    def mouse_detector2(self): if self.all_check2==1:     return self.all_check2 = 1 global MOUSE_POS self.x2_real = MOUSE_POS[0] - 60 - self.master.winfo_x() self.y2_real = MOUSE_POS[1] - 80 - self.master.winfo_y() record = [self.x2_real, self.y2_real] while self.boardlock2:     if  MOUSE_POS[0] - 60 - self.master.winfo_x() - self.x2  47:  self.checklock2 = 1 - self.checklock2  self.all_check = 0  break     if MOUSE_POS[1] - 80 - self.master.winfo_y() - self.y2  238:  self.checklock2 = 1 - self.checklock2  self.all_check = 0  print('---')  break     self.x2_real = MOUSE_POS[0] - 60 - self.master.winfo_x()     self.y2_real = MOUSE_POS[1] - 80 - self.master.winfo_y()     if record != [self.x2_real, self.y2_real]:  self.x2 = self.x2_real - self.control_x2  self.y2 = self.y2_real - self.control_y2  self.window.axis_canvas.destroy()  self.init_canvas1()     time.sleep(0.00001)     record = [self.x2_real, self.y2_real] self.boardlock2 = True self.all_check2 = 0if __name__ == '__main__':#主函数    thread = threading.Thread(target=mouse_detector)    thread.start()    root = tk.Tk()    root.resizable(False, False)    basedesk(root)    root.mainloop()    os._exit(0)