0

I'm trying to write a script that plays audio files in background on loop. The audio to play is chosen randomly in a list of audio files.

I tried to do this: http://www.nerdparadise.com/tech/python/pygame/basics/part3/

And wrote:

from tkinter import *
import random
import pygame

_songs = ['audio1.ogg', 'audio2.ogg', 'audio3.ogg']
pygame.mixer.init()

_currently_playing_song = None

SONG_END = pygame.USEREVENT+1
pygame.mixer.music.set_endevent(SONG_END)


next_song = random.choice(_songs)
while next_song == _currently_playing_song:
        next_song = random.choice(_songs)
_currently_playing_song = next_song
pygame.mixer.music.load(next_song)
pygame.mixer.music.play()

while True:
        for event in pygame.event.get():
                if event.type == SONG_END:
                        pygame.mixer.music.load(next_song)
                        pygame.mixer.music.play()  
tk = Tk()
photo = PhotoImage(file="image.GIF")


image_label=Label(tk, image=photo)
image_label.grid(row=0, column=2)

tk.mainloop()

But I get this error:

for event in pygame.event.get():
pygame.error: video system not initialized 

Is there a way to solve this or use pygame using tkinter instead than pygame video system?

user5507535
  • 1,580
  • 1
  • 18
  • 39
  • 1
    Never tried mixing Tk with pygame. Both are used to present a GUI and handle events. I would think they would conflict unless you are very careful. – cmd Nov 05 '15 at 23:30

1 Answers1

0

You need to call pygame.init(). This will initialize the video system, which is required for the use of pygame.event. It is good practice to call at the beginning of every program that uses pygame. See the pygame event documentation here for more information.

Hope this helped.

EDIT:

As @cmd mentioned The program won't run correctly because you are using both Tkinter and pygame which have conflicting loops. A possible solution is to switch to using only one or the other and not both. See the question here for more info.

Community
  • 1
  • 1
Anthony
  • 670
  • 6
  • 20
  • Sorry, but didn't work. If I add pygame.init() after import pygame, when i run int, a Python shell appears, but nothing happens. – user5507535 Nov 05 '15 at 19:16
  • Is there an error that appears, or something doesn't happen that is supposed to happen? – Anthony Nov 05 '15 at 22:29
  • A window with an image, with music playing in background should appear. But only an empty shell appears. – user5507535 Nov 07 '15 at 15:27