1、添加背景
#插入文件图片 import tkinter as tk root = tk.Tk() #创建一个标签类, [justify]:对齐方式 textLabel = tk.Label(root,text="你在右边会看到一个图片,\n我在换个行", justify = tk.LEFT)#左对齐 textLabel.pack(side=tk.LEFT)#自动对齐,side:方位 #创建一个图片管理类 photo = tk.PhotoImage(file="18.png")#file:t图片路径 imgLabel = tk.Label(root,image=photo)#把图片整合到标签类中 imgLabel.pack(side=tk.RIGHT)#自动对齐 tk.mainloop()效果:
代码: from tkinter import * import cv2 from PIL import Image,ImageTk def take_snapshot(): print("有人给你点赞啦!") def video_loop(): success, img = camera.read() # 从摄像头读取照片 if success: cv2.waitKey(100) cv2image = cv2.cvtColor(img, cv2.COLOR_BGR2RGBA)#转换颜色从BGR到RGBA current_image = Image.fromarray(cv2image)#将图像转换成Image对象 imgtk = ImageTk.PhotoImage(image=current_image) panel.imgtk1 = imgtk panel.config(image=imgtk) root.after(1, video_loop) camera = cv2.VideoCapture(0) #摄像头 root = Tk() root.title("opencv + tkinter") #root.protocol(\'WM_DELETE_WINDOW\', detector) panel = Label(root) # initialize image panel panel.pack(padx=10, pady=10) # root.config(cursor="arrow") btn = Button(root, text="点赞!", command=take_snapshot) btn.pack(fill="both", expand=True, padx=10, pady=10) video_loop() root.mainloop() # 当一切都完成后,关闭摄像头并释放所占资源 camera.release() cv2.destroyAllWindows()