All pastes #335993 Raw Edit

Stuff

public text v1 · immutable
·published 2007-02-01 19:21 UTC
rendered paste body
class pyvcp_checkbutton(Checkbutton):
    """ (control) a checkbutton 
        halpin is 1 when checkbutton-momentary pressed, 0 otherwise 
           halpin is O (the btn sees it as write only)
        halpin changes state when checkbutton-toggle pressed, unchanged otherwise 
           halpin is IO (the btn can read it and write it  [set it the opp state] )

        20070123 
         added parameter 'btnType' to allowed token list
         allow values "Opin" & "IOpin"
         this preserves the original checkbutton with an 'o' pin
         but allows the 'new' IOpin type
           the IOpin type stays pressed until some xtrnl force releases it
           
        <checkbutton>
          <halpin>"mybtn"</halpin>
          <btnType>"O"</btnType>       halpin is ouput only 
        </checkbutton>
        <checkbutton>
          <halpin>"mybtn"</halpin>     optional  default is created from "checkbutton" & a counter
          <btnType>"IO"</btnType>      optional  default it O 
          <text>"Flood"</init>         optional  default is from a counter
          <init>True</init>            optional  default is True
        </checkbutton>
     """
    #TJP 20070125   only chg the halpin when pressed, not when release, so: dont cnx halpin to self.v!!!

    n=0
    def __init__(self,master,pycomp,halpin=None,btnType=None,text=None,init=None, **kw):

        if btnType==None:
          btnType="Opin"
        if ((btnType!="Opin")&(btnType!="IOpin")):
          return

        if init==None:
          init=True
        if ((init!=True)&(init!=False)):
          return

        if text==None:
          btntxt=str(pyvcp_checkbutton.n)
        else:
          btntxt = text

        self.v = BooleanVar(master)       #for original 'Opin' type checkbutton

        self.downVal=init                 # the logic value to emit when pressed
        self.btntype=btnType              # "Opin" or "IOpin"

        #i dont see why this is necc, else update gives
        #       AttributeError: pyvcp_checkbutton instance has no attribute 'newstate'
        self.newstate=self.oldstate=None

        if btnType=="Opin":
          Checkbutton.__init__(self,master, \
              variable=self.v, \
              text=None, indicatoron=0, \
              onvalue=1, offvalue=0, \
              **kw)
        else:
          Checkbutton.__init__(self,master, \
              text=None, indicatoron=0, \
              **kw)

        if halpin == None:
          halpin = "checkbutton."+str(pyvcp_checkbutton.n)

        self.halpin=halpin

        if btnType=="Opin":
          pycomp.newpin(halpin, HAL_BIT, HAL_OUT)
          self.configure(text=btntxt)
        else:
            pycomp.newpin(halpin, HAL_BIT, HAL_IO)
            self.configure(text=btntxt)

            #show the color according to <init>, set the pin & history to opposite of presed state ( becuz not pressed yet )
            if self.downVal==True:
              colr="red"
              self.configure(activebackground=colr)
              self.configure(bg=colr)
              pycomp[self.halpin]=self.oldstate=self.newstate=False       # if init was True, then begin UP, Red, and oldstate = False
            else:
              colr="green"
              self.configure(activebackground=colr)
              self.configure(selectcolor=colr)
              self.configure(bg=colr)
              pycomp[self.halpin]=self.oldstate=self.newstate=True        # if init was False, then begin UP, Green & oldstate = True
            self.bind('<ButtonPress>',self.bdown)               # only bind the IOpin vrsn of cbtn   chg output state when pressed

        pyvcp_checkbutton.n += 1
        self.mirror=None

    # this func requests a change in state
    def bdown(self,event):
      if self.btntype=="IOpin":
        if self.downVal==True:
          self.newstate=True
        else:
          self.newstate=False
        self.configure(state=DISABLED)

    #def bup(self)
    #   there is no bup, only external srcs can release the btn, not this widget

    def update(self,pycomp):
      if self.btntype=="Opin":
        pycomp[self.halpin]=self.v.get()

      else:                         # btnType = "IO"
        dirty = False                                 # no need to change appearance till we know theres been a change

        if self.newstate!=self.oldstate:          # detect a change caused by btn press ( an internal change )
          pycomp[self.halpin]=self.newstate
          dirty=True                                  # need to chg appearance
          self.oldstate=self.newstate
        elif pycomp[self.halpin]!=self.oldstate:  # detect a change caused by by external src
          dirty=True                                  # need to chg appearance
          self.oldstate=self.newstate=pycomp[self.halpin]


        if dirty==True:
          tmp=pycomp[self.halpin]

          if self.downVal==True:          #    downVal is True
              if tmp==True:               #       halpin is True
                self.select()             #             btn looks down    looks decided by downVal & halpin
                colr="green"              #             colr is green     color decided by halpin
              else:                       #       halpin is False
                self.deselect()           #             btn looks up
                colr="red"                #             colr is red
          else:                           #    downVal is False
              if tmp==True:               #       halpin is True
                self.deselect()           #             btn looks up
                colr="green"              #             colr is green
              else:                       #       halpin is False
                self.select()             #             btn looks down
                colr="red"                #             colr is red

          self.configure(selectcolor=colr)
          self.configure(activebackground=colr)
          self.configure(state=NORMAL)
          dirty=False