在打开的wx.ComboCtrl中接收击键

编程入门 行业动态 更新时间:2024-10-27 14:19:28
本文介绍了在打开的wx.ComboCtrl中接收击键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

来自这个问题,我有一个wxComboCtrl,一个自定义弹出窗口由一个面板有一堆radiobuttons ..我的问题是,当我打开弹出窗口组合没有得到击键,因为事件得到处理的面板本身..我想将这些KeyEvents重定向到textctrl组合,但我找不到办法让它工作:/ 我错了吗?用户按键时是否应手动处理textctrl值?我认为这将是一个有点麻烦虽然..因为据说textctrl已经知道如何处理这些事件..

Coming from this question, I have a wxComboCtrl with a custom popup made of a panel with a bunch of radiobuttons.. My problem is that when I open the popup the combo doesn't get keystrokes, because the events get handled by the panel itself.. I'd like to redirect those KeyEvents to the textctrl of the combo, but I can't find a way to get it to work :/ Am I going the wrong way? Should I manually handle the textctrl value as the user presses keys? I think that would be a bit cumbersome though.. Since supposedly the textctrl already knows how to handle those events..

这是我的测试用例(wxPython 2.8在Linux上), on_key方法应该是罪魁祸首:

Here's my testcase (wxPython 2.8 on Linux), the "on_key" method should be the culprit:

import wx import wxbo class CustomPopup(wxbo.ComboPopup): def Create(self, parent): # Create the popup with a bunch of radiobuttons self.panel = wx.Panel(parent) sizer = wx.GridSizer(cols=2) for x in range(10): r = wx.RadioButton(self.panel, label="Element "+str(x)) r.Bind(wx.EVT_RADIOBUTTON, self.on_selection) sizer.Add(r) self.panel.SetSizer(sizer) # Handle keyevents self.panel.Bind(wx.EVT_KEY_UP, self.on_key) def GetControl(self): return self.panel def GetAdjustedSize(self, minWidth, prefHeight, maxHeight): return wx.Size(200, 150) def on_key(self, evt): if evt.GetEventObject() is self.panel: # Trying to redirect the key event to the combo.. But this always returns false :( print self.GetCombo().GetTextCtrl().GetEventHandler().ProcessEvent(evt) evt.Skip() def on_selection(self, evt): self.Dismiss() wx.MessageBox("Selection made") class CustomFrame(wx.Frame): def __init__(self): # Toolbar-shaped frame with a ComboCtrl wx.Frame.__init__(self, None, -1, "Test", size=(800,50)) combo = wxbo.ComboCtrl(self) popup = CustomPopup() combo.SetPopupControl(popup) sizer = wx.BoxSizer(wx.VERTICAL) sizer.Add(combo, 0) self.SetSizer(sizer) self.Layout() if __name__ == '__main__': app = wx.PySimpleApp() CustomFrame().Show() app.MainLoop()

编辑: 我发现这些(未解决)的讨论在同一主题.. ComboCopup显示时,ComboCtrl失去了键盘焦点 使用wx.ComboCtrl的问题

推荐答案

我有一个Robin Dunn自己在wxpython用户的答案:

I've got an answer from Robin Dunn himself on wxpython-users:

将低级事件发送到本机这样的小部件不会使大多数人期望的方式。 您期待这样做会导致将字符添加到文本 ctrl中,但它不能这样做,因为所有 ProcessEvent都是发送 wx.Event到任何绑定的事件处理程序然后它停止。它不会去下一步,并将该事件转换为等效的本机消息,将其发送到本机部件。请参阅模拟键盘事件线程。

Sending low-level events to native widgets like this does not work the way that most people expect it to. You are expecting this to result in the character being added to the text ctrl,but it can't do that because all that ProcessEvent does is send the wx.Event to any bound event handlers and then it stops. It does not go to the next step and convert that event to the equivalent native message and send it on to the native widget. See the "Simulating keyboard events" thread.

在非Windows平台上不起作用,但是你可以尝试调用EmulateKeypress。

It doesn't work especially well on non-Windows platforms, but you could try calling EmulateKeypress instead.

更多推荐

在打开的wx.ComboCtrl中接收击键

本文发布于:2023-10-24 09:55:33,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1523635.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:击键   wx   ComboCtrl

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!