Python 仅当按下按钮时播放OpenCV视频

Python 仅当按下按钮时播放OpenCV视频,python,opencv,tkinter,Python,Opencv,Tkinter,我一直在尝试拍摄视频的帧,当按下按钮时,在TKinter中播放它们。但是,只要我运行应用程序,视频就会开始播放。有人能告诉我哪里出了问题吗 def __init__(self, window, delay): self.window = window self.delay = 15 window.title("User Interface") self.video_source = "movie.mov" self

我一直在尝试拍摄视频的帧,当按下按钮时,在TKinter中播放它们。但是,只要我运行应用程序,视频就会开始播放。有人能告诉我哪里出了问题吗

def __init__(self, window, delay):

        self.window = window
        self.delay = 15
        window.title("User Interface")
        self.video_source = "movie.mov"
        self.vid = 0
        self.canvas = Canvas(window, width = 400, height = 400)
        self.canvas.pack()
        self.hiButton = Button(window, text="hello", command = self.callback)
        self.hiButton.pack()
        self.getFeedButton = Button(window, text = "Get Feed", \
                                    command = self.feedCallBack(window,"movie.mov"))
        self.getFeedButton.pack()

    def update(self):
        ret, frame = self.get_frame()

        if ret:
            self.photo = ImageTk.PhotoImage(image = Image.fromarray(frame))
            self.canvas.create_image(0, 0, image = self.photo, anchor = tkinter.NW)
        self.window.after(self.delay, self.update)
    def callback(self):
        self.guess = Test()
        print('hi')

    def feedCallBack(self, window, video_source):
        self.vid = cv2.VideoCapture(video_source)
        self.update()

        #self.window.after(self.delay, self.update)
    def get_frame(self):
        ret, frame = self.vid.read()
        if ret:
            return (ret, cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
        else:
            return (ret, None)
从这段代码中,我认为视频只会在点击getFeed时播放,但事实并非如此

self.getFeedButton = Button(window, text = "Get Feed", \
                                    command = self.feedCallBack(window,"movie.mov"))
应该是

self.getFeedButton = Button(window, text = "Get Feed", \
                                    command = lambda: self.feedCallBack(window,"movie.mov"))
使用lambda函数

 self.getFeedButton = Button(window, text = "Get Feed", 
                                command = lambda: self.feedCallBack(window,"movie.mov"))
 self.getFeedButton.pack()

非常感谢。真管用!你能解释一下lambda表达式是如何引起这种变化的吗?
function
是一个对象。在你的代码中,
command=self.feedCallBack(窗口,“movie.mov”)
意味着当python运行它时,它将调用函数,最后,
按钮的实际
command
feedCallBack的返回值(窗口,“movie.mov”)
。阅读