有没有.EVT_CHAR或event.GetKeyCode()等价于wxPython(Phoenix)



我刚开始学习Python,所以我尽量避免在Python 2中工作太多。目前正在用wxPython学习GUI元素。Python 3文档还没有介绍部分,所以我使用Python 2的"入门"文档,并在需要的地方转换为Python 3。

我目前在这个部分。有一个部分是wx。EVT_CHAR用于在聚焦对象上检测到按键时的事件处理。我在比较图表、CommandEvent文档或wx的"该类发出的事件"部分中都没有看到对它的引用。TextCtrl文档。我已经能够转换大多数其他非python 3代码,如SizerFlags,但我找不到等效的。

这是我正在做的。

import wx
class ExampleFrame(wx.Frame):
    def __init__(self, parent):
        wx.Frame.__init__(self, parent)
        baseSizer = wx.BoxSizer(wx.VERTICAL)
        # Create an editable text field
        self.textfield = wx.TextCtrl(self)
        # Attach event handlers to text field
        # Event for when the text changes
        self.Bind(wx.EVT_TEXT, self.OnChange, self.textfield)
        # Event for when a key is pressed, for example an arrow key should fire this event but not the EVT_TEXT event
        self.Bind(wx.EVT_CHAR, self.OnKeyPress, self.textfield)
        # Create a button that will clear the textfield
        clearButton = wx.Button(self, wx.ID_CLEAR, "Clear")
        # Attach event handler on the clearButton to call OnClear()
        self.Bind(wx.EVT_BUTTON, self.OnClear, clearButton)
        # Multiline text field for seeing the events fire
        self.logger = wx.TextCtrl(self, -1, style= wx.TE_MULTILINE | wx.TE_READONLY )
        # Add items to frame sizer
        baseSizer.Add(self.textfield, wx.SizerFlags(0).Expand())
        baseSizer.Add(clearButton, wx.SizerFlags(0).Expand())
        baseSizer.Add(self.logger, wx.SizerFlags(1).Expand())
        # Set sizer for frame
        self.SetSizer(baseSizer)
        # Show 
        self.Show()
    def OnClear(self, e):
        # Clear all text entered into the textfield and return focus
        self.textfield.SetValue("")
        self.textfield.SetFocus()
    def OnChange(self, e):
        # Log every time this event is fired
        self.logger.AppendText("OnChange: " + e.GetString() + 'n')
    def OnKeyPress(self, e):
        # Log every key press in the textfield
        self.logger.AppendText("OnKeyPress: " + e.GetKeyCode() + 'n')
app = wx.App(False)
ExampleFrame(None)
app.MainLoop()

OnChange()将在每次textfield中的文本更改时触发。OnKeyPress从不开火。如果我确实让它触发,但我没有看到GetKeyCode()在commanddevent方法摘要等效。

编辑:多亏了Mike Driscoll,问题解决了。我执行了他的更改,即更改以下内容:

self.Bind(wx.EVT_CHAR, self.OnKeyPress, self.textfield)

to this:

self.textfield.Bind(wx.EVT_CHAR, self.OnKeyPress, self.textfield)  

我还必须将e.Skip()添加到OnKeyPress函数中。否则,它会记录键,但不会向文本字段添加文本。如果没有Skip()通过控制树将事件传递给其他侦听器,其他事件也可以正常运行。

在这种情况下,您只是错误地绑定了事件。你想要

self.textfield.Bind(wx.EVT_CHAR, self.OnKeyPress, self.textfield)

代替

self.Bind(wx.EVT_CHAR, self.OnKeyPress, self.textfield)

下面是上下文:

import wx
class ExampleFrame(wx.Frame):
    def __init__(self, parent):
        wx.Frame.__init__(self, parent)
        baseSizer = wx.BoxSizer(wx.VERTICAL)
        # Create an editable text field
        self.textfield = wx.TextCtrl(self)
        # Attach event handlers to text field
        # Event for when the text changes
        self.Bind(wx.EVT_TEXT, self.OnChange, self.textfield)
        # Event for when a key is pressed, for example an arrow key should fire this event but not the EVT_TEXT event
        self.textfield.Bind(wx.EVT_CHAR, self.OnKeyPress, self.textfield)
        # Create a button that will clear the textfield
        clearButton = wx.Button(self, wx.ID_CLEAR, "Clear")
        # Attach event handler on the clearButton to call OnClear()
        self.Bind(wx.EVT_BUTTON, self.OnClear, clearButton)
        # Multiline text field for seeing the events fire
        self.logger = wx.TextCtrl(self, -1, style= wx.TE_MULTILINE | wx.TE_READONLY )
        # Add items to frame sizer
        baseSizer.Add(self.textfield, 1, flag=wx.EXPAND)
        baseSizer.Add(clearButton,0)
        baseSizer.Add(self.logger, 1, flag=wx.EXPAND)
        # Set sizer for frame
        self.SetSizer(baseSizer)
        # Show 
        self.Show()
    def OnClear(self, e):
        # Clear all text entered into the textfield and return focus
        self.textfield.SetValue("")
        self.textfield.SetFocus()
        e.Skip()
    def OnChange(self, e):
        # Log every time this event is fired
        self.logger.AppendText("OnChange: " + e.GetString() + 'n')
        e.Skip()
    def OnKeyPress(self, e):
        # Log every key press in the textfield
        self.logger.AppendText("OnKeyPress: " + str(e.GetKeyCode()) + 'n')
        e.Skip()
app = wx.App(False)
ExampleFrame(None)
app.MainLoop()

您可能需要阅读以下关于不同绑定方法的wiki条目:

  • http://wiki.wxpython.org/self.Bind%20vs.%20self.button.Bind

关于你的另一个问题,虽然我不能确定,但如果OnKeyPress在凤凰城不存在,我会非常惊讶。我会尝试一下即使它不在文档中因为我猜它还在那里

相关内容

  • 没有找到相关文章

最新更新