wx.EVT_KEY_DOWN - evt.skip() 不起作用
wx.EVT_KEY_DOWN - evt.skip() doesn't work
我在 wxpython 中创建了一个图形用户界面。我尝试使用 wx.EVT_KEY_DOWN。当我按下一个键时,我想知道该键是否改变了 messageTxt(它不是一个键,例如 right、shift、alt 等...)。
我在 evt.Skip() 之前和之后打印了 messageTxt,但它没有改变,只有在第二个字符中我才能看到最后的变化。有人知道如何在 evt.Skip() 之后获取新的 messageTxt 吗?
这样,我就可以比较跳过之前和之后的文本,并得到有变化的结果。
这是一些解释问题的代码。
import wx
from wx.stc import StyledTextCtrl
def On_KeyDown(evt):
x, y = messageTxt.GetSelection()
# If something is selected, de-select it
if x != y:
messageTxt.SetEmptySelection(y)
else:
print("Before skipping", messageTxt.GetText())
evt.Skip()
print("After skipping", messageTxt.GetText())
app = wx.App()
frame = wx.Frame(None, -1, title='2', pos=(0, 0), size=(500, 500))
frame.Show(True)
messageTxt = StyledTextCtrl(frame, id=wx.ID_ANY, pos=(0, 0), size=(100 * 3, 100),
style=wx.TE_MULTILINE, name="File")
messageTxt.Bind(wx.EVT_KEY_DOWN, On_KeyDown)
app.SetTopWindow(frame)
app.MainLoop()
你的问题是 EVT_KEY_DOWN
工作。
键按下事件发生在键本身被识别之前。
您应该检查 EVT_KEY_UP
然后测试您的 special
键。
import wx
from wx.stc import StyledTextCtrl
def On_KeyDown(evt):
x, y = messageTxt.GetSelection()
# If something is selected, de-select it
if x != y:
messageTxt.SetEmptySelection(y)
else:
evt.Skip()
def On_KeyUp(evt):
print("Text :", messageTxt.GetText())
k = evt.GetKeyCode()
if k in (wx.WXK_SHIFT,wx.WXK_ALT,wx.WXK_CONTROL,wx.WXK_DOWN):
print('Special key')
evt.Skip()
app = wx.App()
frame = wx.Frame(None, -1, title='2', pos=(0, 0), size=(500, 500))
frame.Show(True)
messageTxt = StyledTextCtrl(frame, id=wx.ID_ANY, pos=(0, 0), size=(100 * 3, 100),
style=wx.TE_MULTILINE, name="File")
messageTxt.Bind(wx.EVT_KEY_DOWN, On_KeyDown)
messageTxt.Bind(wx.EVT_KEY_UP, On_KeyUp)
app.SetTopWindow(frame)
app.MainLoop()
然而,您需要将这些功能分成 wx.EVT_KEY_UP 和 wx.EVT_KEY_DOWN 功能,否则它们将相互交叉工作,即密钥更改测试将工作并且选择失败或副-反之亦然。
你可能误会了wx.event.Skip()
Skip(self, skip=True)
This method can be used inside an event handler to control whether further event handlers bound to this event will be called after the current one returns.
Without Skip (or equivalently if Skip(false) is used), the event will not be processed any more. If Skip(true) is called, the event processing system continues searching for a further handler function for this event, even though it has been processed already in the current handler.
In general, it is recommended to skip all non-command events to allow the default handling to take place. The command events are, however, normally not skipped as usually a single command such as a button click or menu item selection must only be processed by one handler.
我在 wxpython 中创建了一个图形用户界面。我尝试使用 wx.EVT_KEY_DOWN。当我按下一个键时,我想知道该键是否改变了 messageTxt(它不是一个键,例如 right、shift、alt 等...)。 我在 evt.Skip() 之前和之后打印了 messageTxt,但它没有改变,只有在第二个字符中我才能看到最后的变化。有人知道如何在 evt.Skip() 之后获取新的 messageTxt 吗? 这样,我就可以比较跳过之前和之后的文本,并得到有变化的结果。 这是一些解释问题的代码。
import wx
from wx.stc import StyledTextCtrl
def On_KeyDown(evt):
x, y = messageTxt.GetSelection()
# If something is selected, de-select it
if x != y:
messageTxt.SetEmptySelection(y)
else:
print("Before skipping", messageTxt.GetText())
evt.Skip()
print("After skipping", messageTxt.GetText())
app = wx.App()
frame = wx.Frame(None, -1, title='2', pos=(0, 0), size=(500, 500))
frame.Show(True)
messageTxt = StyledTextCtrl(frame, id=wx.ID_ANY, pos=(0, 0), size=(100 * 3, 100),
style=wx.TE_MULTILINE, name="File")
messageTxt.Bind(wx.EVT_KEY_DOWN, On_KeyDown)
app.SetTopWindow(frame)
app.MainLoop()
你的问题是 EVT_KEY_DOWN
工作。
键按下事件发生在键本身被识别之前。
您应该检查 EVT_KEY_UP
然后测试您的 special
键。
import wx
from wx.stc import StyledTextCtrl
def On_KeyDown(evt):
x, y = messageTxt.GetSelection()
# If something is selected, de-select it
if x != y:
messageTxt.SetEmptySelection(y)
else:
evt.Skip()
def On_KeyUp(evt):
print("Text :", messageTxt.GetText())
k = evt.GetKeyCode()
if k in (wx.WXK_SHIFT,wx.WXK_ALT,wx.WXK_CONTROL,wx.WXK_DOWN):
print('Special key')
evt.Skip()
app = wx.App()
frame = wx.Frame(None, -1, title='2', pos=(0, 0), size=(500, 500))
frame.Show(True)
messageTxt = StyledTextCtrl(frame, id=wx.ID_ANY, pos=(0, 0), size=(100 * 3, 100),
style=wx.TE_MULTILINE, name="File")
messageTxt.Bind(wx.EVT_KEY_DOWN, On_KeyDown)
messageTxt.Bind(wx.EVT_KEY_UP, On_KeyUp)
app.SetTopWindow(frame)
app.MainLoop()
然而,您需要将这些功能分成 wx.EVT_KEY_UP 和 wx.EVT_KEY_DOWN 功能,否则它们将相互交叉工作,即密钥更改测试将工作并且选择失败或副-反之亦然。
你可能误会了wx.event.Skip()
Skip(self, skip=True) This method can be used inside an event handler to control whether further event handlers bound to this event will be called after the current one returns.
Without Skip (or equivalently if Skip(false) is used), the event will not be processed any more. If Skip(true) is called, the event processing system continues searching for a further handler function for this event, even though it has been processed already in the current handler.
In general, it is recommended to skip all non-command events to allow the default handling to take place. The command events are, however, normally not skipped as usually a single command such as a button click or menu item selection must only be processed by one handler.