Python: 使用打开文件对话框将文件分配给变量?
Python: Using open file dialogue to assign file to a variable?
我四处搜索,发现了很多关于如何通过直接从 link 打开文件来将文件分配给变量的方法,但我想知道如何将文件分配给要读取的变量一个函数,当用户使用文件对话框打开文件时(例如文件 > 打开 > 用户选择的文件)?
我正在尝试打开要传递给 pyglet.media.load(variable containing file)
的音乐文件。
它returns错误:NameError: f is not defined
from tkinter import *
from tkinter.filedialog import askopenfilename
import pyglet
from threading import Thread
app = Tk()
app.title("Music Player")
app.geometry("600x200")
have_avbin = True
def openFile():
song = filedialog.askopenfilename(filetypes = (("MP3 files", "*.mp3"),("All files","*.*")))
f = song
return f
#Creates menu bar for opening MP3s, and closing the program
menu = Menu(app)
file = Menu(menu)
file.add_command(label='Open', command= openFile) # replace 'print' with the name of your open function
file.add_command(label='Exit', command=app.destroy) # closes the tkinter window, ending the app
menu.add_cascade(label='File', menu=file)
app.config(menu=menu)
#Run each app library mainloop in different python thread to prevent freezing
def playMusic():
global player_thread
player_thread = Thread(target=real_playMusic)
player_thread.start()
def stopMusic():
global player_thread
player_thread = Thread(target=real_stopMusic)
player_thread.start()
#Play open file function attached to button
def real_playMusic():
music = pyglet.media.load(f);
music.play()
pyglet.app.run()
#Stop the music function
def real_stopMusic():
pyglet.app.exit()
#Play button creation
btnPlay = Button(app, text ="Play", command = playMusic)
btnPlay.grid()
#Pause button creation
btnPause = Button(app)
btnPause.grid()
btnPause.configure(text = "Stop", command = stopMusic)
app.mainloop() # keep at the end
have_avbin = True
f='' # initialize this variable
def openFile():
global f # tell the function that we plan on modifying this global variable
f = filedialog.askopenfilename(filetypes = (("MP3 files", "*.mp3"),("All files","*.*")))
我四处搜索,发现了很多关于如何通过直接从 link 打开文件来将文件分配给变量的方法,但我想知道如何将文件分配给要读取的变量一个函数,当用户使用文件对话框打开文件时(例如文件 > 打开 > 用户选择的文件)?
我正在尝试打开要传递给 pyglet.media.load(variable containing file)
的音乐文件。
它returns错误:NameError: f is not defined
from tkinter import *
from tkinter.filedialog import askopenfilename
import pyglet
from threading import Thread
app = Tk()
app.title("Music Player")
app.geometry("600x200")
have_avbin = True
def openFile():
song = filedialog.askopenfilename(filetypes = (("MP3 files", "*.mp3"),("All files","*.*")))
f = song
return f
#Creates menu bar for opening MP3s, and closing the program
menu = Menu(app)
file = Menu(menu)
file.add_command(label='Open', command= openFile) # replace 'print' with the name of your open function
file.add_command(label='Exit', command=app.destroy) # closes the tkinter window, ending the app
menu.add_cascade(label='File', menu=file)
app.config(menu=menu)
#Run each app library mainloop in different python thread to prevent freezing
def playMusic():
global player_thread
player_thread = Thread(target=real_playMusic)
player_thread.start()
def stopMusic():
global player_thread
player_thread = Thread(target=real_stopMusic)
player_thread.start()
#Play open file function attached to button
def real_playMusic():
music = pyglet.media.load(f);
music.play()
pyglet.app.run()
#Stop the music function
def real_stopMusic():
pyglet.app.exit()
#Play button creation
btnPlay = Button(app, text ="Play", command = playMusic)
btnPlay.grid()
#Pause button creation
btnPause = Button(app)
btnPause.grid()
btnPause.configure(text = "Stop", command = stopMusic)
app.mainloop() # keep at the end
have_avbin = True
f='' # initialize this variable
def openFile():
global f # tell the function that we plan on modifying this global variable
f = filedialog.askopenfilename(filetypes = (("MP3 files", "*.mp3"),("All files","*.*")))