我有一个应用程序,其中,我必须在单击按钮时弹出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()
答案 0 :(得分:0)
我不知道这是否对您有帮助:
我使用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()