使用 tkinter 将文件路径保存在变量中
Saving file path in a variable using tkinter
我正在使用 tkinter 为我拥有的旧脚本创建 GUI,但我现在卡住了。
我想单击一个按钮打开 "Search File" window,选择一个特定的文件并将其路径保存在一个变量中。
我的代码能够打开 window,然后我可以 select 文件并显示它的路径,但我找不到将此路径保存在变量中的方法。
这是我的:
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()
有谁知道这样做的好方法吗?
使用全局变量:
from tkinter import *
from tkinter import filedialog
def get_file_path():
global 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()
print(file_path)
一旦您的 windows 关闭,您将获得 file_path。
您可以在 get_file_path
中将 file_path
声明为全局变量
def get_file_path():
global 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()
然后您可以从脚本中的任何位置访问该变量
------编辑------
根据你在评论中所说的,我说你可以使用 tkinter.StringVar
来保存文件路径,然后在调用 count_frames
作为参数时访问它。
可以这样实现:
from tkinter import *
from tkinter import filedialog
file_path_var = StringVar()
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")))
file_path_var.set(file_path) #setting the variable to the value from file path
#Now the file_path can be acessed from inside the function and outside
file_path_var.get() # will return the value stored in file_path_var
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()
# will also return the value saved in file_path_var
file_path_var.get()
window.mainloop()
print(file_path)
现在,无论您的 count_frames
功能在哪里,只要您首先选择了一个文件,您就应该能够做到
count_frames(file_path_var.get())
我正在使用 tkinter 为我拥有的旧脚本创建 GUI,但我现在卡住了。 我想单击一个按钮打开 "Search File" window,选择一个特定的文件并将其路径保存在一个变量中。 我的代码能够打开 window,然后我可以 select 文件并显示它的路径,但我找不到将此路径保存在变量中的方法。 这是我的:
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()
有谁知道这样做的好方法吗?
使用全局变量:
from tkinter import *
from tkinter import filedialog
def get_file_path():
global 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()
print(file_path)
一旦您的 windows 关闭,您将获得 file_path。
您可以在 get_file_path
file_path
声明为全局变量
def get_file_path():
global 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()
然后您可以从脚本中的任何位置访问该变量
------编辑------
根据你在评论中所说的,我说你可以使用 tkinter.StringVar
来保存文件路径,然后在调用 count_frames
作为参数时访问它。
可以这样实现:
from tkinter import *
from tkinter import filedialog
file_path_var = StringVar()
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")))
file_path_var.set(file_path) #setting the variable to the value from file path
#Now the file_path can be acessed from inside the function and outside
file_path_var.get() # will return the value stored in file_path_var
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()
# will also return the value saved in file_path_var
file_path_var.get()
window.mainloop()
print(file_path)
现在,无论您的 count_frames
功能在哪里,只要您首先选择了一个文件,您就应该能够做到
count_frames(file_path_var.get())