1

I have a python script that plays a sound file from the computer chosen by the user, when a tkinter button is pressed. Now I want to put the sound file that was browsed using the filedialog from tkinter in a variable that can be accessed not only by another tkinter button to be played as a command, but have the pygame receive input from keypressed to play the sound chosen by the user.

So the user has a browser sound button to search for the sound file, AND after that he can play the sound using another button from tkinter OR he can play that chosen sound pressing a key in the computer, for example the p key. I attached my code for help. Thanks for any help!!!

import pygame
from tkinter import *
from tkinter import filedialog

pygame.mixer.init(frequency=44100, size=-16, channels=2, buffer=512)
pygame.init()

root = Tk()

audio_file_name1 = ''

def open_masker1():
    global audio_file_name1
    audio_file_name1 = filedialog.askopenfilename(filetypes=(("Audio Files", ".wav .ogg"),   ("All Files", "*.*")))

def playsound1():
    # we will also use the audio_file_name global variable
    global audio_file_name1

    if audio_file_name1:  # play sound if just not an empty string
        noise = pygame.mixer.Sound(audio_file_name1)
        noise.play()

b1 = Button(root, text = 'open file',command = open_masker1)  # browser button 1
b1.pack(anchor=CENTER)

p1 = Button(root, text = 'Som1', command = playsound1)  # playsound1
p1.pack(anchor=W)

root.mainloop()
wim
  • 302,178
  • 90
  • 548
  • 690
Thiago
  • 11
  • 2

0 Answers0