Bhuwan pandey Bhuwan pandey - 4 months ago 20
Python Question

Adding widgets over canvas in tkinter

I want to put a small image and other widgets over a canvas on which an image is displayed. I've tried options such as

compound
and other things.

Background picture is fine and the small image that I want to put over the background image shows fine but it's always top or bottom of the window. I want it to be placed over any area of background image. I've tried many options of all the geometry manager (pack, grid, place) but none of them works. Please help, here's my code :

from Tkinter import *

root = Tk()
root.iconbitmap('E:/a.ico')
root.title('Unick Locker')

canvas = Canvas(root, width=730, height=600)
canvas.grid()

bgImg = PhotoImage(file="E:/a.gif")

canvas.create_image(370, 330, image=bgImg)

login = PhotoImage(file="E:/login.gif")

lo = Label(root, image=login)
lo.grid()

root.mainloop()

Answer

In order to add any widgets over or the foreground of any background image or canvas, the row and column values of all the widgets must be same as of the background image. so, my above mentioned program would be like this :

from Tkinter import *

root = Tk()

root.iconbitmap('E:/a.ico')

root.title('Unick Locker')

canvas = Canvas(root, width=730, height=600)

canvas.grid(row=0, column=0)

bgImg = PhotoImage(file="E:/a.gif")

canvas.create_image(370, 330, image=bgImg)

login = PhotoImage(file="E:/login.gif")

lo = Label(root, image=login)

lo.grid(row=0, column=0)

root.mainloop()

I tried putting the same row and column values to the widgets in grid() methods which I wanted to put over the image, and it worked fine as I wanted :-)