Wx.stc.StyledTextCtrl ScrollBar

问题描述 投票:1回答:2

在我的项目中,我使用wx.stc.StyledTextCtrl()。我将按下和按下键的事件绑定在一起。当我想向TextCtrl中添加字母时,由于某些原因,我不跳过该事件,我使用方法AddText()来添加文本。当文本很长并且ScrollBar(处于屏幕宽度)打开时,我希望ScrollBar处于我可以看到所添加字母的位置(它将自动移动)。当前,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()

python python-3.x wxpython scrollbar wx.textctrl
2个回答
2
投票

很明显,在关键事件之后发生了另一个事件,该事件已被忽略。在与按键事件绑定的功能中使用event.Skip()

跳过(自我,跳过=真实)可以在事件处理程序内部使用此方法来控制在当前事件返回后是否将调用与该事件绑定的其他事件处理程序。

没有跳过(或者等效地,如果使用Skip(false),则将不再处理该事件。如果调用了Skip(true),则事件处理系统将继续搜索该事件的其他处理函数,即使该事件已在当前处理函数中进行了处理。

通常,建议跳过所有非命令事件以允许进行默认处理。但是,通常不会跳过命令事件,因为通常只有一个处理程序才能处理单个命令,例如单击按钮或选择菜单项。

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()

0
投票

我添加了一个不同的答案,因为第一个问题仍然有效,但就我的解释而言,不是针对您的问题。我认为这是您问题的模型,如果不是,请告诉我,我将其删除。“服务器更新”由计时器模拟,添加字符,然后我们只需将光标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()

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.