【问题标题】:How to return value from a widget`s function, and pass it to another widget's function in Tkinter, Python如何从小部件的函数返回值,并将其传递给 Tkinter,Python 中的另一个小部件的函数
【发布时间】:2019-06-22 05:28:36
【问题描述】:

我正在创建一个包含两个按钮的简单 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()   

当我选择一个视频文件时,它的路径会被打印出来。但。当我点击播放按钮时,会显示错误信息(请选择一个视频文件)。

【问题讨论】:

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


    【解决方案1】:

    您需要在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()
    

    【讨论】:

    • 谢谢。有效。有没有其他方法来处理这个?如果我有几个变量,也许使用全局变量并不能让我对函数之间的变量流进行足够的控制。我试过“lambda”,但它似乎只在输入参数是参数而不是变量时才有效。
    • 您可以使用 lambda 轻松地将可变变量传递给函数。请分享您尝试了什么,什么失败了。
    • 谢谢。有用。但是,我不明白“file_record”是如何在函数之间流动的。
    • 这就是可变变量类型的特点。字典、列表是可变类型,因此当您将它们传递给函数时,可以从函数中访问和更改原始变量的值;但是对于像int这样的非可变类型,只有字符串变量的值被传递给函数,所以原始变量的值不能改变。
    猜你喜欢
    • 2020-08-16
    • 1970-01-01
    • 2018-03-10
    • 2019-04-18
    • 2020-02-22
    • 2020-06-21
    • 2021-04-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多