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

Time:03-16

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

CodePudding user response:

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

CodePudding user response:

Yes, i can :)

from tkinter import *
from tkinter import filedialog

root = Tk()
app = Frame(root, bg="white")
app.grid()
lmain = Label(app)
lmain.grid()

path = StringVar()

def openFile():
    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()
  • Related