【发布时间】:2022-01-14 14:59:51
【问题描述】:
我使用 Proxlight Designer 创建拖放式 GUI。这是一个适用于 open-cv-python 的应用程序,但未显示 Button。似乎 open-cv 是问题所在,因为如果将其删除,则 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()
【问题讨论】:
-
不要使用
place完全没有必要,可能按钮隐藏在带有图像的标签之类的东西下,最好使用mainloop而不是@ 987654324@ 与update循环,有一个after方法用于“循环” -
你在while循环中覆盖了
img1,所以使用img1的按钮不起作用。其他两个按钮工作正常。与其他评论一样,在 tkinter 应用程序的主线程中应避免使用 while 循环。
标签: python-3.x tkinter opencv-python photoimage