PhotoImage Tkinter问题:按钮不工作且不显示



im使用Proxlight Designer创建Drag-n-drop GUI。这是一个与opencv-python一起工作的应用程序,但没有显示Button。似乎打开简历是个问题,因为如果你删除它,按钮就会显示并正常工作。这是GUI的代码:

cap = cv2.VideoCapture(0)
window = Tk()
window.geometry("700x800")
window.configure(bg = "#ffffff")
canvas = Canvas(
window,
bg = "#ffffff",
height = 800,
width = 700,
bd = 0,
highlightthickness = 0,
relief = "ridge")
canvas.place(x = 0, y = 0)
l1 = Label(bg = "black")
l1.place(x = 100, y = 150, width = 500, height = 500)
img0 = PhotoImage(file = f"RES/img1.png")
b0 = Button(
image = img0,
borderwidth = 0,
highlightthickness = 0,
command = save_face,
relief = "flat")
b0.place(
x = 250, y = 693,
width = 200,
height = 75)
img1 = PhotoImage(file = f"RES/img2.png")
b1 = Button(
image = img1,
borderwidth = 0,
highlightthickness = 0,
command = encryptPass,
relief = "flat")
b1.place(
x = 480, y = 693,
width = 200,
height = 75)
img2 = PhotoImage(file = f"RES/img3.png")
b2 = Button(
image = img2,
borderwidth = 0,
highlightthickness = 0,
command = generate_key,
relief = "flat")
b2.place(
x = 20, y = 693,
width = 200,
height = 75)
window.resizable(False, False)
while True:
img = cap.read()[1]
img1 = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
img = ImageTk.PhotoImage(Image.fromarray(img1))
l1["image"] = img
window.update()

所以多亏了@Matiss和@acw1668(thx alot btw(的评论,我才得以工作。基本上while循环是个问题。我用这个而不是while循环修复了它:

cap = cv2.VideoCapture(0, cv2.CAP_DSHOW)
def show_frame():
_, frame = cap.read()
global cv2image
cv2image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
img = Image.fromarray(cv2image)
imgtk = ImageTk.PhotoImage(image=img)
l1.imgtk = imgtk
l1.configure(image=imgtk)
l1.after(10, show_frame)
window = Tk()
window.geometry("700x800")
window.configure(bg = "#ffffff")
l1 = Label(bg = "black")
l1.place(x = 100, y = 150, width = 500, height = 500)
window.resizable(False, False)
show_frame()
window.mainloop()

最新更新