gpt4 book ai didi

python - Wx.stc.StyledTextCtrl 滚动条

转载 作者:行者123 更新时间:2023-12-01 00:09:05 26 4
gpt4 key购买 nike

在我的项目中,我使用wx.stc.StyledTextCtrl()。我绑定(bind)了按键按下和按键按下的事件。当我想在 TextCtrl 中添加一个字母时,我不会跳过该事件,由于某些原因,我使用方法 AddText() 来添加文本。当文本很长并且滚动条(屏幕宽度)打开时,我希望滚动条位于我可以看到添加的字母的位置(将按应有的方式自动移动)。目前,ScrollBar 始终位于屏幕的左侧。我正在寻找一个可以做到这一点的函数。

当字母超过 TextCtrl 的宽度(超过 pos 300)时,ScrollBar 仍然不会移动。我希望它像框架右侧的 messageTxt 一样。这是提出我的问题的基本代码:

import wx
import wx.stc

def on_key_down(event):
pass
def on_key_up(event):
key_code = event.GetKeyCode()
messageTxt.AddText(chr(key_code))

app = wx.App()
frame = wx.Frame(None, -1, title='2', pos=(0, 0), size=(500, 500))
frame.Show(True)
messageTxt = wx.stc.StyledTextCtrl(frame, id=wx.ID_ANY, pos=(0, 0), size=(300, 300),
style=wx.TE_MULTILINE, name="File")
messageTxt.Bind(wx.EVT_KEY_DOWN, on_key_down)
messageTxt.Bind(wx.EVT_KEY_UP, on_key_up)
messageTxt2 = wx.stc.StyledTextCtrl(frame, id=wx.ID_ANY, pos=(320, 0), size=(150, 150),
style=wx.TE_MULTILINE, name="File")

app.SetTopWindow(frame)
app.MainLoop()

最佳答案

显然,在关键事件之后发生了另一个事件,而该事件被错过了。
在绑定(bind)到按键事件的函数中使用 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.

import wx
import wx.stc

def on_key_down(event):
event.Skip()
pass
def on_key_up(event):
key_code = event.GetKeyCode()
messageTxt.AddText(chr(key_code))
event.Skip()

app = wx.App()
frame = wx.Frame(None, -1, title='2', pos=(0, 0), size=(500, 500))
frame.Show(True)
messageTxt = wx.stc.StyledTextCtrl(frame, id=wx.ID_ANY, pos=(0, 0), size=(300, 300),
style=wx.TE_MULTILINE, name="File")
messageTxt.Bind(wx.EVT_KEY_DOWN, on_key_down)
messageTxt.Bind(wx.EVT_KEY_UP, on_key_up)
messageTxt2 = wx.stc.StyledTextCtrl(frame, id=wx.ID_ANY, pos=(320, 0), size=(150, 150),
style=wx.TE_MULTILINE, name="File")

app.SetTopWindow(frame)
app.MainLoop()

关于python - Wx.stc.StyledTextCtrl 滚动条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59756826/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com