摄像头显示者tkinter段canvas中很简单,但要无闪烁,就要弄很久。
后来,发现,其实就一句话,也不知道为啥,反正在creat_image之后,加上obr就可以,即类似:
canvas_img.create_image(0, 0, anchor = NW ,image = cam_imageTk)
obr = cam_imageTk
不多说了,上全部代码:
from tkinter import *
import tkinter as tk
from PIL import ImageTk
from PIL import Image
import sys
import cv2
import os
import threading,time
def get_img(filename, width, height):
im = Image.open(filename).resize((width, height))
im = ImageTk.PhotoImage(im)
return im
def printkey(event):
print('你按下了: ' + event.char)
if event.char=='q':#按键盘q
print('你按下了: ' + event.char)
root.attributes("-fullscreen", False)
mainloop()
if event.char=='f':#按键盘f
print('你按下了: ' + event.char)
#root.attributes('-zoomed', True)
root.attributes("-fullscreen", True)
w = root.winfo_screenwidth()
h = root.winfo_screenheight()
#root.geometry("%dx%d" %(w,h))
#root.attributes("-topmost",True)
mainloop()
if event.char=='b':#按键盘b
print('你按下了: ' + event.char)
root.overrideredirect(False)#标题栏
mainloop()
if event.char=='t':#按键盘t
print('你按下了: ' + event.char)
root.overrideredirect(True)#去掉标题栏
mainloop()
if event.char=='e':#按键盘e
print('你按下了: ' + event.char)
#root.quit()
thr=False
capture.release()
cv2.destroyAllWindows()
root.destroy() # destroying the main window
#mainloop()
#sys.exit(0)
def main():
'''
注意: 背景图片im_root/im_root1这些,必须得是在主界面函数里进行加载,换个地方不行了,不知道为啥
'''
#root.attributes("-fullscreen", True) # substitute `Tk` for whatever your `Tk()` object is called
root.title("背景图展示")
sw = root.winfo_screenwidth()
#得到屏幕宽度
sh = root.winfo_screenheight()
#得到屏幕高度
ww = 400
wh = 600
#窗口宽高为600
x = (sw-ww) / 2
y = (sh-wh) / 2
root.geometry("%dx%d+%d+%d" %(ww,wh,x,y))
#root.geometry('400x600+180+100')
#root.resizable(False, False) #影响全屏切换
t=threading.Thread(target=cc)
t.start()
#canvas_camera = tk.Canvas(root, width=400, height=300)
#im_canvas_camera = get_img('./bg1.jpeg', 400, 600)
#canvas_camera.create_image(0,0, anchor='nw', image=im_canvas_camera)
#canvas_camera.bind('<Key>', printkey)
canvas_camera.pack()
# 设置背景图片
canvas_root = tk.Canvas(root, width=400, height=300)
im_root = get_img('./bg2.jpeg', 400, 600)
canvas_root.create_image(0,0, anchor='nw', image=im_root)
canvas_root.bind('<Key>', printkey)
canvas_root.pack()
# label 中设置图片
#im_root1 = get_img('./bg1.jpeg', 100, 40)
#img_label = Label(root, text='欢迎使用J波检测', image=im_root1)
#img_label.place(x=3, y=3, width=100, height=40)
#实例化一个输入框
#entry = Entry(root)
#给输入框绑定按键监听事件<Key>为监听任何按键 <Key-x>监听其它键盘,如大写的A<Key-A>、回车<Key-Return>
#entry.bind('<Key>', printkey)
#显示窗体
#entry.pack()
root.bind('<Key>', printkey)
#t=threading.Thread(target=cc)
#t.start()
mainloop()
def cc():
global capture,canvas_camera,image_file,frame,img,cv2image
#canvas_camera = tk.Canvas(root, width=400, height=300)
#cap = cv2.VideoCapture(0)
#canvas_root.create_image(0,0, anchor='nw', image=im_root)
#canvas_camera.bind('<Key>', printkey)
canvas_camera.pack()
while thr:
#_,frame= cap.read
#cov= cv2.cvtColor(frame,cv2.COLOR_RGB2BGR) #初始图像是RGB格式,转换成BGR即可正常显示了
#img=Image.fromarray(cov)
#img=ImageTk.PhotoImage(img)
ret, frame = capture.read()#从摄像头读取照片
frame = cv2.flip(frame, 1)#翻转 0:上下颠倒 大于0水平颠倒
cv2image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGBA)
img = Image.fromarray(cv2image)
image_file=ImageTk.PhotoImage(img)
canvas_camera.create_image(0,0,anchor='nw',image=image_file)
obr = image_file
#canvas_camera.pack()
#root.update_idletasks()
#root.update()
capture.release()
cv2.destroyAllWindows() # 基本操作
#capture.release()
#cv2.destroyAllWindows()
root.destroy() # destroying the main window
if __name__ == '__main__':
global thr, root, canvas_camera, canvas_root, capture,t,image_file
thr=True
root = Tk()
canvas_camera = tk.Canvas(root, width=400, height=300)
canvas_camera.pack()
canvas_root = tk.Canvas(root, width=400, height=600)
#root.overrideredirect(True)#去掉标题栏
capture = cv2.VideoCapture(0)
main()
#t=threading.Thread(target=cc)
#t.start()
#mainloop()