2

I have an application wherein, I have to pop a Spinbox widget on click of a button. The widget needs to be overlayed on a background which is an image. I have tried with the code below, but the widget does not appear on the click of the button. I believe the image display is taking precedence over the widget display.

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()
Billal Begueradj
  • 20,717
  • 43
  • 112
  • 130
  • Have a look at [How to use an image for the background in tkinter?](https://stackoverflow.com/questions/10158552/how-to-use-an-image-for-the-background-in-tkinter) – figbeam Jul 13 '18 at 12:09
  • But if the image is updated, like while playing a vide feed, the condition will not hold good – shubham jaiswal Jul 13 '18 at 12:48
  • Do you mean: you want the image to appear as a background of the spinbox? – Billal Begueradj Jul 14 '18 at 06:11
  • No, what I want is,when I press the button, the spinbox should be overlayed on top of the background(be it an image or a video), and should remain overlayed until I press the button again. I have a video being played in a tkinter frame and at times it requires me to introduce some momentary delay for which I am using the spinbox to provide a delay. Currently, when I am pressing the button, the spinbox is not appearing. – shubham jaiswal Jul 14 '18 at 06:28

1 Answers1

0

I do not know if this is effect you are looking for:

enter image description here

I used place() and place_forget() to achieve that:

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()
Billal Begueradj
  • 20,717
  • 43
  • 112
  • 130
  • Thank you, it is what I am looking for, but could you also elaborate as to why the previous code was not working? Is it that the image takes precedence over the spinbox widget? – shubham jaiswal Jul 15 '18 at 04:44
  • The previous code did not work because you used the `pack()` geometry manager which, by default (as in your case), stacks the widgets from top to down following the order of packing them. You packed the spinbox after the label, so that is the expected result. You will get the same result if you used `grid()`, so `place()` is your only friend here, and this is one of the rare cases where `place()` is more useful than `pack()` and `grid()` @shubhamjaiswal – Billal Begueradj Jul 15 '18 at 05:08