Вертикальный виджет

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




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

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

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


def vMeterC(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(widgHigh) / float(maxValue)
     mesureValue = devValue * nowValue
     c.create_rectangle(1,1,widgLen,widgHigh,fill='black',outline=outerColor)
     c.create_rectangle(widgLen-1,widgHigh,2,widgHigh-int(mesureValue),fill=outerColor,outline=outerColor)
     c.create_line(widgLen,widgHigh,widgLen+10,widgHigh,width=1,fill=outerColor)
     c.create_line(widgLen,widgHigh/4,widgLen+10,widgHigh/4,width=1,fill=outerColor)
     c.create_line(widgLen,widgHigh/2,widgLen+10,widgHigh/2,width=1,fill=outerColor)
     c.create_line(widgLen,widgHigh-widgHigh/4,widgLen+10,widgHigh-widgHigh/4,width=1,fill=outerColor)
     c.create_line(widgLen,y-29,widgLen+10,y-29,width=1,fill=outerColor)
     c.create_line(widgLen+10,widgHigh,widgLen+10 ,widgHigh,width=1,fill=outerColor)
     c.create_text(widgLen+12,widgHigh,font="Verdana 10",anchor="w",justify=CENTER,fill=outerColor,text='0')
     c.create_text(widgLen+12,y-19,font="Verdana 10",anchor="w",justify=CENTER,fill=outerColor,text=str(maxValue))
     c.create_text(widgLen+12,widgHigh/2,font="Verdana 10",anchor="w",justify=CENTER,fill=outerColor,text=str(maxValue/2))
     c.create_text(widgLen+12,widgHigh-widgHigh/4,font="Verdana 10",anchor="w",justify=CENTER,fill=outerColor,text=str(maxValue/4))
     c.create_text(widgLen+12,widgHigh/4,font="Verdana 10",anchor="w",justify=CENTER,fill=outerColor,text=str(maxValue-maxValue/4))
     c.create_text(2,widgHigh+15,font="Verdana 12",anchor="w",justify=CENTER,fill=outerColor,text=str(nowValue))
     return (c,'vmeter',x,y,widgLen,widgHigh,maxValue,outerColor,nameValue)
def vMeter(c,nowValue,x,y,widgLen,widgHigh,maxValue,outerColor,nameValue):
     if(nowValue > maxValue): nowValue=maxValue-1
     devValue=float(widgHigh) / float(maxValue)
     mesureValue = devValue * nowValue
     c.create_rectangle(1,1,widgLen,widgHigh,fill='black',outline=outerColor)
     c.create_rectangle(widgLen-1,widgHigh,2,widgHigh-int(mesureValue),fill=outerColor,outline=outerColor)
     c.create_line(widgLen,widgHigh,widgLen+10,widgHigh,width=1,fill=outerColor)
     c.create_line(widgLen,widgHigh/4,widgLen+10,widgHigh/4,width=1,fill=outerColor)
     c.create_line(widgLen,widgHigh/2,widgLen+10,widgHigh/2,width=1,fill=outerColor)
     c.create_line(widgLen,widgHigh-widgHigh/4,widgLen+10,widgHigh-widgHigh/4,width=1,fill=outerColor)
     c.create_line(widgLen,1,widgLen+10,1,width=1,fill=outerColor)
     c.create_line(widgLen+10,widgHigh,widgLen+10 ,widgHigh,width=1,fill=outerColor)
     c.create_text(widgLen+12,widgHigh,font="Verdana 10",anchor="w",justify=CENTER,fill=outerColor,text='0')
     c.create_text(widgLen+12,10,font="Verdana 10",anchor="w",justify=CENTER,fill=outerColor,text=str(maxValue))
     c.create_text(widgLen+12,widgHigh/2,font="Verdana 10",anchor="w",justify=CENTER,fill=outerColor,text=str(maxValue/2))
     c.create_text(widgLen+12,widgHigh-widgHigh/4,font="Verdana 10",anchor="w",justify=CENTER,fill=outerColor,text=str(maxValue/4))
     c.create_text(widgLen+12,widgHigh/4,font="Verdana 10",anchor="w",justify=CENTER,fill=outerColor,text=str(maxValue-maxValue/4))
     c.create_text(2,widgHigh+15,font="Verdana 12",anchor="w",justify=CENTER,fill=outerColor,text=str(nowValue))

Теперь добавим этот объект в программу

analogFig2=vMeterC(0,320,320,30,300,1000,'yellow','analog Meter')
getAI=master.execute(1, cst.READ_INPUT_REGISTERS, 0, 10)
vMeter(analogFig2[0],getAI[1],320,320,30,300,1000,'yellow','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)

def vMeterC(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(widgHigh) / float(maxValue)
     mesureValue = devValue * nowValue
     c.create_rectangle(1,1,widgLen,widgHigh,fill='black',outline=outerColor)
     c.create_rectangle(widgLen-1,widgHigh,2,widgHigh-int(mesureValue),fill=outerColor,outline=outerColor)
     c.create_line(widgLen,widgHigh,widgLen+10,widgHigh,width=1,fill=outerColor)
     c.create_line(widgLen,widgHigh/4,widgLen+10,widgHigh/4,width=1,fill=outerColor)
     c.create_line(widgLen,widgHigh/2,widgLen+10,widgHigh/2,width=1,fill=outerColor)
     c.create_line(widgLen,widgHigh-widgHigh/4,widgLen+10,widgHigh-widgHigh/4,width=1,fill=outerColor)
     c.create_line(widgLen,y-29,widgLen+10,y-29,width=1,fill=outerColor)
     c.create_line(widgLen+10,widgHigh,widgLen+10 ,widgHigh,width=1,fill=outerColor)
     c.create_text(widgLen+12,widgHigh,font="Verdana 10",anchor="w",justify=CENTER,fill=outerColor,text='0')
     c.create_text(widgLen+12,y-19,font="Verdana 10",anchor="w",justify=CENTER,fill=outerColor,text=str(maxValue))
     c.create_text(widgLen+12,widgHigh/2,font="Verdana 10",anchor="w",justify=CENTER,fill=outerColor,text=str(maxValue/2))
     c.create_text(widgLen+12,widgHigh-widgHigh/4,font="Verdana 10",anchor="w",justify=CENTER,fill=outerColor,text=str(maxValue/4))
     c.create_text(widgLen+12,widgHigh/4,font="Verdana 10",anchor="w",justify=CENTER,fill=outerColor,text=str(maxValue-maxValue/4))
     c.create_text(2,widgHigh+15,font="Verdana 12",anchor="w",justify=CENTER,fill=outerColor,text=str(nowValue))
     return (c,'vmeter',x,y,widgLen,widgHigh,maxValue,outerColor,nameValue)
def vMeter(c,nowValue,x,y,widgLen,widgHigh,maxValue,outerColor,nameValue):
     if(nowValue > maxValue): nowValue=maxValue-1
     devValue=float(widgHigh) / float(maxValue)
     mesureValue = devValue * nowValue
     c.create_rectangle(1,1,widgLen,widgHigh,fill='black',outline=outerColor)
     c.create_rectangle(widgLen-1,widgHigh,2,widgHigh-int(mesureValue),fill=outerColor,outline=outerColor)
     c.create_line(widgLen,widgHigh,widgLen+10,widgHigh,width=1,fill=outerColor)
     c.create_line(widgLen,widgHigh/4,widgLen+10,widgHigh/4,width=1,fill=outerColor)
     c.create_line(widgLen,widgHigh/2,widgLen+10,widgHigh/2,width=1,fill=outerColor)
     c.create_line(widgLen,widgHigh-widgHigh/4,widgLen+10,widgHigh-widgHigh/4,width=1,fill=outerColor)
     c.create_line(widgLen,1,widgLen+10,1,width=1,fill=outerColor)
     c.create_line(widgLen+10,widgHigh,widgLen+10 ,widgHigh,width=1,fill=outerColor)
     c.create_text(widgLen+12,widgHigh,font="Verdana 10",anchor="w",justify=CENTER,fill=outerColor,text='0')
     c.create_text(widgLen+12,10,font="Verdana 10",anchor="w",justify=CENTER,fill=outerColor,text=str(maxValue))
     c.create_text(widgLen+12,widgHigh/2,font="Verdana 10",anchor="w",justify=CENTER,fill=outerColor,text=str(maxValue/2))
     c.create_text(widgLen+12,widgHigh-widgHigh/4,font="Verdana 10",anchor="w",justify=CENTER,fill=outerColor,text=str(maxValue/4))
     c.create_text(widgLen+12,widgHigh/4,font="Verdana 10",anchor="w",justify=CENTER,fill=outerColor,text=str(maxValue-maxValue/4))
     c.create_text(2,widgHigh+15,font="Verdana 12",anchor="w",justify=CENTER,fill=outerColor,text=str(nowValue))


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')
analogFig2=vMeterC(0,320,320,30,300,1000,'yellow','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]))

analogFig[0].delete("all")
analogFig2[0].delete("all")
hMeter(analogFig[0],getAI[0],20,120,300,20,1000,'red','analog Meter')
vMeter(analogFig2[0],getAI[1],320,320,30,300,1000,'yellow','analog Meter')
root.mainloop()

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

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