0

I created a window that captures the webcam by Tkinter, and I wanted to put transparent buttons in it. I joined the camera code + the one I found on the site (Transparent Backgrounds on Buttons in Tkinter), individually both work, but together I get the error "'Label' object has no attribute 'create_image'"

What could I do please?

from PIL import Image,ImageTk
import pytesseract
import cv2
import tkinter as tk
from tkinter import *
#from tkinter.ttk import *
from PIL import Image, ImageTk

#Create the window and camera
width, height = 1920,1080 #800, 600
cap = cv2.VideoCapture(0)
cap.set(cv2.CAP_PROP_FRAME_WIDTH, width)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, height)
root = Tk()
root.geometry("1920x1080")  #adc isso
#root.attributes('-alpha',0.5) deixa transpartente
root.bind('<Escape>', lambda e: root.quit())
lmain = Label(root)
lmain.pack()
def show_frame():
    _, frame = cap.read()
    frame = cv2.flip(frame, 1)
    cv2image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
    cv2image = cv2.resize(frame, (1000,700))
    img = PIL.Image.fromarray(cv2image)
    imgtk = ImageTk.PhotoImage(image=img)
    lmain.imgtk = imgtk
    lmain.configure(image=imgtk)
    lmain.after(10, show_frame)


#Create the transparent buttons
def exit(event):
    window.destroy()
#creating button which supports png transparency
b1Image = ImageTk.PhotoImage(Image.open("Design sem nome.gif"))
b1Button = lmain.create_image(50, 50, image=b1Image)
lmain.tag_bind(b1Button, "<Button-1>", exit)




show_frame()
root.mainloop()
Christoph Rackwitz
  • 5,130
  • 3
  • 17
  • 27
Julia
  • 11
  • 5
  • The error is telling the truth. A Label object does not have a `create_image` method. Why do you think it does, or are you assuming that `lmain` is a canvas when it is not? – Bryan Oakley Apr 25 '22 at 03:20
  • https://stackoverflow.com/questions/29857757/transparent-backgrounds-on-buttons-in-tkinter in this tutorial it worked, what's different about mine besides the camera?? – Julia Apr 25 '22 at 03:28
  • That example is calling `create_image` on an instance of `Canvas`, which has that method. You're calling it on an instance of `Label`, which doesn't. – Bryan Oakley Apr 25 '22 at 03:44
  • I researched more about it and managed to understand, thank you! sorry for the silly mistake – Julia Apr 25 '22 at 22:11

0 Answers0