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

时间:2018-07-13 10:36:23

标签: python python-3.x tkinter tk

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