如何从窗口小部件的函数返回值,并将其传递给Tkinter,Python中的另一个窗口小部件的函数

时间:2019-01-29 06:29:55

标签: python tkinter command return-value video-capture

我正在创建一个包含两个按钮的简单GUI。 第一个按钮用于选择视频文件,第二个按钮获取视频文件路径,然后播放(使用OpenCV)。

问题是我无法从第一个按钮绑定函数返回文件路径并将其传递给第二个按钮绑定函数。

我将“文件名”定义为全局变量,但在“ PlayVideo()”函数中仍未定义“文件名”。

以下是我的代码:

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

当我选择一个视频文件时,将打印其路径。但。当我单击“播放”按钮时,将显示错误消息(请选择一个视频文件)。

2 个答案:

答案 0 :(得分:0)

您需要在两个函数OpenFilePlayVideo的开头添加此行

global filename

添加此行后,您的程序就会知道,不必在该函数中创建/使用局部变量“文件名”,而必须使用全局变量“文件名”。

更新:

为避免使用全局变量,可以使用这样的可变类型。

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

答案 1 :(得分:0)

我还尝试了如下的“ lambda”。但是,问题是我无法从“ OpenFile”函数返回“文件名”。因此,未为“ PlayVideo”功能定义“文件名”。

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

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

def PlayVideo(filename):

    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= lambda: PlayVideo(filename))

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

root.mainloop()