在PGU中自动向下滚动

2024-10-03 06:27:30 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用PGU作为我用python3和pygame编写的游戏的GUI。这个游戏将有多人在它,所以我一直在游戏聊天系统的工作。聊天系统工作正常,显示在一个滚动区域,没有问题,但滚动区域不会在收到新消息时自动向下滚动。这意味着每次有新消息时,用户都需要手动向下滚动以读取新消息。有人知道PGU的方法吗?或者有人有其他方法的建议吗?环顾四周,我发现this example表明它可以做到,但张贴在那里的代码似乎没有显示我正在寻找的部分。下面是我自己代码的简化版本。收到聊天信息后,会自动调用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)

Tags: 方法代码selfadd游戏消息区域container
1条回答
网友
1楼 · 发布于 2024-10-03 06:27:30

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

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\u vertical\u scroll()强制设置滚动区域的位置。通过将someint设置为大于聊天框中消息数量的数字,它将进入底部。在实际的工作解决方案中,someint需要是一个随消息数量增加而增加的变量,或者必须对显示的消息数量进行限制(这就是我对项目所做的)。你知道吗

相关问题 更多 >