python实现称量类,并更新到tkinter界面显示

import threading
import tkinter as tk
data=0
def fun():
    global data
    data+=20
    print(data)
    return data




class ScaleSensor:
    '''定义一个称类'''
    def __init__(self,advance_weight,target_weight):
        '''
        :param current_weight: 当前重量
        :param advance_weight: 设定提前量
        :param target_weight: 目标重量
        '''
        self.current_weight = 0
        self.advance_weight = advance_weight
        self.target_weight = target_weight
        self.rough_stability_time=1
        self.accuracy_stablility_time=0.5
        self.deleay_time=5
        self.is_weightting=True
        self.widget_var = None
    def get_current_weight(self,fun):
        '''
        :param fun: 获取称值函数
        :return:
        '''
        value=fun()
        return value

    def set_advance_weight(self,value:int):
        self.advance_weight=value

    def set_target_weight(self,value:int):
        self.target_weight=value

    def set_stability_time(self,value:int):
        self.stability_time = value

    def set_dealay_time(self,value:int):
        self.deleay_time=value

    def rough_open_door(self):
        print('粗称时开两门')
    def rough_close_door(self):
        print('粗称门关')
    def accuracy_open_door(self):
        print('精称门开')
    def accuracy_close_door(self):
        print('精称门关')

    def widget_show(self,widget_var):
        '''
        :param widget_var: tk.StringVar,tk.IntVar
        '''
        self.widget_var=widget_var
    def start_weight(self):
        '''
        :param callback: 需要执行的函数,如给plc指定值
        :param widget: 把值在界面上更新
        :return:
        '''
        import time

        while True:
            self.current_weight=self.get_current_weight(fun) #获取重量值

            if self.widget_var:
                self.widget_var.set(str(self.current_weight))

            if self.is_weightting == False:
                break

            if self.current_weight>self.target_weight:
                return
            elif self.current_weight<self.target_weight-self.advance_weight:
                self.rough_open_door()#打开两个称门函数
                time.sleep(self.rough_stability_time)
                self.rough_close_door()#关闭称门
            elif self.current_weight>=self.target_weight-self.advance_weight:
                self.accuracy_open_door()
                time.sleep(self.accuracy_stablility_time)
                self.accuracy_close_door()
            print('----------')
            time.sleep(self.deleay_time)   #稳定延时
            if self.current_weight==self.target_weight:
                self.accuracy_close_door()
                print('称量结束',self.current_weight)
                return



def btn_click():
    t.start()

def fun2():
    scal=ScaleSensor(50,100)
    scal.rough_stability_time=2
    scal.accuracy_stablility_time=1
    scal.set_advance_weight(60)
    scal.set_target_weight(180)
    scal.widget_show(sacl_var)
    scal.start_weight()



if __name__ == '__main__':



    root=tk.Tk()
    root.geometry('500x400')
    sacl_var=tk.StringVar()

    label=tk.Label(root,bg='green',width=20,height=5,textvariable=sacl_var)
    label.place(x=100,y=30)

    btn=tk.Button(root,text='click',command=btn_click)
    btn.place(x=100,y=150)

    t=threading.Thread(target=fun2)

    root.mainloop()


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值