繁体   English   中英

如何在wxPython GUI应用程序上运行nfcpy读卡器系统

[英]how to run nfcpy card reader system on wxPython GUI application

我是wxPython GUI应用程序的新手

我想将我的python脚本创建为GUI应用程序

这是我的示例脚本

import binascii
import nfc

class MyCardReader(object):

   def on_connect(self, tag):
      print "touched"
      self.idm = binascii.hexlify(tag.idm)
      return True

   def read_id(self):
      clf = nfc.ContactlessFrontend('usb')
      try:
         clf.connect(rdwr={'on-connect': self.on_connect})
      finally:
         clf.close()

if __name__ == '__main__':
  cr = MyCardReader()
  while True:
    print "touch card:"
    cr.read_id()
    print "released"
    print cr.idm

如何使用wxPython将上述脚本作为GUI应用程序运行,我已经尝试过了,但是没有用,我的代码有什么问题。

#-*- encoding: utf-8 -*-
import wx
import nfc
import binascii
class MyFrame(wx.Frame):

   def __init__(self, parent, title):
      wx.Frame.__init__(self, parent, title=title, size=(400, 500))
      panel = wx.Panel(self)
      self.Show(True)
      while True:
        clf = nfc.ContactlessFrontend('usb')
        clf.connect(rdwr={'on-connect': self.on_connect})
        self.text = wx.StaticText(panel, label='i want to print the return value here', pos=(100, 100))

  def on_connect(self, tag):
      self.idm = binascii.hexlify(tag.idm)
      return self.idm

app = wx.App(False)
frame = MyFrame(None, 'card reader app')
app.MainLoop()

nfc.ContactlessFrontend.connect()方法在调用者线程上下文中运行,因此至少在连接标签之前不会返回(并且如果“ on-connect”回调返回True,它将进一步等待标签消失)。 wx.App.mainLoop()也在调用程序线程上下文中运行以调度窗口事件。 因此,正确的方法是在单独的线程中运行标签发现,并将事件发送到渲染帧。 以下示例显示了标签靠近阅读器时的标签标识符。

#!/usr/bin/env python
import wx
import wx.lib.newevent
import nfc
import threading
from binascii import hexlify

ShowCardEvent, SHOW_CARD_EVENT = wx.lib.newevent.NewEvent()
GoneCardEvent, GONE_CARD_EVENT = wx.lib.newevent.NewEvent()


class TagReader(threading.Thread):
    def __init__(self, wx_frame):
        super(TagReader, self).__init__(name="TagReader")
        self.terminate = False
        self.wx_frame = wx_frame

    def run(self):
        clf = nfc.ContactlessFrontend('usb')
        rdwr_options = {
            'on-connect': self.on_tag_connect,
            'on-release': self.on_tag_release
        }
        while not self.terminate:
            clf.connect(rdwr=rdwr_options, terminate=lambda: self.terminate)

    def on_tag_connect(self, tag):
        wx.PostEvent(self.wx_frame, ShowCardEvent(tag=tag))
        return True

    def on_tag_release(self, tag):
        wx.PostEvent(self.wx_frame, GoneCardEvent())


class Frame(wx.Frame):
    def __init__(self, title):
        super(Frame, self).__init__(None, title=title, size=(500, 200))
        self.text = wx.StaticText(wx.Panel(self), pos=(100, 100))
        self.Bind(SHOW_CARD_EVENT, self.show_card_event)
        self.Bind(GONE_CARD_EVENT, self.gone_card_event)
        self.Bind(wx.EVT_CLOSE, self.close_window_event)
        wx.PostEvent(self, GoneCardEvent())
        self.Show()

    def close_window_event(self, event):
        self.Destroy()

    def show_card_event(self, event):
        self.text.SetLabel("Card ID {}".format(hexlify(event.tag.identifier)))

    def gone_card_event(self, event):
        self.text.SetLabel("no card")


app = wx.App()
reader = TagReader(Frame('card reader app'))
reader.start()
app.MainLoop()
reader.terminate = True  # tell the reader to terminate
reader.join()  # and wait for the reader thread to finish

您已将代码放入无尽的While True循环中,这意味着Gui主循环永远不会运行。
您应该基于来自读卡器的输入来添加event ,或者要开始使用,只需添加一个按钮并在按下按钮时访问读卡器即可。 这将使您快速启动并运行。
这是您的代码,经过调整可在我的计算机上使用,仅用于证明它可以正常工作。
注意self.text.SetLabel()的使用

import wx
#import nfc
import binascii
class MyFrame(wx.Frame):

    def __init__(self, parent, title):
        wx.Frame.__init__(self, parent, title=title, size=(400, 500))
        panel = wx.Panel(self)
        self.text = wx.StaticText(panel, label='i want to print the return value here', pos=(100, 100))
        self.read = wx.Button(panel, -1, label="Read Card", pos=(100, 200))
        self.read.Bind(wx.EVT_BUTTON, self.on_button)
        self.Show()
        self.n = 0

    def on_button(self,event):
#        clf = nfc.ContactlessFrontend('usb')
#        clf.connect(rdwr={'on-connect': self.on_connect})
        self.n+=1
        self.on_connect("abc")
        self.text.SetLabel("Card Value "+self.idm+" "+str(self.n))

    def on_connect(self, tag):
        self.idm = binascii.hexlify(tag)#.idm)
        return self.idm

app = wx.App()
frame = MyFrame(None, 'card reader app')
app.MainLoop()

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM