views:

441

answers:

3

Writing a test app to emulate PIO lines, I have a very simple Python/Tk GUI app. Using the numeric Keys 1 to 8 to simulate PIO pins 1 to 8. Press the key down = PIO High, release the Key = PIO goes low. What I need it for is not the problem. I kind of went down a rabbit hole trying to use a factory to create the key press call back functions.

Here is some stripped down code:

#!usr/bin/env python
"""
Python + Tk GUI interface to simulate a 8 Pio lines.
"""

from Tkinter import *

def cb_factory(numberic_key):
    """
    Return a call back function for a specific keyboard numeric key (0-9)
    """
    def cb( self, event, key=numberic_key ):
        bit_val = 1<<numberic_key-1
        if int(event.type) == 2 and not (bit_val & self.bitfield):
            self.bitfield |= bit_val
            self.message("Key %d Down" % key)
        elif int(event.type) == 3 and (bit_val & self.bitfield):
            self.bitfield &= (~bit_val & 0xFF)
            self.message("Key %d Up" % key)
        else:
            # Key repeat
            return
        print hex(self.bitfield)
        self.display_bitfield()
    return cb

class App( Frame ):
    """
    Main TK App class 
    """

    cb1 = cb_factory(1)
    cb2 = cb_factory(2)
    cb3 = cb_factory(3)
    cb4 = cb_factory(4)
    cb5 = cb_factory(5)
    cb6 = cb_factory(6)
    cb7 = cb_factory(7)
    cb8 = cb_factory(8)

    def __init__(self, parent):
        "Init"
        self.parent = parent
        self.bitfield = 0x00
        Frame.__init__(self, parent)

        self.messages = StringVar()
        self.messages.set("Initialised")

        Label( parent, bd=1, 
               relief=SUNKEN, 
               anchor=W, 
               textvariable=self.messages,
               text="Testing" ).pack(fill=X)

        self.bf_label = StringVar()
        self.bf_label.set("0 0 0 0 0 0 0 0")

        Label( parent, bd=1, 
               relief=SUNKEN, 
               anchor=W, 
               textvariable=self.bf_label,
               text="Testing" ).pack(fill=X)

 # This Doesn't work! Get a traceback saying 'cb' expected 2 arguements
 # but only got 1?
 #
 #       for x in xrange(1,9):
 #           cb = self.cb_factory(x)
 #           self.parent.bind("<KeyPress-%d>" % x, cb) 
 #           self.parent.bind("<KeyRelease-%d>" % x, cb) 

        self.parent.bind("<KeyPress-1>", self.cb1)
        self.parent.bind("<KeyRelease-1>", self.cb1)

        self.parent.bind("<KeyPress-2>", self.cb2)
        self.parent.bind("<KeyRelease-2>", self.cb2)

        self.parent.bind("<KeyPress-3>", self.cb3)
        self.parent.bind("<KeyRelease-3>", self.cb3)

        self.parent.bind("<KeyPress-4>", self.cb4)
        self.parent.bind("<KeyRelease-4>", self.cb4)

        self.parent.bind("<KeyPress-5>", self.cb5)
        self.parent.bind("<KeyRelease-5>", self.cb5)

        self.parent.bind("<KeyPress-6>", self.cb6)
        self.parent.bind("<KeyRelease-6>", self.cb6)

        self.parent.bind("<KeyPress-7>", self.cb7)
        self.parent.bind("<KeyRelease-7>", self.cb7)

        self.parent.bind("<KeyPress-8>", self.cb8)
        self.parent.bind("<KeyRelease-8>", self.cb8)


    def display_bitfield(self):
        """
        Display the PIO lines (1 for on, 0 for off)
        """
        bin_lst = []
        for x in xrange(8):
            bit = 1 << x
            if bit & self.bitfield:
                bin_lst.append("1")
            else:
                bin_lst.append("0")
        bin_lst.reverse()
        bin_str = " ".join( bin_lst )
        self.bf_label.set( bin_str )

    def message( self, msg_txt ):
        "set"
        self.messages.set( msg_txt )

    def cb_factory(self,  numberic_key ):
        """
        Return a call back function for a specific keyboard numeric key (0-9)
        """
        def cb( self, event, key=numberic_key ):
            bit_val = 1<<numberic_key-1
            if int(event.type) == 2:
                self.bitfield |= bit_val
                self.message("Key %d Down" % key)
            else:
                self.bitfield &= (~bit_val & 0xFF)
                self.message("Key %d Up" % key)
            print hex(self.bitfield)
            self.display_bitfield()
        return cb

##########################################################################

if __name__ == "__main__":

    root = Tk()
    root.title("PIO Test")
    theApp = App( root )

    root.mainloop()

I finally got some sort of method factory working for the callback but I don't find it very satisfactory.

So my question is, can you have a class method factory, that will produce class methods the way I tried (see commented out code and App class method cb_factory())?

NOTES: Yes, I know that this app only lets you hold down 4 keys at a time, but that is good enough for my purposes.

+1  A: 

cb expects 'self' and 'event'. Maybe it only gets event from the bind?

SpliFF
Yep, that was it! The 'self' was superfluous in the 'cb' method returned by 'cb_factory'. I will post the amended code to show the working example.Now if someone would kindly explain this to me, my brain is hurting!
Pev
A: 

Here is the amended code, taking into account SpliFF's answer. I find this much more aesthetically pleasing but it bugs me that I don't understand how it works. So, for extra credit, can anyone explain how this does work?

#!usr/bin/env python
"""
Python + Tk GUI interface to simulate a 8 Pio lines.
"""

from Tkinter import *
from pio_handler import *

class App( Frame ):
    """
    Main TK App class 
    """

    def __init__(self, parent):
        "Init"
        self.parent = parent
        self.bitfield = 0x00
        Frame.__init__(self, parent)

        self.messages = StringVar()
        self.messages.set("Initialised")

        Label( parent, bd=1, 
               relief=SUNKEN, 
               anchor=W, 
               textvariable=self.messages,
               text="Testing" ).pack(fill=X)

        self.bf_label = StringVar()
        self.bf_label.set("0 0 0 0 0 0 0 0")

        Label( parent, bd=1, 
               relief=SUNKEN, 
               anchor=W, 
               textvariable=self.bf_label,
               text="Testing" ).pack(fill=X)

        # This is the clever bit!
        # Use a factory to assign a callback function for keys 1 to 8
        for x in xrange(1,9):
            cb = self.cb_factory(x)
            self.parent.bind("<KeyPress-%d>" % x, cb) 
            self.parent.bind("<KeyRelease-%d>" % x, cb) 

    def display_bitfield(self):
        """
        Display the PIO lines (1 for on, 0 for off)
        """
        bin_lst = []
        for x in xrange(8):
            bit = 1 << x
            if bit & self.bitfield:
                bin_lst.append("1")
            else:
                bin_lst.append("0")
        bin_lst.reverse()
        bin_str = " ".join( bin_lst )
        self.bf_label.set( bin_str )

    def message( self, msg_txt ):
        "set"
        self.messages.set( msg_txt )

    def cb_factory(self,  numeric_key ):
        """
        Return a call back function for a specific keyboard numeric key (0-9)
        """
        def cb( event, key=numeric_key ):
            bit_val = 1<<numeric_key-1
            if int(event.type) == 2:
                self.bitfield |= bit_val
                self.message("Key %d Down" % key)
            else:
                self.bitfield &= (~bit_val & 0xFF)
                self.message("Key %d Up" % key)
            print hex(self.bitfield)
            self.display_bitfield()
        return cb

##########################################################################

if __name__ == "__main__":

    root = Tk()
    root.title("PIO Test")
    theApp = App( root )

    root.mainloop()
Pev
A: 

In answer to your followup question.

I'm not sure which part you don't understand but I'm guessing you don't quite have a handle on how event callbacks work? If so it's pretty easy. Tk runs in a loop looking for events (keypresses, mouseclicks, etc..). When you bind a callback/function to a event you're just telling it to call your function and pass an event object as it's argument. You can then query the event object for more details of what actually occurred. You are currently building seperate callback functions and binding each to 18 key events (down and release on keys 1-9). I think you can rewrite this to simply have cb as a method of your class because the event object will almost certainly contain the keycode as well.

class:
  def __init__(self):
    for x in xrange(8):
      self.parent.bind("<KeyPress-%d>" % x, self.keyaction)
      self.parent.bind("<KeyRelease-%d>" % x, self.keyaction)

  def keyaction(self, event):
    key = event.keycode # attribute may have another name, I haven't checked tk docs
    ... do stuff ...

Since we're now using self.keyaction as the callback it should get self as its first argument. It gets its keycode from the event object. Now neither value needs to be 'built' into the function when the function is created so the need to actually create different callbacks for each key is removed and the code easier to understand.

SpliFF
It's key = event.charBut I was more interested in why the 'cb' method produced by the factory doesn't need 'self' as it's first parameter.
Pev
because it inherited the meaning of self from the factory function, almost like a global variable. self isn't defined in cb but it is defined in the scope above (the object method)
SpliFF