How to return value from a widget`s function, and pass it to another widget's function in Tkinter, Python
You need to add this line at the start of both functions OpenFile
and PlayVideo
global filename
When you add this line, your program knows that instead of creating/using a local variable "filename" in that function, it has to use the global variable "filename".
UPDATE:
To avoid using global variables, you can use mutable type like this.
from tkinter import *from tkinter import filedialogfrom tkinter import messageboxdef OpenFile(file_record): file_record['video1'] = filedialog.askopenfilename(title = "Select file",filetypes = ( ("MP4 files","*.mp4"), ("WMV files","*.wmv"), ("AVI files","*.avi") )) print(file_record['video1'])def PlayVideo(file_record): try: import cv2 cap = cv2.VideoCapture(file_record['video1']) while(cap.isOpened()): ret, frame = cap.read() cv2.imshow('frame', frame) if cv2.waitKey(25) & 0xFF == ord('q'): break cap.release() cv2.destroyAllWindows() except: messagebox.showinfo(title='Video file not found', message='Please select a video file.')root = Tk()filename_record = {}selectButton = Button(root, text = 'Select video file', command=lambda: OpenFile(filename_record))playButton = Button(root, text = 'Play', command=lambda: PlayVideo(filename_record))selectButton.grid(row=0)playButton.grid(row=2)root.mainloop()