1

编写一个测试应用程序来模拟 PIO 行,我有一个非常简单的 Python/Tk GUI 应用程序。使用数字键 1 到 8 来模拟 PIO 引脚 1 到 8。按下按键 = PIO 高电平,松开按键 = PIO 变为低电平。我需要它不是问题。我有点陷入了一个兔子洞,试图使用工厂来创建按键回调函数。

这是一些精简的代码:

#!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()

我终于得到了某种方法工厂来为回调工作,但我觉得它不是很令人满意。

所以我的问题是,你能有一个类方法工厂,它会按照我尝试的方式生成类方法(参见注释掉的代码和 App 类方法 cb_factory())?

注意:是的,我知道这个应用程序一次只能按住 4 个键,但这对于我的目的来说已经足够了。

4

3 回答 3

1

cb 期望“自我”和“事件”。也许它只从绑定中获取事件?

于 2009-05-26T08:25:04.697 回答
1

回答您的后续问题。

我不确定您不了解哪一部分,但我猜您不太了解事件回调的工作原理?如果是这样,这很容易。Tk 在循环中运行以查找事件(按键、鼠标点击等)。当您将回调/函数绑定到事件时,您只是告诉它调用您的函数并将事件对象作为参数传递。然后,您可以查询事件对象以获取有关实际发生的事件的更多详细信息。您当前正在构建单独的回调函数并将每个函数绑定到 18 个键事件(在键 1-9 上按下和释放)。我认为您可以将其重写为简单地将 cb 作为您的类的方法,因为事件对象几乎肯定也会包含键码。

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 ...

由于我们现在使用self .keyaction 作为回调,它应该将 self 作为它的第一个参数。它从事件对象中获取其键码。现在,在创建函数时,两个值都不需要“内置”到函数中,因此不再需要为每个键实际创建不同的回调,并且代码更易于理解。

于 2009-05-26T14:03:10.257 回答
0

这是修改后的代码,考虑到 SpliFF 的回答。我发现这在美学上更令人愉悦,但我不明白它是如何工作的,这让我很烦恼。所以,为了额外的功劳,谁能解释这是如何工作的?

#!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()
于 2009-05-26T12:50:31.540 回答