1

I create a little loading screen that show a animation during the load of any picture.

I've tried to add a smooth animation with Thread. Basically, I create a class Booting for a nice loading screen.


class Booting(Thread):
    """Thread to boot up the program"""

    def __init__(self):
        super(Booting, self).__init__()
        self.loop = True

    def run(self):
        if debug == True :
            self.begin = time.time()
        while self.loop:
            pygame.draw.rect(screen, (16, 16, 16), (0, 0, 1920, 1080)
            try:
                screen.blit(icon_big, (0, 0))
                screen.blit(animation[current], (0,0))
                current = current + 1
                if current == 9 :
                    current = 0
            except:
                pass

            pygame.display.update()

As you can see, I put the pygame.display.update in the thread

And... when I run it there is no pictures at all ! ( it work when I put it back ). So the Thread seems to be not able to solve my problem.

Someone can help me to find something that could help ? It will be better if it something simple :)

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • I'm a student for only some month and don't know how you pratice double buffering, can you give a code explain ? –  Jun 05 '19 at 22:12
  • [cont.] Are you sure that painting is the one that brings problems? pygame and the GUIs optimize that part so it should not be a problem. Have not you thought that another part of your code does not generate the problem? – eyllanesc Jun 05 '19 at 22:18
  • No, I really think it this part cause I recreate a new python file with only this and the main thread. Yeah, during the load of the picture, the windows freeze a few time ( depend of the size of the picture ) and I would add a animation on this screen. So I got the idea to make the animation part in the thread to make it smoothly –  Jun 05 '19 at 22:24
  • So why I said optimization cause it a lagggy animation that I will ( if this problem is solve ) make smoother –  Jun 05 '19 at 22:26
  • 1
    Wait I think I said something ( sorry, my english is not good as you ). Let me try to explain from a another point of view : first time I got my program wihout any class of threads that load any picture with pygame.image.load, pictures after pictures. At each load of a picture, the program stop until the picture is loaded... That I would change cause if I put the animation inside the main thread, I will be pretty ugly to see. You said me I can't make it with a thread, so, we are searching a simple solution –  Jun 05 '19 at 22:35
  • 1
    You have a [XY problem](https://meta.stackexchange.com/questions/66377), that is, you are asking for a possible solution that no one guarantees works against the underlying problem. Your question should be: How to load a large image without blocking the GUI? and then say that you have tried multithreading and it fails. And if the correct solution does not use multithreading? – eyllanesc Jun 05 '19 at 22:39
  • Rightly, I don't know what the correct solution use, so, I try with the tools I knwo... Unfortunately the double buffering look to be pretty difficul no ? –  Jun 05 '19 at 22:44
  • I've edited it :) –  Jun 05 '19 at 22:52
  • As a general advice, try to not use `threading` to do different pygame related stuff, high chance to fail. How big are these pictures you want to load? How long does it take to load one? (just to have an idea of the times involved) – Valentino Jun 05 '19 at 23:06
  • Depend of the picture, it'can be very fast or go to 1~2 second freeze that make to the animation 'laggy' because there is something like 50 pictures to load –  Jun 05 '19 at 23:13
  • By the way, could you show the code where the Booting instance is called too? It may be useful. – Valentino Jun 06 '19 at 00:59
  • @Edhyjox Do it the other way around. Load the images in a separated thread. Meanwhile you can do anything you want in the main thread. When the thread is finished and all images are loaded ([.join()](https://docs.python.org/3/library/threading.html#threading.Thread.join)) the main application can start. – Rabbid76 Jun 06 '19 at 17:17

0 Answers0