Advertisement
skip420

MusicPlayer

Sep 15th, 2021
872
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.38 KB | None | 0 0
  1. #python 2 or 3 audio.py
  2. #play music with python
  3. # add any png,jpg image as a buttonn to turn off
  4.  
  5.  
  6. import pyglet
  7. from pyglet.gl import *
  8.  
  9. class main (pyglet.window.Window):
  10.     def __init__ (self):
  11.         super(main, self).__init__(800, 600, fullscreen = False)
  12.         self.button_texture = pyglet.image.load('button.jpg')
  13.         self.button = pyglet.sprite.Sprite(self.button_texture)
  14.  
  15.         self.sound = pyglet.media.load('Amon.mp3')
  16.         self.sound.play()
  17.  
  18.         self.alive = 1
  19.  
  20.     def on_draw(self):
  21.         self.render()
  22.  
  23.     def on_close(self):
  24.         self.alive = 0
  25.  
  26.     def on_mouse_press(self, x, y, button, modifiers):
  27.         if x > self.button.x and x < (self.button.x + self.button_texture.width):
  28.             if y > self.button.y and y < (self.button.y + self.button_texture.height):
  29.                 self.alive = 0
  30.  
  31.     def on_key_press(self, symbol, modifiers):
  32.         if symbol == 65307: # [ESC]
  33.             self.alive = 0
  34.  
  35.     def render(self):
  36.         self.clear()
  37.         self.button.draw()
  38.         self.flip()
  39.  
  40.     def run(self):
  41.         while self.alive == 1:
  42.             self.render()
  43.  
  44.             # -----------> This is key <----------
  45.             # This is what replaces pyglet.app.run()
  46.             # but is required for the GUI to not freeze
  47.             #
  48.             event = self.dispatch_events()
  49.  
  50.  
  51. x = main()
  52. x.run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement