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
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()
–
–
–
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.