0

I am developing a program that will allow user to create and edit a GUI made in Pygame. The program itself will have a nice GUI and for that I am making a module to simplify its creation. While everything else for now works as I expect, I am hitting on that one problem that I simply can't solve, neither understand:

When I start the program a black window shows up. The problem is that the window should be white and containing a black rectangle with an image in it. I tried clicking in it and moving it around, but it didn't show anything.

First I thought that I didn't update the window, but I checked the script and found out that I did. Then I started trying different things with the window and found out that only the part of the window that exits my actual screen (monitor) gets updated and it gets updated instantly the moment I drag it out and back into screen.

Can someone please explain me what is happening, why is it happening and how I can fix it?

EDIT

I found out that the window also updates correctly if I minimize it and then maximize it. So the problem is not so critical but it is very annoying.

Here is the code:

import pygame
import time

def initialize():
    global display
    global screen
    pygame.init()
    display = pygame.display
    screen = None

class Screen():
    def __init__(self, size, color=[255, 255, 255]):
        global screen
        self.size = size
        self.color = color
        self.screen = display.set_mode(self.size)
        self.fill(self.color)
        self.update()
        screen = self
    def update(self, rectangle=None):
        display.update(rectangle)
    def fill(self, color, rectangle=None):
        self.screen.fill(color, rectangle)
    def draw(self, sprite):
        self.screen.blit(sprite.image, sprite.rectangle[0])

class Engine():
    def __init__(self):
        self.events = []
        self.running = False
    def update_events(self):
        self.events += pygame.event.get()
    def get_events(self):
        events = self.events
        self.events = []
        return events
    def start(self, function=None, args=[], kwargs={}, frequency=21, function_first=False):
        self.running = True
        while self.running:
            if function_first and function:
                function(*args, **kwargs)
            self.update_events()
            if not function:
                for event in self.events:
                    if event.type == pygame.QUIT:
                        self.running = False
                        pygame.quit()
                        break
            if not function_first and function:
                function(*args, **kwargs)
            if self.running:
                tick = 1.0 / frequency
                time.sleep(tick)
    def stop(self):
        self.running = False

class Sprite():
    def __init__(self, rectangle, color=[0, 0, 0]):
        self.rectangle = rectangle
        self.color = color
        self.image = None
    def update(self):
        global screen
        screen.fill(self.color, self.rectangle)
        screen.draw(self)
    def load_image(self, image):
        self.image = pygame.image.load(image).convert()

if __name__ == "__main__":
    initialize()
    s = Screen([500, 500])
    e = Engine()
    sprite = Sprite([[10, 10], [200, 200]])
    sprite.load_image("test.png")
    sprite.update()
    s.update()
    e.start()

Here are the screenshots:

When I start the program: enter image description here

When I pull the window out of my screen: enter image description here

When I pull it back in: enter image description here

1 Answer 1

1

You have some errors in your code.

  1. You don't update the screen in your main loop.

  2. You should always call pygame.event.get in every iteration of your main loop.

  3. Don't use time.sleep, use pygame.time.clock.tick

Sign up to request clarification or add additional context in comments.

4 Comments

You are right. I don't update the display in the loop, but I update it before it. And from my experience I know, that that should work as well. As for calling the pygame.event.get(), I do. If you look at my code, you can see that i call engine.start() that starts the loop. In it I call self.update_events() in every iteration of it. Nonetheless, the behaviour I get is a kind of behaviour I never got before.
No you have to update the screen in every iteration if you wanna be able to drag around the window. No way around it
I am able to drag it around. But the image won't show up. And it always did. No matter if updated the screen constantly or just once.
@Cosinux, Fred is right, you need to draw things to the screen every frame if the window is not a still picture within the desktop area. That aside, this is the strangest implementation of pygame I've ever seen :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.