添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

Unable to save file path using tkinter filedialog askopenfilename, triggred through a button

Ask Question

I need to save the file path returned by askopenfilename within tkinter filedialog for later use I trigger the function openFile via a button which I define and I expect this function to store the requested file's path, I tried using global variable and StringVar() too but It didn't work it gives empty string, the global variable path is where I need file path string.

code :

from tkinter import *
from PIL import ImageTk, Image
import cv2
from matplotlib.pyplot import text
# from Main import pipeline
# import Object_Detection
# from Object_Detection import Simulation
from tkinter import filedialog
root = Tk()
# Create a frame
app = Frame(root, bg="white")
app.grid()
# Create a label in the frame
lmain = Label(app)
lmain.grid()
path = StringVar()
def openFile():
    file_path = filedialog.askopenfilename()
    path.set(file_path)
button = Button(text="Open a video file",command=openFile)
button.grid()
print(path.get())
cap = cv2.VideoCapture('.\\Datasets\\video_datasets\\drive0.mp4')
# det = Object_Detection.ObjectDetection()
# function for video streaming
def video_stream():
    _, frame = cap.read()
    # image_box,classes,scores,boxes = pipeline(frame,det)
    # Simulation().DistanceEstimation(boxes,classes,scores,image_box)
    cv2image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGBA)
    img = Image.fromarray(cv2image)
    imgtk = ImageTk.PhotoImage(image=img)
    lmain.imgtk = imgtk
    lmain.configure(image=imgtk)
    lmain.after(1, video_stream) 
video_stream()
root.mainloop()

where exactly are you trying to grab the path in your code? The print statement "print(path.get())" on line 29 will get executed before your file_path and path variables hold any values. So natrually it will result in an empty print statement.

Try this: I added a bottun to print the path to terminal. Ofc it will only work if beforehand the file has been selected in openFile()

def openFile():
    file_path = filedialog.askopenfilename()
    path.set(file_path)
    print(path.get(), 'in openFile')
def printpath():
    print(path.get(), 'printpath')
button = Button(text="Open a video file", command=openFile)
button.grid()
button = Button(text="print path", command=printpath)
button.grid()
                once button = Button(text="Open a video file",command=openFile)  is executed path.get() should be able to give me the string but it doesn't notice how command=openFile calls the function before we access path.get()
– Pawan Nirpal
                Mar 15, 2022 at 17:49
                Well, it does. At least for me. After openfile() has been executed you should be able to access path.get() as my example function printpath() confirmes.
– Dan Green
                Mar 15, 2022 at 17:59
                @PawanNirpal When button = Button(text="Open a video file",command=openFile) is executed, it only creates the button and openFile() will not be executed. So path is not updated yet.
– acw1668
                Mar 16, 2022 at 4:22
    file_path = filedialog.askopenfilename()
    path.set(file_path)
    print(path.get(), 'in openFile')
    root.after(2000, printpath)
def printpath():
    print(path.get(), 'printpath')
    # root.after(2000, printpath)
button = Button(text="Open a video file", command=openFile)
button.grid()
# button = Button(text="print path", command=printpath)
# button.grid()
root.mainloop()
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.