Горизонтальный виджет

Горизонтальный измерительный виджет выводит на canvas измеренное значение аналогового сигнала.




def hMeterC – функция создания объекта
Возвратные значения:
return (c,'hmeter',x,y,widgLen,widgHigh,maxValue,outerColor,nameValue)

с – ссылка на объект
‘hmeter’ – строковый идентификатор объекта
х – координата х
у – координата у
widgLen – ширина виджета,
widgHigh – высота виджета,
maxValue – максимальное измеряемое значение,
outerColor – цвет заполнения ,
nameValue – название измеряемого значения

def hMeter – функция изменения значения
с – ссылка на объект canvas
nowValue – величина измеренного значения
х – координата х
у – координата у
widgLen – ширина виджета,
widgHigh – высота виджета,
maxValue – максимальное измеряемое значение,
outerColor – цвет заполнения ,
nameValue – название измеряемого значения


def hMeterC(nowValue,x,y,widgLen,widgHigh,maxValue,outerColor,nameValue):
     c = Canvas(root,width=widgLen+50,height=widgHigh+40,bg="black",bd=0, highlightthickness=0, relief='ridge')
     c.place(x=x, y=y)
     if(nowValue > maxValue): nowValue=maxValue-1
     devValue=float(widgLen) / float(maxValue)
     mesureValue = devValue * nowValue
     c.create_rectangle(1,1,widgLen,widgHigh,fill='black',outline=outerColor)
     c.create_rectangle(2,2,int(mesureValue),widgHigh-1,fill='red',outline='red')
     c.create_line(1,widgHigh,1,widgHigh+5,width=1,fill=outerColor)
     c.create_line(widgLen,widgHigh,widgLen,widgHigh+5,width=1,fill=outerColor)
     c.create_line(1+widgLen/4,widgHigh,1+widgLen/4 ,widgHigh+5,width=1,fill=outerColor)
     c.create_line(1+widgLen/2,widgHigh,1+widgLen/2 ,widgHigh+5,width=1,fill=outerColor)
     c.create_line(1+widgLen-widgLen/4,widgHigh,1+widgLen-widgLen/4 ,widgHigh+5,width=1,fill=outerColor)
     c.create_text(0,widgHigh+10,font="Verdana 10",anchor="w",justify=CENTER,fill='white',text='0')
     c.create_text(widgLen -10,widgHigh+10,font="Verdana 10",anchor="w",justify=CENTER,fill='white',text=str(maxValue))
     c.create_text(widgLen/2 -10,widgHigh+10,font="Verdana 10",anchor="w",justify=CENTER,fill='white',text=str(int(maxValue/2)))
     c.create_text(widgLen/4-10,widgHigh+10,font="Verdana 10",anchor="w",justify=CENTER,fill='white',text=str(int(maxValue/4)))
     c.create_text(widgLen-widgLen/4-10,widgHigh+10,font="Verdana 10",anchor="w",justify=CENTER,fill='white',text=str(int(maxValue-maxValue/4)))
     c.create_text(widgLen/2 -10,widgHigh-8,font="Verdana 12",anchor="w",justify=CENTER,fill='white',text=str(int(nowValue)))
     c.create_text(1,widgHigh+21,font="Verdana 10",anchor="w",justify=CENTER,fill='white',text=nameValue)
     return (c,'hmeter',x,y,widgLen,widgHigh,maxValue,outerColor,nameValue)

def hMeter(c,nowValue,x,y,widgLen,widgHigh,maxValue,outerColor,nameValue):
     if(nowValue > maxValue): nowValue=maxValue-1
     devValue=float(widgLen) / float(maxValue)
     mesureValue = devValue * nowValue
     c.create_rectangle(1,1,widgLen,widgHigh,fill='black',outline=outerColor)
     c.create_rectangle(2,2,int(mesureValue),widgHigh-1,fill=outerColor,outline=outerColor)
     c.create_line(1,widgHigh,1,widgHigh+5,width=1,fill=outerColor)
     c.create_line(widgLen,widgHigh,widgLen,widgHigh+5,width=1,fill=outerColor)
     c.create_line(1+widgLen/4,widgHigh,1+widgLen/4 ,widgHigh+5,width=1,fill=outerColor)
     c.create_line(1+widgLen/2,widgHigh,1+widgLen/2 ,widgHigh+5,width=1,fill=outerColor)
     c.create_line(1+widgLen-widgLen/4,widgHigh,1+widgLen-widgLen/4 ,widgHigh+5,width=1,fill=outerColor)
     c.create_text(0,widgHigh+10,font="Verdana 10",anchor="w",justify=CENTER,fill='white',text='0')
     c.create_text(widgLen -10,widgHigh+10,font="Verdana 10",anchor="w",justify=CENTER,fill='white',text=str(maxValue))
     c.create_text(widgLen/2 -10,widgHigh+10,font="Verdana 10",anchor="w",justify=CENTER,fill='white',text=str(int(maxValue/2)))
     c.create_text(widgLen/4-10,widgHigh+10,font="Verdana 10",anchor="w",justify=CENTER,fill='white',text=str(int(maxValue/4)))
     c.create_text(widgLen-widgLen/4-10,widgHigh+10,font="Verdana 10",anchor="w",justify=CENTER,fill='white',text=str(int(maxValue-maxValue/4)))
     c.create_text(widgLen +10,widgHigh-8,font="Verdana 12",anchor="w",justify=CENTER,fill=outerColor,text=str(int(nowValue)))
     c.create_text(1,widgHigh+21,font="Verdana 10",anchor="w",justify=CENTER,fill='white',text=nameValue)


Теперь добавим запрос по аналоговым регистрам

analogFig=hMeterC(0,20,20,300,20,1000,'red','analog Meter')
getAI=master.execute(1, cst.READ_INPUT_REGISTERS, 0, 10)
hMeter(analogFig[0],getAI[0],20,20,300,20,1000,'red','analog Meter')

Текст программы примет вид:

from Tkinter import *
import modbus_tk
import modbus_tk.defines as cst
import modbus_tk.modbus_tcp as modbus_tcp
def hMeterC(nowValue,x,y,widgLen,widgHigh,maxValue,outerColor,nameValue):
     c = Canvas(root,width=widgLen+50,height=widgHigh+40,bg="black",bd=0, highlightthickness=0, relief='ridge')
     c.place(x=x, y=y)
     if(nowValue > maxValue): nowValue=maxValue-1
     devValue=float(widgLen) / float(maxValue)
     mesureValue = devValue * nowValue
     c.create_rectangle(1,1,widgLen,widgHigh,fill='black',outline=outerColor)
     c.create_rectangle(2,2,int(mesureValue),widgHigh-1,fill='red',outline='red')
     c.create_line(1,widgHigh,1,widgHigh+5,width=1,fill=outerColor)
     c.create_line(widgLen,widgHigh,widgLen,widgHigh+5,width=1,fill=outerColor)
     c.create_line(1+widgLen/4,widgHigh,1+widgLen/4 ,widgHigh+5,width=1,fill=outerColor)
     c.create_line(1+widgLen/2,widgHigh,1+widgLen/2 ,widgHigh+5,width=1,fill=outerColor)
     c.create_line(1+widgLen-widgLen/4,widgHigh,1+widgLen-widgLen/4 ,widgHigh+5,width=1,fill=outerColor)
     c.create_text(0,widgHigh+10,font="Verdana 10",anchor="w",justify=CENTER,fill='white',text='0')
     c.create_text(widgLen -10,widgHigh+10,font="Verdana 10",anchor="w",justify=CENTER,fill='white',text=str(maxValue))
     c.create_text(widgLen/2 -10,widgHigh+10,font="Verdana 10",anchor="w",justify=CENTER,fill='white',text=str(int(maxValue/2)))
     c.create_text(widgLen/4-10,widgHigh+10,font="Verdana 10",anchor="w",justify=CENTER,fill='white',text=str(int(maxValue/4)))
     c.create_text(widgLen-widgLen/4-10,widgHigh+10,font="Verdana 10",anchor="w",justify=CENTER,fill='white',text=str(int(maxValue-maxValue/4)))
     c.create_text(widgLen/2 -10,widgHigh-8,font="Verdana 12",anchor="w",justify=CENTER,fill='white',text=str(int(nowValue)))
     c.create_text(1,widgHigh+21,font="Verdana 10",anchor="w",justify=CENTER,fill='white',text=nameValue)
     return (c,'hmeter',x,y,widgLen,widgHigh,maxValue,outerColor,nameValue)

def hMeter(c,nowValue,x,y,widgLen,widgHigh,maxValue,outerColor,nameValue):
     if(nowValue > maxValue): nowValue=maxValue-1
     devValue=float(widgLen) / float(maxValue)
     mesureValue = devValue * nowValue
     c.create_rectangle(1,1,widgLen,widgHigh,fill='black',outline=outerColor)
     c.create_rectangle(2,2,int(mesureValue),widgHigh-1,fill=outerColor,outline=outerColor)
     c.create_line(1,widgHigh,1,widgHigh+5,width=1,fill=outerColor)
     c.create_line(widgLen,widgHigh,widgLen,widgHigh+5,width=1,fill=outerColor)
     c.create_line(1+widgLen/4,widgHigh,1+widgLen/4 ,widgHigh+5,width=1,fill=outerColor)
     c.create_line(1+widgLen/2,widgHigh,1+widgLen/2 ,widgHigh+5,width=1,fill=outerColor)
     c.create_line(1+widgLen-widgLen/4,widgHigh,1+widgLen-widgLen/4 ,widgHigh+5,width=1,fill=outerColor)
     c.create_text(0,widgHigh+10,font="Verdana 10",anchor="w",justify=CENTER,fill='white',text='0')
     c.create_text(widgLen -10,widgHigh+10,font="Verdana 10",anchor="w",justify=CENTER,fill='white',text=str(maxValue))
     c.create_text(widgLen/2 -10,widgHigh+10,font="Verdana 10",anchor="w",justify=CENTER,fill='white',text=str(int(maxValue/2)))
     c.create_text(widgLen/4-10,widgHigh+10,font="Verdana 10",anchor="w",justify=CENTER,fill='white',text=str(int(maxValue/4)))
     c.create_text(widgLen-widgLen/4-10,widgHigh+10,font="Verdana 10",anchor="w",justify=CENTER,fill='white',text=str(int(maxValue-maxValue/4)))
     c.create_text(widgLen +10,widgHigh-8,font="Verdana 12",anchor="w",justify=CENTER,fill=outerColor,text=str(int(nowValue)))
     c.create_text(1,widgHigh+21,font="Verdana 10",anchor="w",justify=CENTER,fill='white',text=nameValue)

master = modbus_tcp.TcpMaster(host='192.168.0.1', port=502)
master.set_timeout(1.0)

root = Tk()
im = PhotoImage(file='bg.gif')
canv = Canvas(root,width=1900,height=950,bg="black",bd=0, highlightthickness=0, relief='ridge')
canv.place(x=0, y=25)
canv.create_image(1, 1,anchor=NW, image=im)
nText=canv.create_text(10,10,text="000",font="Verdana 12",anchor="w",justify=CENTER,fill='white')

analogFig=hMeterC(0,20,120,300,20,1000,'red','analog Meter')
getAI=master.execute(1, cst.READ_INPUT_REGISTERS, 0, 10)
getDI=master.execute(1, cst.READ_DISCRETE_INPUTS, 0, 10)
canv.itemconfig(nText,text=str(getDI[0]))
hMeter(analogFig[0],getAI[0],20,120,300,20,1000,'red','analog Meter')
root.mainloop()

В координатах canvas x=10 и y=10 будет выведено состояние дискретного сигнала по адресу [0] ноль или 1,
а в координатах х=20, у=120 будет отображаться горизонтальный измерительный прибор со значением.