By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,790 Members | 1,426 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,790 IT Pros & Developers. It's quick & easy.

Python Tkinter Background image

P: 1
below is my code of a simple program where you can control a circle with buttons WASD. my question is, how do I add a background image? Please help me. :)


Expand|Select|Wrap|Line Numbers
  1. from Tkinter import *
  2. import time, math, random
  3.  
  4. def timerFired(previousTime):
  5.     currentTime = time.time()
  6.     dt = currentTime - previousTime
  7.     canvasWidth = int(canvas.cget("width"))
  8.     canvas.after(40, timerFired, currentTime)
  9.     redrawAll()
  10. def key(event):
  11.     if event.char == "a":
  12.         for i in range(len(canvas.data.x)):
  13.             canvas.data.x[i] = canvas.data.x[i] - 5
  14.     elif event.char == "d":
  15.         for i in range(len(canvas.data.x)):
  16.             canvas.data.x[i] = canvas.data.x[i] + 5
  17.     elif event.char == "w":
  18.         for i in range(len(canvas.data.x)):
  19.             canvas.data.y[i] = canvas.data.y[i] - 5
  20.     elif event.char == "s":
  21.         for i in range(len(canvas.data.x)):
  22.             canvas.data.y[i] = canvas.data.y[i] + 5
  23.     redrawAll()
  24.  
  25. def drawBackground():
  26.     pass
  27.  
  28. def redrawAll():
  29.     canvas.delete(ALL)
  30.     drawBackground()
  31.     canvas.create_text(int(canvas.cget("width")) - 10,
  32.                        int(canvas.cget("height")) - 10,
  33.                        anchor=SE,
  34.                        text = "X = " + str(canvas.data.x) + " Y = " + str(canvas.data.y))
  35.     for i in range(len(canvas.data.x)):
  36.         canvas.create_oval(canvas.data.x[i] - canvas.data.r[i],
  37.                            canvas.data.y[i] + canvas.data.r[i],
  38.                            canvas.data.x[i] + canvas.data.r[i],
  39.                            canvas.data.y[i] - canvas.data.r[i],
  40.                            fill=canvas.data.color[i])
  41.  
  42. def init():
  43.     canvas.data.groundHeight = 600
  44.     canvas.data.x = [400]
  45.     canvas.data.y = [300]
  46.     canvas.data.r = [100]
  47.     canvas.data.color = [""]
  48.     canvas.data.backupX = []
  49.     canvas.data.backupY = []
  50.     canvas.data.backupR = []
  51.     canvas.data.backupColor = []
  52.  
  53. def run():
  54.     global canvas
  55.     root = Tk()
  56.     canvas = Canvas(root, width=800, height=600, bg="yellow")
  57.     canvas.pack()
  58.     root.canvas = canvas.canvas = canvas
  59.     class MyModel: pass
  60.     canvas.data = MyModel()
  61.     init()
  62.     root.bind("<Key>", key)
  63.     timerFired(time.time())
  64.     root.mainloop()
  65.  
  66. run()
Mar 17 '13 #1
Share this Question
Share on Google+
1 Reply

Expert 100+
P: 626
You use canvas.create_image(x, y, image=name, + other options) see here for the create_image options. The "image=" must be a BitmapImage or PhotoImage instance.
Mar 17 '13 #2

Post your reply

Sign in to post your reply or Sign up for a free account.