EVT_CHAR从不调用处理程序

时间:2018-08-23 01:02:14

标签: python wxpython wxwidgets wxpython-phoenix

在自定义控件上接收EVT_CHAR事件时,我仍然遇到很大的麻烦。在互联网上搜索了几天之后,我迷茫了为什么不参加这些活动。

我了解到有关EVT_CHAR的问题很多-我已经阅读了很多问题,但是所提供的解决方案似乎都不起作用。我确保以下几点:

  • 自定义控件具有焦点
  • 我直接绑定事件的来源(自定义控件)
  • 我的自定义控件具有wx.WANTS_CHARS。我尝试将其删除,没有明显的差异。

我尝试过在框架上使用wx.WANTS_CHARS和不使用wx.WANTS_CHARS。 我尝试绑定到框架和控件,无论有没有源参数都指向它们。 没有其他与键盘相关的事件绑定,但是我按照以下代码配置了加速器。

我还尝试扩展wx.TextEntry。我试图追逐的一种可能性是,自定义控件不会收到这些事件,因为它似乎不是文本输入控件,并且该事件仅在非只读/可编辑控件上触发。我没办法弄清楚这一点。

版本信息:
我正在Python 3.7和最常见的3.6上进行测试。
wxPython版本4.0.3
Windows 10.1709企业版

以下是我的框架类中的代码片段,负责构造GUI并侦听这些事件。我还为EVT_CHAR包含了(当前无用的)事件处理程序。

def do_create_gui(self):
    classname = self.__class__.__name__

    app = wx.App()
    AppTitle = "%s: %s" % (self._comms.port, classname)
    size = wx.Size(700, 450)
    frame = wx.Frame(None, title=AppTitle, size=size)
    panel = wx.Panel(frame)
    panelSizer = wx.BoxSizer(wx.VERTICAL)
    sizer = wx.BoxSizer(wx.VERTICAL)

    # Configure Menu
    fileMenu = wx.Menu()
    copyitem = fileMenu.Append(wx.ID_COPY, "&Copy\tCtrl-C")
    pasteitem = fileMenu.Append(wx.ID_PASTE, "&Paste\tCtrl-V")
    fileMenu.AppendSeparator()
    brkitem = fileMenu.Append(wx.ID_ANY, "&Break\tCtrl-B")
    fileMenu.AppendSeparator()
    quititem = fileMenu.Append(wx.ID_EXIT, "&Quit")

    helpMenu = wx.Menu()
    hotkeyitem = helpMenu.Append(wx.ID_ANY, "Program &Shortcuts")

    menubar = wx.MenuBar()
    menubar.Append(fileMenu, '&File')
    menubar.Append(helpMenu, '&Help')
    frame.SetMenuBar(menubar)

    self._terminal = TerminalCtrl(panel)
    self._terminal.SetSpacing(0)
    self._terminal.SetWrap(True)

    sizer.Add(self._terminal, 1, wx.EXPAND)
    panelSizer.Add(panel, 1, wx.EXPAND)
    panel.SetSizer(sizer)
    frame.SetSizer(panelSizer)
    frame.SetMinSize(wx.Size(313, 260))
    frame.Show()

    # Set up accelerators
    accelC = wx.AcceleratorEntry(wx.ACCEL_CTRL, ord('C'), wx.ID_COPY)
    accelV = wx.AcceleratorEntry(wx.ACCEL_CTRL, ord('V'), wx.ID_PASTE)
    accelB = wx.AcceleratorEntry(wx.ACCEL_CTRL, ord('B'), brkitem.GetId())
    accel = wx.AcceleratorTable([accelC, accelV, accelB])
    frame.SetAcceleratorTable(accel)

    # Bind on window events
    frame.Bind(wx.EVT_CLOSE, self.onClose)
    self._terminal.Bind(wx.EVT_CHAR, self.onChar)

    # Bind Menu handlers
    frame.Bind(wx.EVT_MENU, self.onClose, quititem)
    frame.Bind(wx.EVT_MENU, self.showHotkeys, hotkeyitem)
    frame.Bind(wx.EVT_MENU, lambda e: self.onCopy(), copyitem)
    frame.Bind(wx.EVT_MENU, lambda e: self.onPaste(), pasteitem)
    frame.Bind(wx.EVT_MENU, lambda e: self.send_break(), brkitem)

    # Register for events from Serial Communications thread
    EVT_SERIAL(frame, self.onSerialData)

    # Ensure the terminal has focus
    self._terminal.SetFocus()

    self._wxObj = frame
    self._tLock.release()
    app.MainLoop()

def onChar(self, event):
    code = event.GetUnicodeKey()

    if code == wx.WXK_NONE:
        code = event.GetKeyCode()

    if (not 27 < code < 256) or event.HasAnyModifiers():
        # So we don't consume the event
        print('We don\'t process your kind here! (%d)' % code)
        event.Skip()
        return

    print("CHAR: %s(%d)" % (chr(code), code))

关于自定义控件,这是定义。

import wx
from wx.lib.scrolledpanel import ScrolledPanel

class TerminalCtrl(ScrolledPanel, wx.Window):
    def __init__(self, parent, id=wx.ID_ANY, pos=wx.DefaultPosition,
                 size=wx.DefaultSize, style=wx.TAB_TRAVERSAL,
                 name=wx.ControlNameStr):
        pass
    pass

2 个答案:

答案 0 :(得分:2)

我正在尝试做类似的事情,但这对我也不起作用。

根据我的实验,问题是由TAB_TRAVERSAL标志引起的。如果使用此标志创建窗口,则TAB_TRAVERSAL机制似乎会吸收所有EVT_CHAR事件,并且永远不会调用处理程序。这可能是一个错误,但我不确定。

有时会默认设置此标志:对于wx.Panel,这是默认的窗口样式,而对于wx.Window,则不是。在我的测试程序中,当我尝试在面板中获取按键时,它不起作用,但是如果我使用wx.Window,它就可以了。当我更改Panel的样式参数以避免设置TAB_TRAVERSAL标志时,它也起作用。

我还尝试处理EVT_CHAR_HOOK事件,并调用event.DoAllowNextEvent()。文档(我建议使用该术语)说这是必要的,但显然不是必需的。

这是我的测试程序的实际版本。通过试验,您可以(进行排序)弄清发生了什么。注释掉各种选项或从wx.Panel更改为wx.Window很有启发性。

import wx

class MainWindow(wx.Frame):
    def __init__(self):
        super().__init__(None, title="Char test")
        self.label = wx.StaticText(self, label="Label")
        self.panel = DaPanel(self)
        bs = wx.BoxSizer(wx.VERTICAL)
        bs.Add(self.label, wx.SizerFlags().Expand())
        bs.Add(self.panel, wx.SizerFlags(1).Expand())
        self.SetSizerAndFit(bs)
        self.SetAutoLayout(True)
        self.SetSize(wx.Size(800, 600))
        self.Centre()
        self.Show()

        # self.Bind(wx.EVT_CHAR_HOOK, self.do_char_hook)

    def do_char_hook(self, event):
        # The functions DoAllowNextEvent and Skip seem to do the same thing
        event.DoAllowNextEvent()
        event.Skip()
        # Calling either causes the EVT_KEY_DOWN event to occur for default
        # wxPanel but make no difference for wxWindows.

class DaPanel(wx.Window):
    def __init__(self, parent):
        # default style wxPanel won't ever get EVT_CHAR events
        # default style wxWindow will get EVT_CHARs
        super().__init__(parent)
        self.Bind(wx.EVT_CHAR, self.do_char)
        # self.Bind(wx.EVT_KEY_DOWN, self.do_down)

    def do_char(self, event):
        print("char", event.GetUnicodeKey())

    def do_down(self, event):
        print("down", event.GetUnicodeKey(), self.HasFocus())
        # if you run this handler you must Skip()
        # or else no char event ever happens
        event.Skip()

def main():
    app = wx.App()
    mw = MainWindow()
    mw.Show(True)
    app.MainLoop()

main()

答案 1 :(得分:1)

如果焦点窗口没有得到wxEVT_CHAR,则表示某事物正在处理wxEVT_KEY_DOWN而不是跳过它。这似乎并没有发生在代码中的任何地方,所以我唯一的猜测是它是由您使用的ScrolledPanel类完成的。尝试使用普通的wxWindow,您确实应该得到事件。

相关问题