Wx.stc.StyledTextCtrl滚动条
Wx.stc.StyledTextCtrl ScrollBar
在我的项目中,我使用 wx.stc.StyledTextCtrl()。我绑定按键事件和按键事件。当我想在 TextCtrl 中添加一个字母时,我不会跳过该事件,出于某些原因,我使用方法 AddText() 来添加文本。当文本很长并且 ScrollBar(屏幕宽度)打开时,我希望 ScrollBar 位于我可以看到添加的字母的位置(将按原样自动移动)。目前 ScrollBar 始终位于屏幕的左侧。
我正在寻找可以做到这一点的功能。
当字母超过 TextCtrl 的宽度(超过位置 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()
显然在关键事件之后发生了另一个事件,但被遗漏了。
在绑定到按键事件的函数中使用 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()
我添加了一个不同的答案,因为第一个问题仍然有效,只是不适合你的问题,正如我所解释的那样。
我假设这是您问题的模型,如果不是,请告诉我,我会删除它。
"server updates"是通过一个定时器来模拟的,添加字符,那么我们只要将光标向右移动1个字符即可。
import wx
import wx.stc
server_sends=['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','T','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','T']
class MyFrame(wx.Frame):
def __init__(self, parent, id=wx.ID_ANY, title=""):
super(MyFrame, self).__init__(parent, id, title)
self.SetSize((500,500))
self.panel = wx.Panel(self, -1 , size=(500,500))
self.messageTxt = wx.stc.StyledTextCtrl(self.panel, id=wx.ID_ANY, pos=(0, 0), size=(300, 300),
style=wx.TE_MULTILINE, name="File")
self.messageTxt2 = wx.stc.StyledTextCtrl(self.panel, id=wx.ID_ANY, pos=(320, 0), size=(150, 150), style=wx.TE_MULTILINE, name="File")
self.timer = wx.Timer(self)
self.Bind(wx.EVT_TIMER, self.OnTimer, self.timer)
self.timer.Start(500)
self.cnt = 0
self.Show()
def OnTimer(self, event):
print (server_sends[self.cnt])
self.Server_Update(server_sends[self.cnt])
self.cnt += 1
if self.cnt > len(server_sends) - 1 :
self.timer.Stop()
def Server_Update(self,char):
self.messageTxt.AddText(char)
self.messageTxt.CharRight()
if __name__ == "__main__":
app = wx.App()
frame = MyFrame(None,title="The Main Frame")
app.MainLoop()
在我的项目中,我使用 wx.stc.StyledTextCtrl()。我绑定按键事件和按键事件。当我想在 TextCtrl 中添加一个字母时,我不会跳过该事件,出于某些原因,我使用方法 AddText() 来添加文本。当文本很长并且 ScrollBar(屏幕宽度)打开时,我希望 ScrollBar 位于我可以看到添加的字母的位置(将按原样自动移动)。目前 ScrollBar 始终位于屏幕的左侧。 我正在寻找可以做到这一点的功能。
当字母超过 TextCtrl 的宽度(超过位置 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()
显然在关键事件之后发生了另一个事件,但被遗漏了。
在绑定到按键事件的函数中使用 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()
我添加了一个不同的答案,因为第一个问题仍然有效,只是不适合你的问题,正如我所解释的那样。
我假设这是您问题的模型,如果不是,请告诉我,我会删除它。
"server updates"是通过一个定时器来模拟的,添加字符,那么我们只要将光标向右移动1个字符即可。
import wx
import wx.stc
server_sends=['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','T','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','T']
class MyFrame(wx.Frame):
def __init__(self, parent, id=wx.ID_ANY, title=""):
super(MyFrame, self).__init__(parent, id, title)
self.SetSize((500,500))
self.panel = wx.Panel(self, -1 , size=(500,500))
self.messageTxt = wx.stc.StyledTextCtrl(self.panel, id=wx.ID_ANY, pos=(0, 0), size=(300, 300),
style=wx.TE_MULTILINE, name="File")
self.messageTxt2 = wx.stc.StyledTextCtrl(self.panel, id=wx.ID_ANY, pos=(320, 0), size=(150, 150), style=wx.TE_MULTILINE, name="File")
self.timer = wx.Timer(self)
self.Bind(wx.EVT_TIMER, self.OnTimer, self.timer)
self.timer.Start(500)
self.cnt = 0
self.Show()
def OnTimer(self, event):
print (server_sends[self.cnt])
self.Server_Update(server_sends[self.cnt])
self.cnt += 1
if self.cnt > len(server_sends) - 1 :
self.timer.Stop()
def Server_Update(self,char):
self.messageTxt.AddText(char)
self.messageTxt.CharRight()
if __name__ == "__main__":
app = wx.App()
frame = MyFrame(None,title="The Main Frame")
app.MainLoop()