【发布时间】:2020-04-19 23:14:40
【问题描述】:
我正在使用 tkinter 为我拥有的旧脚本创建 GUI,但我现在卡住了。 我想单击一个按钮打开“搜索文件”窗口,选择一个特定文件并将其路径保存在变量中。 我拥有的代码能够打开窗口,然后我可以选择文件并显示它的路径,但我找不到将此路径保存在变量中的方法。 这就是我所拥有的:
from tkinter import *
from tkinter import filedialog
def get_file_path():
# Open and return file path
file_path= filedialog.askopenfilename(title = "Select A File", filetypes = (("mov files", "*.png"), ("mp4", "*.mp4"), ("wmv", "*.wmv"), ("avi", "*.avi")))
l1 = Label(window, text = "File path: " + file_path).pack()
window = Tk()
# Creating a button to search the file
b1 = Button(window, text = "Open File", command = get_file_path).pack()
window.mainloop()
有谁知道这样做的好方法吗?
【问题讨论】:
-
你的函数没有返回任何东西。使用全局也是一种选择。