【问题标题】:Saving file path in a variable using tkinter使用 tkinter 将文件路径保存在变量中
【发布时间】: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()

有谁知道这样做的好方法吗?

【问题讨论】:

  • 你的函数没有返回任何东西。使用全局也是一种选择。

标签: python tkinter filepath


【解决方案1】:

使用全局变量:

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)

关闭窗口后,您将获得文件路径。

【讨论】:

  • 这确实有效。但是,如果我想使用“file_path”作为其他函数的参数,则返回它仍未定义。如果我在b1 = Button(window, text = "Open File", command = get_file_path).pack() 下使用l4 = Label(window, text = "This video has " + str(count_frames(file_path)) + " frames."),我会遇到这个问题(count_frames 返回给定文件路径中视频的帧数)。如何改进代码以避免这种情况?
【解决方案2】:

您可以在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())

【讨论】:

  • 嘿 Jojo 正如我对 Panda50 所说,这确实有效。但是,如果我尝试使用这个全局变量作为另一个函数的参数,我仍然会遇到同样的问题。如果我使用 l4 = Label(window, text = "This video has " + str(count_frames(file_path)) + " frames.") ,它仍然不起作用(count_frames 返回给定文件路径中视频的帧数)。
猜你喜欢
  • 1970-01-01
  • 2023-03-15
  • 2021-10-31
  • 2023-03-04
  • 2021-08-31
  • 1970-01-01
  • 1970-01-01
  • 2018-07-28
  • 1970-01-01
相关资源
最近更新 更多