以前的一些东西:
所有这些都是基于wxTerminal.pyLink
(Pyserial微型端口和wxpythongui的组合)
使用: Python:2.7.14。 WxPython:4.0.0b2
我的问题是我有一个线程从我的设备读取串行数据, 并尝试使用事件更新GUI:
class TerminalFrame(wx.Frame):
....
....
def ComPortThread(self):
"""\
Thread that handles the incoming traffic. Does the basic input
transformation (newlines) and generates an SerialRxEvent
"""
while self.alive.isSet():
b = self.serial.read(self.serial.in_waiting or 1)
if b:
# newline transformation
if self.settings.newline == NEWLINE_CR:
b = b.replace(b'\r', b'\n')
elif self.settings.newline == NEWLINE_LF:
pass
elif self.settings.newline == NEWLINE_CRLF:
b = b.replace(b'\r\n', b'\n')
event = SerialRxEvent(self.GetId(), b)
**ERROR!** >>> self.GetEventHandler().AddPendingEvent(event)
我得到一个错误:
^{pr2}$少了什么?在
SERIALRX = wx.NewEventType()
# bind to serial data receive events
EVT_SERIALRX = wx.PyEventBinder(SERIALRX, 0)
class SerialRxEvent(wx.PyCommandEvent):
eventType = SERIALRX
def __init__(self, windowID, data):
wx.PyCommandEvent.__init__(self, self.eventType, windowID)
self.data = data
def Clone(self):
self.__class__(self.GetId(), self.data)
下面是一个线程事件的(Linux)示例,它可以与python2.7.12(wx3.0)和python3.5.2(wx4.0)一起工作。它使用的是Tcp套接字,而不是串行端口,但我相信您可以从中找出要点。
测试方法:
我只需安装wxpython3.0.2(insted 4.0.0b2)作为示例wxTerminal.py工作
https://sourceforge.net/projects/wxpython/files/wxPython/3.0.2.0/wxPython3.0-win32-3.0.2.0-py27.exe/download
这件事有时也让我感到困惑。在
由于某些原因,
Clone
方法永远不会在wxPython经典中被调用(尝试插入raise
)。pyserial的作者似乎弄错了。根据the docs,Clone
方法应返回一个新事件!在当使用Phoenix(通过安装pip获得的)时,
Clone
方法将被调用。因为您返回None
,AddPendingEvent
会抱怨。在编辑事件类(wxTerminal中的SerialRxEvent)中的Clone方法,使其正确返回:
相关问题 更多 >
编程相关推荐