简体   繁体   中英

How to return value from a widget`s function, and pass it to another widget's function in Tkinter, Python

I am creating a simple GUI including two buttons. First button is for selecting a video file, and the second button gets the video file path and then play it (using OpenCV).

The problem is that I cannot return the file path from the first button binding function and pass it into the second button binding function.

I defined "filename" as a Global Variable, but still "filename" is not defined in the "PlayVideo()" function.

Following is my code:

from tkinter import *
from tkinter import filedialog
from tkinter import messagebox

global filename


def OpenFile():
    filename =  filedialog.askopenfilename(title = "Select file",filetypes = ( ("MP4 files","*.mp4"), ("WMV files","*.wmv"), ("AVI files","*.avi") ))
    print(filename)


def PlayVideo():
    try:
        import cv2

        cap = cv2.VideoCapture(filename)

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

selectButton = Button(root, text = 'Select video file', command=OpenFile)
playButton = Button(root, text = 'Play', command=PlayVideo)

selectButton.grid(row=0)
playButton.grid(row=1)

root.mainloop()   

When I select a video file, its path is printed. But. when I click the Play button, the error message (Please select a video file) is shown.

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 filedialog
from tkinter import messagebox

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM