Search code examples
pythonpygamedrag-and-drop

Why is a Drag and Drop event on Pygame not allowed?


I have a simple Pygame display:

pygame.init()
screen = pygame.display.set_mode((1024, 576))
clock = pygame.time.Clock()
while True:
    for event in pygame.event.get():  # to handle clicks on the screen (prevent crash)
        if event.type == pygame.QUIT:
            pygame.display.quit()
        if event.type == pygame.DROPFILE:
            path = event.file
            print(path)
    pygame.display.update()

I'm currently testing the "drop file" event to use it in a project I'm working on. Unfortunately, when I drag a file onto the screen, the cursor turns into a "not allowed" sign and nothing happens when I drop the file. Why does it happen?


Solution

  • Without changing your code much (adding 'import pygame'), it doesn't work for me either. I droped a file and then the same happend for me what happend for you. Thats what I thought.

    I first tried Python 3.8.6 with Pygame 1.9.6. Then I remembered, that I have an other installation of Python with 3.9.1 and Pygame version 2.0.0.

    This second combination worked for me. I don't know which part made the difference in the end, but I think they did much work for pygame 2.0.0, so give it a try.

    This works for me on Windows 10.