首页 文章

在图像框架上叠加tkinter小部件

提问于
浏览
1

我有一个应用程序,其中,我必须点击一个按钮弹出一个 Spinbox 小部件 . 小部件需要覆盖在作为图像的背景上 . 我已尝试使用下面的代码,但单击按钮时不显示小部件 . 我相信图像显示优先于小部件显示 .

import tkinter as tk
import cv2
from PIL import Image,ImageTk
top = tk.Tk()
count = 1
image = cv2.imread("frames/0.jpg")
w = tk.Spinbox(top, from_=0, to=10)
def helloCallBack():
    global count,w
    if count%2 != 0:
        w.pack()

    else:
        w.forget()

    print(count)    
    count+=1


B = tk.Button(top, text ="Hello", command = helloCallBack)

B.pack()

label = tk.Label(top)
label.pack()

img = Image.fromarray(image)
imgtk = ImageTk.PhotoImage(image=img)
label.imgtk = imgtk
label.configure(image=imgtk)
top.update()

top.mainloop()

1 回答

  • 0

    我不知道这是否是您正在寻找的效果:

    enter image description here

    我使用 place()place_forget() 来实现:

    import tkinter as tk
    import cv2
    from PIL import Image,ImageTk
    
    
    top = tk.Tk()
    count = 1
    
    def helloCallBack():
        global count,w
        if count%2 != 0:        
            w.place(x=180, y=650)
        else:
            w.place_forget()
    
        print(count)    
        count+=1
    
    B = tk.Button(top, text ="Hello", command = helloCallBack)
    B.pack()
    
    label = tk.Label(top)
    label.pack()
    
    image = cv2.imread("frames/0.jpg")
    img = Image.fromarray(image)
    imgtk = ImageTk.PhotoImage(image=img)
    label.imgtk = imgtk
    label.configure(image=imgtk)
    
    w = tk.Spinbox(top, from_=0, to=10)
    
    top.update()
    
    top.mainloop()
    

相关问题