Before the rest of the answer, I would like to preface that global variables can be dangerous if you are not careful. With that being said, you could use a global variable for this. Here is a simple example of utilising a global variable with tkinter.
import tkinter
changing_value = 0
def change():
global changing_value
changing_value += 1
def printer():
print(changing_value)
window = tkinter.Tk()
change_button = tkinter.Button(window, command=change, text="Change")
change_button.pack()
print_button = tkinter.Button(window, command=printer, text="Print")
print_button.pack()
window.mainloop()
As you may have noticed, I only declared changing_value
as global
in change()
. This is since variable assignments would normally create a local scope value that is then lost when the function scope ends. Declaring it a global variable allows it to change the variable in the global scope, thus allowing the value used by printer()
to change since it accesses the global scope variable by default. Something to consider is how to handle the value before one is initially assigned, that may be an issue if you don't have a default and the function which uses the value could be invoked before the one assigning it.
An alternative is using a mutable object and changing/accessing the value stored in the mutable object, but in my opinion that is just global variables with extra steps.
Applying the example to yours:
def select_file():
global filename
filetypes = (
('mp3 files', '*.mp3'),)
filename = fd.askopenfilename(
title='Select audio', initialdir='/',
filetypes=filetypes)
def play():
p = vlc.MediaPlayer(filename)
p.play()
time.sleep(60)
#play button
play_button = tk.Button(root, text='Play', command=play)
play_button.place(x=20, y=10)