Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/358.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 在PGU中自动向下滚动_Python_Python 3.x_Pygame_Pgu - Fatal编程技术网

Python 在PGU中自动向下滚动

Python 在PGU中自动向下滚动,python,python-3.x,pygame,pgu,Python,Python 3.x,Pygame,Pgu,我正在使用python3和pygame编写一个游戏的GUI。这个游戏将有多人参与,所以我一直在开发一个游戏内聊天系统。聊天系统可以正常工作并显示在滚动区域中,不会出现任何问题,但是当收到新消息时,滚动区域不会自动向下滚动。这意味着每次有新消息时,用户需要手动向下滚动以读取新消息。有人知道用PGU做这件事的方法吗?或者有人有其他方法的建议吗?环顾四周,我发现这表明它可以做到,但张贴在那里的代码似乎没有显示我正在寻找的部分。这是我自己代码的简化版本。当收到聊天信息时,会自动调用chatmessage

我正在使用python3和pygame编写一个游戏的GUI。这个游戏将有多人参与,所以我一直在开发一个游戏内聊天系统。聊天系统可以正常工作并显示在滚动区域中,不会出现任何问题,但是当收到新消息时,滚动区域不会自动向下滚动。这意味着每次有新消息时,用户需要手动向下滚动以读取新消息。有人知道用PGU做这件事的方法吗?或者有人有其他方法的建议吗?环顾四周,我发现这表明它可以做到,但张贴在那里的代码似乎没有显示我正在寻找的部分。这是我自己代码的简化版本。当收到聊天信息时,会自动调用chatmessage

class ChatScreen:
    def __init__(self):
        self.desktop = gui.Desktop(theme=gui.Theme("data/themes/default/"))
        self.container = gui.Container(width=800,height=600)
        self.chatinput = gui.Input(size=65)
        self.chatdoc = gui.Document(width=1, height=10)
        self.chatscroll = gui.ScrollArea(self.chatdoc,width=600,height=100,hscrollbar=False)

        self.container.add(self.chatinput, 10, 550)
        self.container.add(self.chatscroll, 10, 440)
        self.desktop.init(self.container)

    def chatmessage(self, message):
        self.chatdoc.add(gui.Label(message))
        self.chatdoc.br(1)

我找到了自己解决这个问题的办法。我正在为将来可能有这个问题的其他人发布答案。下面是我更新的示例代码和修复程序:

class ChatScreen:
    def __init__(self):
        self.desktop = gui.Desktop(theme=gui.Theme("data/themes/default/"))
        self.container = gui.Container(width=800,height=600)
        self.chatinput = gui.Input(size=65)
        self.chatdoc = gui.Document(width=1, height=10)
        self.chatscroll = gui.ScrollArea(self.chatdoc,width=600,height=100,hscrollbar=False)

        self.container.add(self.chatinput, 10, 550)
        self.container.add(self.chatscroll, 10, 440)
        self.desktop.init(self.container)

    def chatmessage(self, message):
        self.chatdoc.add(gui.Label(message))
        self.chatdoc.br(1)
        self.desktop.loop()
        self.chatscroll.set_vertical_scroll(someint)
我发现set_vertical_scroll()强制设置滚动区域的位置。通过将someint设置为大于聊天框中消息数量的数字,它将进入底部。在实际的工作解决方案中,someint需要是一个随消息数量增加而增加的变量,或者必须对显示的消息数量进行限制(这就是我在项目中所做的)