Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Wx.stc.StyledTextCtrl滚动条_Python_Python 3.x_Wxpython_Scrollbar_Wx.textctrl - Fatal编程技术网

Python Wx.stc.StyledTextCtrl滚动条

Python Wx.stc.StyledTextCtrl滚动条,python,python-3.x,wxpython,scrollbar,wx.textctrl,Python,Python 3.x,Wxpython,Scrollbar,Wx.textctrl,在我的项目中,我使用wx.stc.StyledTextCtrl()。我将键向下和键向上的事件绑定。当我想在TextCtrl中添加字母时,我不会跳过该事件,出于某些原因,我使用AddText()方法来添加文本。当文本较长且滚动条(屏幕宽度)打开时,我希望滚动条位于可以看到添加的字母的位置(将自动移动)。当前,滚动条始终位于屏幕的左侧。 我正在寻找一个可以做到这一点的函数 当字母超过文本CTRL的宽度(超过位置300)时,滚动条仍然不移动。我希望它将像在框架右侧的messageTxt一样。 下面是一

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

当字母超过文本CTRL的宽度(超过位置300)时,滚动条仍然不移动。我希望它将像在框架右侧的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()

跳过(self,Skip=True) 此方法可在事件处理程序内使用,以控制在当前事件返回后是否调用绑定到此事件的其他事件处理程序

如果没有Skip(或者如果使用Skip(false),那么将不再处理事件。如果调用Skip(true),则事件处理系统将继续搜索此事件的其他处理程序函数,即使它已在当前处理程序中处理

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


我添加了一个不同的答案,因为第一个问题仍然有效,只是我解释的不是你的问题。
我假设这是您问题的模型,如果不是,请告诉我,我将删除它。
“服务器更新”由计时器模拟,添加字符,然后将光标向右移动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()

在我的项目中,我从服务器接收数据,需要使用AddText(text)将数据添加到messageTxt中。正如我在中所说的,我不想跳过按键向下的事件。您有其他解决方案吗?我想您可能混淆了“skip”的英文含义,即
不做
和wxpython事件。skip()
控制在当前事件返回后是否调用绑定到此事件的其他事件处理程序
,即执行进一步的事件,如推进滚动条。这个词的用法令人困惑。你试过了吗?我明白你说的。我将再次解释我自己-在项目中,我将每个字符发送到服务器,如果服务器允许我将字符添加到messageTxt,我将使用AddText()方法添加它。当我使用AddText()方法时,滚动条不会自行移动。我需要一个能处理它的函数。我只能建议你提供一个更好的示例代码,因为从你所说的,当前的示例并不代表你实际在做什么。
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()