diff options
author | Chris Liechti <cliechti@gmx.net> | 2020-09-15 01:23:11 +0200 |
---|---|---|
committer | Chris Liechti <cliechti@gmx.net> | 2020-09-15 01:23:11 +0200 |
commit | 9da0f0f4c4d214331e531c4939d2b6b46dab6725 (patch) | |
tree | 20f3806820026894e049d4c52e0ed7fee23f1151 | |
parent | c1cd16ededb0d9e163209803f4b0f429dbf06753 (diff) | |
download | pyserial-git-9da0f0f4c4d214331e531c4939d2b6b46dab6725.tar.gz |
examples: refactor wx example, use Bind to avoid deprecated warnings, IsChecked, unichr
- wxTerminal: update event handling. sending still with issues, only sending capital letters
fixes #443
fixes #444
-rwxr-xr-x | examples/wxSerialConfigDialog.py | 6 | ||||
-rwxr-xr-x | examples/wxTerminal.py | 33 |
2 files changed, 17 insertions, 22 deletions
diff --git a/examples/wxSerialConfigDialog.py b/examples/wxSerialConfigDialog.py index 0064a9c..1901d77 100755 --- a/examples/wxSerialConfigDialog.py +++ b/examples/wxSerialConfigDialog.py @@ -202,10 +202,10 @@ class SerialConfigDialog(wx.Dialog): # end wxGlade def __attach_events(self): - wx.EVT_BUTTON(self, self.button_ok.GetId(), self.OnOK) - wx.EVT_BUTTON(self, self.button_cancel.GetId(), self.OnCancel) + self.button_ok.Bind(wx.EVT_BUTTON, self.OnOK) + self.button_cancel.Bind(wx.EVT_BUTTON, self.OnCancel) if self.show & SHOW_TIMEOUT: - wx.EVT_CHECKBOX(self, self.checkbox_timeout.GetId(), self.OnTimeout) + self.checkbox_timeout.Bind(wx.EVT_CHECKBOX, self.OnTimeout) def OnOK(self, events): success = True diff --git a/examples/wxTerminal.py b/examples/wxTerminal.py index 0811721..08c9ee9 100755 --- a/examples/wxTerminal.py +++ b/examples/wxTerminal.py @@ -7,31 +7,26 @@ # SPDX-License-Identifier: BSD-3-Clause import codecs +from serial.tools.miniterm import unichr import serial import threading import wx +import wx.lib.newevent import wxSerialConfigDialog +try: + unichr +except NameError: + unichr = chr + # ---------------------------------------------------------------------- # Create an own event type, so that GUI updates can be delegated # this is required as on some platforms only the main thread can # access the GUI without crashing. wxMutexGuiEnter/wxMutexGuiLeave # could be used too, but an event is more elegant. +SerialRxEvent, EVT_SERIALRX = wx.lib.newevent.NewEvent() 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) # ---------------------------------------------------------------------- @@ -215,6 +210,7 @@ class TerminalFrame(wx.Frame): self.Bind(wx.EVT_MENU, self.OnPortSettings, id=ID_SETTINGS) self.Bind(wx.EVT_MENU, self.OnTermSettings, id=ID_TERM) self.text_ctrl_output.Bind(wx.EVT_CHAR, self.OnKey) + self.Bind(wx.EVT_CHAR_HOOK, self.OnKey) self.Bind(EVT_SERIALRX, self.OnSerialRead) self.Bind(wx.EVT_CLOSE, self.OnClose) @@ -304,8 +300,8 @@ class TerminalFrame(wx.Frame): serial port. Newline handling and local echo is also done here. """ code = event.GetUnicodeKey() - if code < 256: # XXX bug in some versions of wx returning only capital letters - code = event.GetKeyCode() + # if code < 256: # XXX bug in some versions of wx returning only capital letters + # code = event.GetKeyCode() if code == 13: # is it a newline? (check for CR which is the RETURN key) if self.settings.echo: # do echo if needed self.text_ctrl_output.AppendText('\n') @@ -320,6 +316,7 @@ class TerminalFrame(wx.Frame): if self.settings.echo: # do echo if needed self.WriteText(char) self.serial.write(char.encode('UTF-8', 'replace')) # send the character + event.StopPropagation() def WriteText(self, text): if self.settings.unprintable: @@ -345,21 +342,19 @@ class TerminalFrame(wx.Frame): pass elif self.settings.newline == NEWLINE_CRLF: b = b.replace(b'\r\n', b'\n') - event = SerialRxEvent(self.GetId(), b) - self.GetEventHandler().AddPendingEvent(event) + wx.PostEvent(self, SerialRxEvent(data=b)) def OnRTS(self, event): # wxGlade: TerminalFrame.<event_handler> self.serial.rts = event.IsChecked() def OnDTR(self, event): # wxGlade: TerminalFrame.<event_handler> - self.serial.dtr = event.Checked() + self.serial.dtr = event.IsChecked() # end of class TerminalFrame class MyApp(wx.App): def OnInit(self): - wx.InitAllImageHandlers() frame_terminal = TerminalFrame(None, -1, "") self.SetTopWindow(frame_terminal) frame_terminal.Show(True) |