如何在使用批量渲染时从屏幕上删除对象(标签,图形)?

问题描述 投票:1回答:1

我正在Pyglet中构建一个演示程序,它使用串行输出来读取和更新屏幕上的对象。

import pyglet
import output_parser as parse

port = input("Serial port ? (examples:COM3, /dev/ttyUSB0) ")
ser = parse.serial.Serial(port=port, baudrate=115200)

ser.write('exit\r'.encode('utf-8'))

# Test is ser working
ser.isOpen()

##########################################################
def update(self):
    #Some code here to obtain output and treat data
    #
    #
    if posTiles and negTiles:
        global finalcoords
        finalcoords = findCoords(poscoords, negcoords, wide)
        global canvas
        canvas[0].vertices = finalcoords
        if uid:
            global uid_label
            uid_label = pyglet.text.Label(text=str(uid),
                                          font_name='Times New Roman',
                                          font_size=24,
                                          x=finalcoords[0]+wide,
                                          y=finalcoords[5],
                                          color=(0,0,250,255),
                                          batch=batch, group=text)
    else:
        if canvas:
            try:
                canvas[0].vertices = zero
                uid_label.delete()
            except:
                pass
######################################################
try:
    config = pyglet.gl.Config(double_buffer=True)
    window = pyglet.window.Window(1280, 720, resizable=True, config=config)

    window.set_minimum_size(640, 480)

    batch = pyglet.graphics.Batch()
    plate = pyglet.graphics.OrderedGroup(0)
    connect = pyglet.graphics.OrderedGroup(1)
    text = pyglet.graphics.OrderedGroup(2)

    cells = {}
    canvas = {}
    cells[0] = Cell(x[0],y[0],l, global_id[0:4])

    canvas[0] = batch.add(4, pyglet.gl.GL_QUADS, connect, ('v2f', zero))

    @window.event
    def on_draw():
        window.clear()
        batch.draw()

    @window.event
    def on_deactivate():
        ser.close()

    #window.push_handlers(pyglet.window.event.WindowEventLogger())

    pyglet.clock.schedule_interval(update, 1/240)

    pyglet.app.run()

finally:
    window.close()
    ser.close()

update()函数检查串行输出的变化,并在pyglet窗口中绘制适当的形状。我还希望update()函数在串口没有实际输出时从屏幕上删除uid_labelThis image显示程序的工作原理 - 您可以将画布对象视为白色矩形,将“3004”视为标签。

但是,当串口没有输出时(没有posTilesnegTiles的值),标签对象仍然是:Image,即使我已经调用了uid_label.delete()

所以,我的问题是 - 如何让屏幕上的标签消失? uid_label.delete()似乎不起作用,因为标签仍在记忆中,即使在window.clear()batch.draw()之后仍然在屏幕上显示。除非我不正确地理解这一点,否则不应在窗口上重绘从批处理中删除的对象。

在尝试使canvas[0]对象出现和消失之前,我遇到了同样的问题,但我通过将顶点设置为零找到了解决方法。但是,我不能用标签做到这一点,理想情况下我也想在程序运行时添加和删除对象,而不必在需要时存储它们的顶点并将它们设置为零。

python python-3.x windows pyglet
1个回答
0
投票

实际上,.delete()确实有效。这是一个最小的例子(基于你的代码),可以解决这个问题:

import pyglet

canvas = {}

try:
    config = pyglet.gl.Config(double_buffer=True)
    window = pyglet.window.Window(1280, 720, resizable=True, config=config)
    window.set_minimum_size(640, 480)

    batch = pyglet.graphics.Batch()

    canvas[1] = pyglet.text.Label("Moo", x=10, y=10, batch=batch)

    @window.event
    def on_draw():
        window.clear()
        batch.draw()

    @window.event
    def on_key_press(symbol, modifiers):
        # As soon as a key is pressed, we delete the batch objects (all of them)
        for index in list(canvas):
            canvas[index].delete()
            del(canvas[index])

    pyglet.app.run()

finally:
    window.close()

按任意键,它应该使标签消失。 您尝试此操作时可能遇到的一个问题是,代码中没有任何内容可以实际刷新图形区域。

哪会产生错觉,没有发生任何事情。无论何时使用batch.add(...),它都会返回一个顶点对象,你可以在其中执行delete()。精灵也一样。

© www.soinside.com 2019 - 2024. All rights reserved.