hey i am working with python 3 and i am trying to set a gif file as my GUI background. The canvas.configure(bg=myimage) does not work. Any ideas?

width, height = 800, 500

canvas = tkinter.Canvas(width=width, height=height, bg="black")
canvas.pack(fill=None, expand=False)

#Sets the backround image
myimage = PhotoImage(file='world.gif')
canvas.configure(bg=myimage)

Dani AI

Generated

Canvas.bg only accepts a color name, not an image. To use a GIF as a background on a Canvas, create an image item and give it coordinates. The error "bad screen distance 'pyimage1'" typically means Tk expected a numeric coordinate but received the internal image name because x, y were omitted or arguments were mis-ordered. Also keep a Python reference to the PhotoImage; otherwise it may be garbage-collected and not display.

import tkinter as tk

root = tk.Tk()
canvas = tk.Canvas(root, width=800, height=500)
canvas.pack()

bg = tk.PhotoImage(file="world.gif")
bg_id = canvas.create_image(0, 0, image=bg, anchor="nw")  # x, y first
canvas.image = bg  # keep a reference

# ensure background stays behind future drawings
canvas.tag_lower(bg_id)

root.mainloop()

Notes and tips:

  • Always pass x, y before keyword options to create_image. See the Tk canvas image docs: canvas create image.
  • Keep the PhotoImage alive (e.g., assign to self.bg in a class, or canvas.image as above). Reference: tkinter.PhotoImage.
  • If your image is not GIF/PNG or needs resizing, use Pillow: Image.open(...).resize(...); ImageTk.PhotoImage(...).
  • To fill the window, either size the canvas to the image dimensions or handle window <Configure> events to rescale/redraw the background.
  • If you prefer not to use Canvas, you can also place a Label with the image as background and lay other widgets on top using place or pack.

Recommended Answers

All 4 Replies

use instead of configure, and then place whatever objects on top of it.

canvas.create_image returns a "bad screen distance "pyimage1"" error. I have no idea what that means

self.canvas.create_image(image=myimage)

this returns a tuple index out of range. Any ideas?

A working example ...

''' tk_Canvas_BGImage1.py
use a Tkinter canvas for a background image
for result see http://prntscr.com/12p9ux
'''

try:
    # Python2
    import Tkinter as tk
except ImportError:
    # Python3
    import tkinter as tk

root = tk.Tk()
root.title('background image')

# pick a .gif image file you have in the working directory
fname = "Flowers.gif"
bg_image = tk.PhotoImage(file=fname)
# get the width and height of the image
w = bg_image.width()
h = bg_image.height()

# size the window so the image will fill it
root.geometry("%dx%d+50+30" % (w, h))

cv = tk.Canvas(width=w, height=h)
cv.pack(side='top', fill='both', expand='yes')

cv.create_image(0, 0, image=bg_image, anchor='nw')

# add canvas text at coordinates x=15, y=20
# anchor='nw' implies upper left corner coordinates
cv.create_text(15, 20, text="Python Greetings", fill="red", anchor='nw')

# now add some button widgets
btn1 = tk.Button(cv, text="Click")
btn1.pack(side='left', padx=10, pady=5, anchor='sw')

btn2 = tk.Button(cv, text="Quit", command=root.destroy)
btn2.pack(side='left', padx=10, pady=5, anchor='sw')

root.mainloop()
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.