在图像框架上覆盖 tkinter 小部件

Overlaying a tkinter widget on an image frame

我有一个应用程序,其中我必须在单击按钮时弹出一个 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()

不知道这是不是你要找的效果:

我使用 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()