我刚开始学习 Python,所以我尽量避免在 Python 2 中工作太多。目前正在使用 wxPython 学习 GUI 元素。 Python 3 文档还没有介绍部分,因此我使用 Python 2“入门”文档并在需要时转换为 Python 3。
我目前在 this section .当在焦点对象上检测到按键时,有一个 wx.EVT_CHAR 部分用于事件处理。我在 comparison chart 中没有看到对它的引用,或在 CommandEvent docs ,或在 wx.TextCtrl docs 的“此类发出的事件”部分.我已经能够转换大多数其他非 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() 将在每次文本字段中的文本更改时触发。 OnKeyPress 从不触发。但是,如果我确实启动了它,我在 CommandEvent methods summary 中看不到等效的 GetKeyCode() .
编辑: 感谢 Mike Driscoll 解决了问题.我实现了他的改变,即改变这个:
self.Bind(wx.EVT_CHAR, self.OnKeyPress, self.textfield)
为此:
self.textfield.Bind(wx.EVT_CHAR, self.OnKeyPress, self.textfield)
我还必须将 e.Skip() 添加到 OnKeyPress 函数中。否则它会记录 key ,但不会向文本字段添加文本。其他事件没有 Skip()ing 将事件向上传递给其他监听器的控制树。
请您参考如下方法:
在这种情况下,您只是错误地绑定(bind)了事件。你想要
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()
您可能想阅读以下有关不同绑定(bind)方法的 wiki 条目:
至于您的其他问题,虽然我不能确定,但如果 Phoenix 中不存在 OnKeyPress,我会感到非常惊讶。即使它不在文档中,我也会尝试一下,因为我猜它仍然在那里。
