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

问题描述 投票:0回答:2

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

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

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

以下是我的代码:

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

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

python tkinter command return-value video-capture
2个回答
0
投票

我也试过“lambda”如下。但是,问题是我不能从“OpenFile”函数返回“filename”。因此,“文件名”未定义为“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()  

0
投票

你需要在两个函数OpenFilePlayVideo的开头添加这一行

global filename

当你添加这一行时,程序知道不是在该函数中创建/使用局部变量“filename”,而是必须使用全局变量“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()
© www.soinside.com 2019 - 2024. All rights reserved.