YannThorimbert / Thorpy

A GUI library for Pygame
http://www.thorpy.org
Other
31 stars 5 forks source link

pygame integration + launchers not working #8

Open 0tso opened 1 year ago

0tso commented 1 year ago

Hello,

adding a launcher (for example a BrowserLauncher) to a PyGame-integrated Thorpy program causes the whole program to crash when opening said launcher.

Steps to reproduce

  1. Copy the code from the PyGame integration tutorial
  2. Add a BrowserLauncher as demonstrated in the elements overview example:
    
    #Tutorial : how to use ThorPy with a pre-existing code - step 1
    import pygame, thorpy
    pygame.init()
    pygame.key.set_repeat(300, 30)
    screen = pygame.display.set_mode((400,400))
    screen.fill((255,255,255))
    rect = pygame.Rect((0, 0, 50, 50))
    rect.center = screen.get_rect().center
    clock = pygame.time.Clock()

pygame.draw.rect(screen, (255,0,0), rect) pygame.display.flip()

declaration of some ThorPy elements ...

browser = thorpy.Browser("../../", text="Browser") browserlauncher = thorpy.BrowserLauncher(browser, const_text="Choose file:", var_text="") slider = thorpy.SliderX(100, (12, 35), "My Slider") button = thorpy.make_button("Quit", func=thorpy.functions.quit_func) box = thorpy.Box(elements=[slider,button, browserlauncher])

we regroup all elements on a menu, even if we do not launch the menu

menu = thorpy.Menu(box)

important : set the screen as surface for all elements

for element in menu.get_population(): element.surface = screen

use the elements normally...

box.set_topleft((100,100)) box.blit() box.update()

playing_game = True while playing_game: clock.tick(45) for event in pygame.event.get(): if event.type == pygame.QUIT: playing_game = False break elif event.type == pygame.KEYDOWN: if event.key == pygame.K_LEFT: pygame.draw.rect(screen, (255,255,255), rect) #delete old pygame.display.update(rect) rect.move_ip((-5,0)) pygame.draw.rect(screen, (255,0,0), rect) #drat new pygame.display.update(rect) menu.react(event) #the menu automatically integrate your elements

pygame.quit()

The following error is produced when trying to open the launcher:

Traceback (most recent call last): File "program.py", line 45, in menu.react(event) #the menu automatically integrate your elements File "..../thorpy/menus/basicmenu.py", line 121, in react element.react(event) File "..../thorpy/elements/ghost.py", line 328, in react reaction._try_activation(event) File "..../thorpy/miscgui/reaction.py", line 57, in _try_activation self.reac_func(**self.params) File "..../thorpy/miscgui/launchers/launcher.py", line 253, in launch self.prelaunch() File "..../thorpy/miscgui/launchers/launcher.py", line 215, in prelaunch self.activate_focus() File "..../thorpy/miscgui/launchers/launcher.py", line 178, in activate_focus self.save_active_records() File "..../thorpy/miscgui/launchers/launcher.py", line 170, in save_active_records for e in menu.get_population(): AttributeError: 'NoneType' object has no attribute 'get_population'

# Possible explanation
I believe the error is caused by the fact that in a PyGame-integrated program, the primary `Menu`-object is never set to be the `_CURRENT_MENU` that's stored in `miscgui/application.py`. This happens because the `Menu.play()`-method, which would set the current menu object, is never called. However, some of `Launcher`'s methods use the current menu object, thus causing the error.

# Possible solution
Add the following two lines to the PyGame integration tutorial:
```python
import thorpy.miscgui.functions
thorpy.miscgui.functions.set_current_menu(menu)

The above code sets the current menu and resolves the crash.

Alternative solution

Add a set_current()-method (or similar) to the Menu-class that would play the role of miscgui.functions.set_current_menu(), and add that to the PyGame integration tutorial.

YannThorimbert commented 1 year ago

Hello,

Thanks for reporting the issue and for the solution that you tested. I will commit it as soon as I can.

In the upcoming weeks, a very new version of Thorpy (2.0) will be out, where everything has been rewritten and cleaned, using the things I learned from the mistakes I have made with the first version. In the end the library will be less intrusive, more complete and more straightforward to use.

Since retrocompatibility won't be guaranteed, I will commit a final version for Thorpy1.x that will rest on a different Git repository than Thorpy2.x versions, so your modification will last somehow.

Cheers,

Yann

Le jeu. 13 avr. 2023 à 17:28, 0tso @.***> a écrit :

Hello,

adding a launcher (for example a BrowserLauncher) to a PyGame-integrated Thorpy program causes the whole program to crash when opening said launcher. Steps to reproduce

  1. Copy the code from the PyGame integration tutorial http://thorpy.org/tutorials/include.html
  2. Add a BrowserLauncher as demonstrated in the elements overview example http://thorpy.org/examples/overview.html:

Tutorial : how to use ThorPy with a pre-existing code - step 1import pygame, thorpypygame.init()pygame.key.set_repeat(300, 30)screen = pygame.display.set_mode((400,400))screen.fill((255,255,255))rect = pygame.Rect((0, 0, 50, 50))rect.center = screen.get_rect().centerclock = pygame.time.Clock()

pygame.draw.rect(screen, (255,0,0), rect)pygame.display.flip()

declaration of some ThorPy elements ...browser = thorpy.Browser("../../", text="Browser")browserlauncher = thorpy.BrowserLauncher(browser, const_text="Choose file:",

                                            var_text="")slider = thorpy.SliderX(100, (12, 35), "My Slider")button = thorpy.make_button("Quit", func=thorpy.functions.quit_func)box = thorpy.Box(elements=[slider,button, browserlauncher])#we regroup all elements on a menu, even if we do not launch the menumenu = thorpy.Menu(box)#important : set the screen as surface for all elementsfor element in menu.get_population():
element.surface = screen#use the elements normally...box.set_topleft((100,100))box.blit()box.update()

playing_game = Truewhile playing_game: clock.tick(45) for event in pygame.event.get(): if event.type == pygame.QUIT: playing_game = False break elif event.type == pygame.KEYDOWN: if event.key == pygame.K_LEFT: pygame.draw.rect(screen, (255,255,255), rect) #delete old pygame.display.update(rect) rect.move_ip((-5,0)) pygame.draw.rect(screen, (255,0,0), rect) #drat new pygame.display.update(rect) menu.react(event) #the menu automatically integrate your elements pygame.quit()

The following error is produced when trying to open the launcher:

Traceback (most recent call last): File "program.py", line 45, in menu.react(event) #the menu automatically integrate your elements File "..../thorpy/menus/basicmenu.py", line 121, in react element.react(event) File "..../thorpy/elements/ghost.py", line 328, in react reaction._try_activation(event) File "..../thorpy/miscgui/reaction.py", line 57, in _try_activation self.reac_func(**self.params) File "..../thorpy/miscgui/launchers/launcher.py", line 253, in launch self.prelaunch() File "..../thorpy/miscgui/launchers/launcher.py", line 215, in prelaunch self.activate_focus() File "..../thorpy/miscgui/launchers/launcher.py", line 178, in activate_focus self.save_active_records() File "..../thorpy/miscgui/launchers/launcher.py", line 170, in save_active_records for e in menu.get_population(): AttributeError: 'NoneType' object has no attribute 'get_population'

Possible explanation

I believe the error is caused by the fact that in a PyGame-integrated program, the primary Menu-object is never set to be the _CURRENT_MENU that's stored in miscgui/application.py. This happens because the Menu.play()-method, which would set the current menu object, is never called. However, some of Launcher's methods use the current menu object, thus causing the error. Possible solution

Add the following two lines to the PyGame integration tutorial:

import thorpy.miscgui.functionsthorpy.miscgui.functions.set_current_menu(menu)

The above code sets the current menu and resolves the crash. Alternative solution

Add a set_current()-method (or similar) to the Menu-class that would play the role of miscgui.functions.set_current_menu(), and add that to the PyGame integration tutorial.

— Reply to this email directly, view it on GitHub https://github.com/YannThorimbert/Thorpy/issues/8, or unsubscribe https://github.com/notifications/unsubscribe-auth/AC5VETIX4NTXYNVZQPTSX23XBALQNANCNFSM6AAAAAAW5H5QBA . You are receiving this because you are subscribed to this thread.Message ID: @.***>