如何使Pyglet的ScrollableTextLayout中的文本向上滚动,而不是向下滚动。视图似乎没有文档中列出的效果

2024-10-01 19:15:02 发布

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

我在尝试如何使文本在Pyglet的ScrollableTextLayout中向上滚动,而不是向下滚动。为了清楚起见,这里有一个简短的快照来说明我所说的“向上”是什么意思(以防万一)

How it currently behaves

我希望它的表现:

enter image description here

根据文档,这个行为可以通过view属性实现,但是我尝试过各种不同的值,但是都没有明显的变化。在

代码:

import pyglet

class LoadDialog(pyglet.sprite.Sprite):
    def __init__(self):
        self.lbatch = pyglet.graphics.Batch()

        self.loading_window = pyglet.image.load('..\\resources\\loading_base.png')
        super(LoadDialog, self).__init__(self.loading_window, batch=self.lbatch)


        self.doc = pyglet.text.decode_text('Hello world!'.ljust(40))
        self.doc.set_style(0,12, dict(font_name='Arial', font_size=12,
                                    color=(0,0,0,255)))

        self.layout = pyglet.text.layout.ScrollableTextLayout(self.doc, 
                                            width=self.load_animation.width, 
                                            height=100, multiline=True, batch=self.lbatch)
        self.layout.x = 220
        self.layout.y = 160
        self.layout.view_y = -80

    def update(self, dx):
        self.doc.insert_text(0, "New line".ljust(40))






sprite = LoadDialog()
window = pyglet.window.Window(width=640, height=480)

pyglet.gl.glClearColor(1, 1, 1, 1)

@window.event
def on_draw():
    window.clear()
    sprite.lbatch.draw()
    sprite.layout.draw()

@window.event
def update(dx):
    sprite.update(dx)

pyglet.clock.schedule_interval(update, 1.0)
pyglet.app.run()

我尝试了layout.view_y的大量值,从-1到荒谬的值,比如-3000,或者{},只是想看看某些东西是否改变了。但它总是给出第一张图中所示的确切行为。在

我做错什么了?在


Tags: textselfviewdocdefupdatewindowwidth
1条回答
网友
1楼 · 发布于 2024-10-01 19:15:02

首先,您的示例依赖于一个图像文件及其宽度(未提供),这使测试稍微复杂一些。在

第二步通过调用pyglet.text.decode_text创建一个UnformattedDocument,然后在这行的0位置(开始处)将文本显式插入到UnformattedDocument中:

def update(self, dx):
    self.doc.insert_text(0, "New line".ljust(40))

如果您希望文本显示在末尾,就像您在图形中所暗示的那样,请在末尾插入它!在

^{pr2}$

第三个让我们回答您实际提出的问题。如果您阅读属性ScrollableTextLayout.view_y的API文档,您会发现。。。在

Values outside of the range [height - content_height, 0] are automatically clipped in range.

…因此,当内容高度为0时,将视图设置为-80,会导致视图被剪裁为0,然后再也不会尝试设置视图。滚动问题的解决方案是每次内容高度更改时设置视图。对于一个简单的修复方法,您可以简单地设置视图,使内容的底部始终向上滚动到框架的底部:

def update(self, dx):
    # Fix the implied bug
    self.doc.insert_text(-1, "New line".ljust(40))
    # The answer to the stated question
    self.layout.view_y = -self.layout.content_height

相关问题 更多 >

    热门问题