[Python] Events not neutralised after use

Discussão em 'Python' iniciado por Stack, Maio 6, 2025.

  1. Stack

    Stack Membro Participativo

    As you can see in the demonstration program, when two widgets are superimposed, they both receive the same event and process it one after the other, whereas the desired behaviour would be for the first widget to process it and then delete it.

    In the documentation, it says that you only need to return True to neutralise an event but this doesn't seem to work.

    I don't know if it's related but in this code I have another problem. If you click on a button without releasing it, exit the main window to release it, this causes an exception to be thrown.

    from __future__ import annotations

    import pyglet
    from pyglet.graphics import Batch
    from pyglet.window import Window
    from pyglet.gui.widgets import WidgetBase, PushButton
    from pyglet.shapes import Rectangle

    from PIL import Image, ImageDraw



    class TestsEventsKlass() :
    def __init__( self, width, height ) :
    try :
    self.init_display( width, height )
    self.setup_ressources()
    self.setup_widgets()
    self.setup_handlers()

    except Exception as Err:
    raise



    def init_display( self, width, height ) :
    try :
    self.width = width
    self.height = height

    self.batch = Batch()
    self.display = pyglet.display.get_display()
    self.screen = self.display.get_default_screen()

    template = pyglet.gl.Config( double_buffer=True, alpha_size = 8, depth_size=0, stencil_size=0 )
    try:
    self.config = self.screen.get_best_config(template)
    except NoSuchConfigException as Err:
    print( f"Configuration désirée non supportée...[{Err}]")
    raise

    context = self.config.create_context(None)

    self.main_window = Window( width, height, config=self.config, style=Window.WINDOW_STYLE_OVERLAY )
    self.main_window.set_caption("B & Ev")

    except Exception as Err:
    raise



    def setup_ressources( self ) :
    try :
    self.back_color = (0,0,0,0)

    w = round( self.main_window.width * 0.4 )
    h = round( self.main_window.height * 0.3 )

    # UNPRESSED
    temp_image = Image.new( "RGBA", (w,h), self.back_color )
    draw = ImageDraw.Draw(temp_image)
    draw.rounded_rectangle( xy=(0,0,w,h), radius=27.0, fill=(64,64,64,255), outline=(32,32,32,255), width=7, corners=(True,True,True,True) )
    # temp_image.save( "./unpressed.png" )
    raw_image = temp_image.tobytes()
    self.unpressed_img = pyglet.image.ImageData( temp_image.width, temp_image.height, 'RGBA', raw_image, pitch=-temp_image.width * 4)

    # PRESSED
    temp_image = Image.new( "RGBA", (w,h), self.back_color )
    draw = ImageDraw.Draw(temp_image)
    draw.rounded_rectangle( xy=(0,0,w,h), radius=27.0, fill=(32,32,32,255), outline=(64,64,64,255), width=4, corners=(True,True,True,True) )
    # temp_image.save( "./pressed.png" )
    raw_image = temp_image.tobytes()
    self.pressed_img = pyglet.image.ImageData( temp_image.width, temp_image.height, 'RGBA', raw_image, pitch=-temp_image.width * 4)

    # HOVER
    temp_image = Image.new( "RGBA", (w,h), self.back_color )
    draw = ImageDraw.Draw(temp_image)
    draw.rounded_rectangle( xy=(0,0,w,h), radius=27.0, fill=(32,64,32,255), outline=(32,64,32,255), width=2, corners=(True,True,True,True) )
    # temp_image.save( "./hover.png" )
    raw_image = temp_image.tobytes()
    self.hover_img = pyglet.image.ImageData( temp_image.width, temp_image.height, 'RGBA', raw_image, pitch=-temp_image.width * 4)

    except Exception as Err:
    raise



    def setup_widgets( self ) :
    try :
    self.background = Rectangle( 0, 0, self.main_window.width, self.main_window.height,
    (0,0,255,32),
    batch=self.batch
    )

    w = self.unpressed_img.width
    h = self.unpressed_img.height
    x = round( (self.main_window.width-w) / 2 )
    y = round( (self.main_window.height-(h*2)) / 3 )
    self.button1 = PushButton( x, self.main_window.height - (y*4), self.unpressed_img, self.pressed_img, self.hover_img, self.batch )
    self.button2 = PushButton( x, y*2, self.unpressed_img, self.pressed_img, self.hover_img, self.batch )

    except Exception as Err:
    raise



    def setup_handlers( self ) :
    try :
    self.main_window.on_draw = self.window_on_draw

    self.button1_handlers = Button1_Events()
    self.button1.push_handlers( self.button1_handlers )

    self.button2_handlers = Button2_Events()
    self.button2.push_handlers( self.button2_handlers )

    self.main_window.push_handlers(self.button2)
    self.main_window.push_handlers(self.button1)

    except Exception as Err:
    raise



    def window_on_draw( self ) :
    try :
    self.main_window.clear()
    self.batch.draw()

    except Exception as Err:
    raise



    def run( self ) :
    try :
    pyglet.app.run()

    except Exception as Err:
    raise



    class Button1_Events() :

    def on_press( self, widget: PushButton ) :
    try :
    print( f"{self.__class__.__name__}.on_press" )
    widget._sprite.opacity = 27
    return True

    except Exception as Err:
    raise



    def on_release( self, widget: PushButton ) :
    try :
    print( f"{self.__class__.__name__}.on_release" )
    widget._sprite.opacity = 255
    return True

    except Exception as Err:
    raise



    class Button2_Events() :

    def on_press( self, widget: PushButton ) :
    try :
    print( f"{self.__class__.__name__}.on_press" )
    widget._sprite.opacity = 27
    return True

    except Exception as Err:
    raise



    def on_release( self, widget: PushButton ) :
    try :
    print( f"{self.__class__.__name__}.on_release" )
    widget._sprite.opacity = 255
    return True

    except Exception as Err:
    raise





    #################################################################
    if __name__ != "__main__":
    pass
    #################################################################
    else:
    #################################################################
    try :

    # Test
    ww = wh = 500
    app = TestsEventsKlass(ww,wh)
    app.run()

    except Exception as Err:
    print(Err)
    raise

    #################################################################

    Continue reading...

Compartilhe esta Página