0

I am using pygame to load an image (Count.png, just a grey background png file) as an overlay counter to anything that is currently displayed onscreen. During this process if I let this run for 5 minutes all the sudden the image quits loading (doesn't show up at the coordinates 300x300) but the program keeps running, meaning the action that is tied to the counter keeps going. In this case simple key press of e. How do I properly "close" the image so it can be reopened or prevent the image from disappearing (not really sure what happens to the image, it shows on the taskbar and action still runs every x seconds)?

What you will see is the counter just counting down on whatever image that is saved as Count.png. This runs from a simple tkinter button. The button starts an automation process and starts this counter. When the counter reaches 0, the process is restarted which should include the counter. I wrote out all the parts except the tkinter window with a button. The icon.ico file is exactly what it states so instead of the nice little feather on the taskbar I get the actual icon tied to the application.


import os
from ctypes import windll
import pygame
import time
import pyautogui


def countingRoutine():

    locx = 300
    locy = 300

    os.environ['SDL_VIDEO_WINDOW_POS'] = "%d,%d" % (locx, locy)
    pygame.init()
    set_window_pos = windll.user32.SetWindowPos
    screen = pygame.display.set_mode((50, 30), pygame.NOFRAME)
    set_window_pos(pygame.display.get_wm_info()['window'], -1, locx, locy, 0, 0, 
    0x0001)

    clock = pygame.time.Clock()
    counter, text = 13, '00'.ljust(4)
    pygame.time.set_timer(pygame.USEREVENT, 1000)
    font = pygame.font.SysFont('Avatar', 23)

    while True:

        for e in pygame.event.get():
            if e.type == pygame.USEREVENT:
                if counter > 0:
                    counter -= 1
                    text = str(counter).center(0).rjust(4)

                else:
                    main()

        screen.fill((255, 191, 0))
        for_trans = pygame.image.load_extended('Count.png')
        loadImg = pygame.image.load_basic('icon.ico')
        pygame.display.set_icon(loadImg)
        screen.blit(for_trans, (0, -15))
        screen.blit(font.render(text, True, (200, 0, 200)), (0, 0))
        pygame.display.flip()
        clock.tick(counter)
        continue


def msgBox():
    pyautogui.confirm("Enabling Automated process\n ", "Activating", buttons= 
    ['Affirmative'])


def main():

    while True:

        pyautogui.hotkey('e')
        countingRoutine()
        time.sleep(13)

if __name__ == "__main__":
    msgBox()
    main()
bob
  • 21
  • 1
  • 4
  • 1
    What is `pygame.image.load_basic`? It's not a part of the [official documentation](https://www.pygame.org/docs/ref/image.html). – Ted Klein Bergman Jun 25 '18 at 21:15
  • Also, what is the snippet for? – Ted Klein Bergman Jun 25 '18 at 21:17
  • The argument to `clock.tick` is not the amount of time to sleep, it's the FPS cap. So you're running the game at somewhere between 29 to 44 frames per second. You also shouldn't be using `pygame.display.flip` and `pygame.display.update` right after each other. They're doing exactly the same thing (at least in your situation) and are really performance heavy. I'd suggest using only `pygame.display.update`. – Ted Klein Bergman Jun 25 '18 at 21:22
  • 1
    You have an event posted every second that decrements the counter, and since the counter is only a number between 29 and 44, your program should call `pygame.display.quit()` after about 29 to 44 seconds and enter your main function. Also, `pygame.QUIT` is a constant (an event type), not a function. Did you mean to call `pygame.quit()`. – Ted Klein Bergman Jun 25 '18 at 21:30
  • I left some testing items in this by accident, mainly the pygame.update and the pygame.QUIT.. This overlay is a counter that counts down and when it reaches 0 it should restart. This works fine for about 5 minutes then the image doesn't show up anymore (image being the imgOverlay.png) but the function that this is using keeps going and restarts. ygame.image.load_basic is used to load an icon on the window itself so it shows up in windows taskbar. I believe I got that from one of the pygame tutorials that I went through. – bob Jun 26 '18 at 00:13
  • I do have similar items such as this one running that work with the above code the only exception being the amount of time they restart. This is the shortest item that restarts the display of the counter the others run for at least a minute before they are restarted. – bob Jun 26 '18 at 00:17
  • But it doesn't restart, it deinitializes the display module (`pygame.display.quit()`) and then calls a main function. This after only 29 to 44 seconds. Also, what about the two first comments? This could be be useful to know in order to find an answer to the problem. Could you edit the code to match your code (i.e. without the testing items)? – Ted Klein Bergman Jun 26 '18 at 00:19
  • Yep, here you go.. I know this counter works with the image overlay, tested it again before posting.. – bob Jun 26 '18 at 13:35
  • can you create a chat room? – bob Jun 26 '18 at 13:59
  • @bob can you create a [mcve] example so that folks can try to help. – import random Jun 29 '18 at 03:53
  • updated let me know if this was not enough – bob Jul 02 '18 at 18:58
  • This issue turns out to be a random number being used on the "counter" line: counter, text = 13, '00'.ljust(4) When this is being used as below the problem manifests itself. Without using a randomly generated number, the counter displays on screen as it should. counter, text = random.randint(130, 150), '00'.ljust(4) Why does this occurs I am still do not know – bob Jul 19 '18 at 14:57

0 Answers0