简体   繁体   English

在PGU中自动向下滚动

[英]Automatically scrolling down in PGU

I am using PGU as a GUI for a game I am writing in python3 with pygame. 我正在使用PGU作为我在python3中使用pygame编写的游戏的GUI。 This game is going to have multiplayer in it so I have been working on an in-game chat system. 这个游戏将有多人游戏,所以我一直在研究游戏中的聊天系统。 The chat system works and is displayed in a scroll area without problems however the scroll area does not scroll down automatically when new messages are received. 聊天系统工作并且在滚动区域中显示没有问题,但是当接收到新消息时滚动区域不会自动向下滚动。 This means every time there is a new message the user needs to scroll down manually to read the new messages. 这意味着每次有新消息时,用户需要手动向下滚动以阅读新消息。 Does anyone know of a way to do this with PGU? 有没有人知道如何用PGU做到这一点? Or does someone have recommendations for alternative way of doing this? 或者有人建议采取其他方式吗? Looking around on my own I found this example indicating it can be done however the code posted there doesn't seem to show the part I am looking for. 我自己环顾四周,发现这个例子表明它可以完成但是那里发布的代码似乎没有显示我正在寻找的部分。 Here is a dumbed down version of my own code. 这是我自己的代码的一个愚蠢的版本。 When a chat message is received then chatmessage is called automatically. 收到聊天消息后,将自动调用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)

I found my own solution to this problem. 我找到了自己解决这个问题的方法。 I'm posting the answer for anyone else that might have this issue in the future. 我正在为将来可能会遇到此问题的其他人发布答案。 Here is my updated example code with the fix: 这是我更新的示例代码和修复:

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)

I found that set_vertical_scroll() forcibly sets where the scroll area is. 我发现set_vertical_scroll()强制设置滚动区域的位置。 By setting someint to a number greater then what the number of messages in the chat box it goes to the bottom. 通过将someint设置为更大的数字,然后将聊天框中的消息数量设置为底部。 In a real working solution someint will need to be a variable that increases with the number of messages or there has to be a limit to the number of messages that are displayed (which is what I have done with my project). 在一个真正有效的解决方案中,someint将需要是一个随着消息数量而增加的变量,或者必须对显示的消息数量进行限制(这是我对我的项目所做的)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM