Skip to content Skip to sidebar Skip to footer

Playing Music With Pyglet And Tkinter In Python

I wanted to create a simple gui with a play and stop button to play an mp3 file in python. I created a very simple gui using Tkinter that consists of 2 buttons (stop and play). I c

Solution 1:

You are mixing two UI libraries together - that is not intrinsically bad, but there are some problems. Notably, both of them need a main loop of their own to process their events. TKinter uses it to communicate with the desktop and user-generated events, and in this case, pyglet uses it to play your music.

Each of these loops prevents a normal "top down" program flow, as we are used to when we learn non-GUI programming, and the program should proceed basically with callbacks from the main loops. In this case, in the middle of a Tkinter callback, you put the pyglet mainloop (calling pyglet.app.run) in motion, and the control never returns to the Tkinter library.

Sometimes loops of different libraries can coexist on the same process, with no conflicts -- but of course you will be either running one of them or the other. If so, it may be possible to run each library's mainloop in a different Python thread.

If they can not exist together, you will have to deal with each library in a different process.

So, one way to make the music player to start in another thread could be:

from threading import Thread

def real_playsound () :
    sound = pyglet.media.load('music.mp3')
    sound.play()
    pyglet.app.run()

def playsound():
    global player_thread
    player_thread = Thread(target=real_playsound)
    player_thread.start()

If Tkinter and pyglet can coexist, that should be enough to get your music to start. To be able to control it, however, you will need to implement a couple more things. My suggestion is to have a callback on the pyglet thread that is called by pyglet every second or so -- this callback checks the state of some global variables, and based on them chooses to stop the music, change the file being played, and so on.

Solution 2:

I would do something like:

import pyglet
from pyglet.gl import *

classmain (pyglet.window.Window):
    def__init__ (self):
        super(main, self).__init__(800, 600, fullscreen = False)
        self.button_texture = pyglet.image.load('button.png')
        self.button = pyglet.sprite.Sprite(self.button_texture)

        self.sound = pyglet.media.load('music.mp3')
        self.sound.play()

        self.alive = 1defon_draw(self):
        self.render()

    defon_close(self):
        self.alive = 0defon_mouse_press(self, x, y, button, modifiers):
        if x > self.button.x and x < (self.button.x + self.button_texture.width):
            if y > self.button.y and y < (self.button.y + self.button_texture.height):
                self.alive = 0defon_key_press(self, symbol, modifiers):
        if symbol == 65307: # [ESC]
            self.alive = 0defrender(self):
        self.clear()
        self.button.draw()
        self.flip()

    defrun(self):
        while self.alive == 1:
            self.render()

            # -----------> This is key <----------# This is what replaces pyglet.app.run()# but is required for the GUI to not freeze#
            event = self.dispatch_events()


x = main()
x.run()

Solution 3:

This solution is the easiest one:

import pyglet
foo=pyglet.media.load("/data/Me/Music/Goo Goo Dolls/[1998] Dizzy Up The Girl/11 - Iris.mp3")
foo.play()

defexiter(dt):
    pyglet.app.exit()
print"Song length is: %f" % foo.duration
# foo.duration is the song length
pyglet.clock.schedule_once(exiter, foo.duration)

pyglet.app.run()

source: http://ubuntuforums.org/showthread.php?t=1651906

Solution 4:

There is a media player implementation in the pyglet documentation:

http://www.pyglet.org/doc/programming_guide/playing_sounds_and_music.html

The script you should look at is media_player.py

Hopefully this will get you started

Post a Comment for "Playing Music With Pyglet And Tkinter In Python"