微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

wxPython 不会触发重音键事件

如何解决wxPython 不会触发重音键事件

我正在尝试创建一个仅接受某些键的 wx.TextCtrl。我创建了一个验证函数,并使用 wx.EVT_CHAR 将我的 TextCtrl 绑定到该函数。该功能工作正常,除非我尝试按重音键(´、`、~、¨、^)。我在验证函数的开头放了一个 print(event) 以尝试调试它,但是当我按下其中一个键时没有打印任何内容。只是没有触发该事件。

我也试过 wx.EVT_KEY_DOWN,但它也没有触发。

self.foo_input = wx.TextCtrl(self,size=(150,-1))
self.foo_input.Bind(wx.EVT_CHAR,lambda event: validation_foo(event,foo_params))

这是验证函数(将验证 foo 视为此):

import wx

def validate_number_key_press(event,negative=False,decimal=False,scientific_notation=False,imaginary=False,decimal_separator='.'):
    key_code = event.GetUnicodeKey()

    print(event)  # for debug
    if ord('0') <= key_code <= ord('9'):
        event.Skip()
        return

    if negative and key_code == ord('-'):
        event.Skip()
        return

    if decimal and key_code == ord(decimal_separator):
        event.Skip()
        return

    if scientific_notation and key_code == ord('e'):
        event.Skip()
        return

    if imaginary and key_code == ord('j'):
        event.Skip()
        return

    # Navigation and deletion
    if key_code in (wx.WXK_BACK,wx.WXK_DELETE,wx.WXK_NUMPAD_DELETE,wx.WXK_TAB,wx.WXK_NUMPAD_TAB,wx.WXK_LEFT,wx.WXK_RIGHT,wx.WXK_NUMPAD_LEFT,wx.WXK_NUMPAD_RIGHT,wx.WXK_HOME,wx.WXK_NUMPAD_HOME,wx.WXK_END,wx.WXK_NUMPAD_END):
        print('Navigation and deletion')  # for debug
        event.Skip()
        return

    return

解决方法

因为您只给了我们一小段代码,请使用以下内容进行测试:
(测试 ["~","`","^"] 作为定义的重音键)看看你是否还有问题。

import wx

class DemoFrame(wx.Frame):

    def __init__(self):
        wx.Frame.__init__(self,None,wx.ID_ANY,"Evt_Char Test",size=(600,400)
                          )
        foo_params = ["~","^"]
        panel = wx.Panel(self)
        text  = wx.TextCtrl(panel,-1,pos=(10,10))
        text.Bind(wx.EVT_CHAR,self.character)
        text.Bind(wx.EVT_CHAR,lambda event: self.validation_foo(event,foo_params))

    def character(self,event):
        print ("Char keycode : {0}".format(event.GetKeyCode()))
        print ('%c' % event.GetUnicodeKey())
        event.Skip()

    def validation_foo(self,event,params=None):
        print ("Validation keycode : {0}".format(event.GetKeyCode()))
        k = event.GetUnicodeKey()
        if chr(k) in params:
            print("**** Accent key pressed ****",chr(k))

        event.Skip()
        
if __name__ == "__main__":
    app = wx.App()
    mainFrame = DemoFrame()
    mainFrame.Show()
    app.MainLoop()
,

您是否尝试过使用 wx.EVT_CHAR 事件?

例如:

#!/usr/bin/env python
# -*- coding: UTF-8 -*-

import wx


class MainFrame(wx.Frame):
    def __init__(self,*args,**kwds):
        # MainFrame.__init__
        kwds["style"] = kwds.get("style",0) | wx.DEFAULT_FRAME_STYLE
        wx.Frame.__init__(self,**kwds)
        self.SetSize((506,373))
        self.txtInput = wx.TextCtrl(self,style=wx.TE_PROCESS_ENTER)
        self.btnClear = wx.Button(self,"Clear")
        self.txtResult = wx.TextCtrl(self,"",style=wx.TE_MULTILINE)        

        self.__set_properties()
        self.__do_layout()

        self.btnClear.Bind(wx.EVT_BUTTON,self.OnBtnClear)
        self.txtInput.Bind(wx.EVT_CHAR,lambda event: self.OnTextEnter(event,[]))
        

    def __set_properties(self):
        
        self.SetTitle("Test App")
        self.SetBackgroundColour(wx.Colour(38,117,157))
        

    def __do_layout(self):        
        sizer_1 = wx.BoxSizer(wx.VERTICAL)
        main_sizer = wx.FlexGridSizer(2,1,0)
        grid_sizer_1 = wx.FlexGridSizer(1,3,0)
        label_1 = wx.StaticText(self,"Write something:")
        label_1.SetForegroundColour(wx.Colour(255,255,255))
        grid_sizer_1.Add(label_1,wx.ALIGN_CENTER_VERTICAL | wx.LEFT,10)
        grid_sizer_1.Add(self.txtInput,wx.ALL | wx.EXPAND,10)
        grid_sizer_1.Add(self.btnClear,wx.ALIGN_CENTER_VERTICAL | wx.RIGHT,10)
        grid_sizer_1.AddGrowableCol(1)
        main_sizer.Add(grid_sizer_1,wx.EXPAND,0)
        main_sizer.Add(self.txtResult,0)
        main_sizer.AddGrowableRow(1)
        main_sizer.AddGrowableCol(0)
        sizer_1.Add(main_sizer,0)
        self.SetSizer(sizer_1)
        self.Layout()
        self.Centre()       

    def OnTextEnter(self,params):        
        self.txtResult.AppendText("You have pressed the key: {}\n".format(event.GetUnicodeKey()))      
        event.Skip()

    def OnBtnClear(self,event):
        self.txtResult.Clear()

# end of class MainFrame

class MyApp(wx.App):
    def OnInit(self):
        self.mainFrame = MainFrame(None,"")
        self.SetTopWindow(self.mainFrame)
        self.mainFrame.Show()
        return True

# end of class MyApp

if __name__ == "__main__":
    app = MyApp(0)
    app.MainLoop()

当您写入任何这些字符(`,¨,´,...)时,将调用事件处理程序。但请记住,要编写它们,总是需要在它们后面加上一个空格。

因此,您只需检查引入的字符是否不是这些数字之一

enter image description here

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。