我期待视频捕获刷新屏幕。当它不是一个班级时,它按照我的预期工作。然后我将它转换为oop-below。 它没有刷新......然后我期待"之后#34;会做的工作。显然,我不明白后工作怎么样?它不是在我的情况下 - 在root. root.vidImg.vid()下连续调用。如果是,为什么self.cap = cv2.VideoCapture(0)不捕获并显示新图像? 感谢您提前帮助
这是我的代码......
import tkinter as tk
from PIL import Image, ImageTk
import cv2
class Window(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
self.videoFrm = tk.Frame(self, width=480, bg='yellow', height=320)
self.videoFrm.grid(row=0, column=0, sticky=tk.NW)
self.vidImg = cnvs(self.videoFrm,self)
self.vidImg.grid()
class cnvs(tk.Frame):
def __init__(self,parent,controller):
tk.Frame.__init__(self,parent)
self.controller = controller
self.canvas = tk.Canvas(self, width=480, height=320, bg='green')
self.canvas.grid()
def vid(self):
# Capture video frames
self.cap = cv2.VideoCapture(0)
_, self.frame = self.cap.read()
self.img = cv2.flip(self.frame, 1)
self.cv2image = cv2.cvtColor(self.img, cv2.COLOR_BGR2RGBA)
self.cv2image = cv2.resize(self.cv2image, (0, 0), fx=0.2, fy=0.2)
self.img = Image.fromarray(self.cv2image)
self.imgtk = ImageTk.PhotoImage(image=self.img)
self.canvas.create_image(10, 10, anchor=tk.NW, image=self.imgtk)
def main():
root = Window()
root.after(0, root.vidImg.vid())
root.mainloop()
if __name__ == '__main__':
main()
答案 0 :(得分:0)
要创建animation loop with the after
method,您需要让动画函数多次调用自身。
在您的情况下,您可以将以下方法添加到cnvs
类:
def video_loop(self):
self.vid()
self.controller.after(10, self.video_loop)
然后在mainloop()
之前调用它。
此外,不是为每个框架向画布添加新图片,而是canvas.itemconfig(...)
使用let gender = ["M","F"]
@IBOutlet weak var dataTableView: UITableView!
//pickerFuncs
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return gender[row]
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return gender.count
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
print(gender[row])
}
可能更好。