2017-09-25 66 views
0

我是新來的wxPython的GUI應用程序如何在wxPython的GUI應用程序運行nfcpy讀卡系統

我想創建我的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() 

回答

1

nfc.ContactlessFrontend.connect()方法呼叫者線程上下文內操作,從而至少一個標籤已經連接不返回之前(並且如果「上-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 
+0

非常感謝您的解釋斯蒂芬。這就是我想要做的。 –

+0

@HAUMUNTUANG如果這回答你的問題,請記住點擊左邊灰色的'Tick'標記來接受它。 –

0

你已失去你的代碼進入無限循環While True,這意味着桂主循環永遠不會運行。
您應該根據來自讀卡器的輸入添加一個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() 

enter image description here

+0

謝謝@薩克森爵士 –

相關問題