Lars Andersson
Lars Andersson

Reputation: 11

Python Tkinter background

canvas = tk.Canvas(window, width=800, height=600, bg="black") # PEP8: space after comma
canvas.pack()

Do anyone know how I could import a image from google to set as a background in canvas?

Upvotes: 1

Views: 185

Answers (1)

10 Rep
10 Rep

Reputation: 2270

First, download the image as a .png. If you have the latest version of tkinter, then the following will work.

Write this code:

canvas = tk.Canvas(window, width=800, height=600, bg="black") # PEP8: space after 
canvas.pack()
p = tk.PhotoImage(file = "putyourimagenamehere.png")
canvas.create_image(100, 100, image = p)

For the above code to work, you need to put the image in the same directory as the .py file.

Hope this helps!

Upvotes: 1

Related Questions