I have been attempting to utilize a draw command script that loads a canvas, and through certain mouse events, draws rectangles. The original code is from http://www.java2s.com/Code/Python/Ev...peoncanvas.htm .
The code itself is:
Essentially I am looking at setting the background image as a photo using the create_image function. I have gotten it to work in a structure that is not utilizing classes such as this example code:
I've gotten it to work with a different file of course, however, when I try to implement my image background in the first code all I get is a grey canvas. So without further ado here is my code:
Thanks for any help on getting this little issue fixed! Note that the section I'm referring to is around line 12. I just need my jpg or gif loaded as the background.
Thanks,
JP
The code itself is:
Code:
from Tkinter import *
trace = 0
class CanvasEventsDemo:
def __init__(self, parent=None):
canvas = Canvas(width=300, height=300, bg='beige')
canvas.pack()
canvas.bind('<ButtonPress-1>', self.onStart)
canvas.bind('<B1-Motion>', self.onGrow)
canvas.bind('<Double-1>', self.onClear)
canvas.bind('<ButtonPress-3>', self.onMove)
self.canvas = canvas
self.drawn = None
self.kinds = [canvas.create_oval, canvas.create_rectangle]
def onStart(self, event):
self.shape = self.kinds[0]
self.kinds = self.kinds[1:] + self.kinds[:1]
self.start = event
self.drawn = None
def onGrow(self, event):
canvas = event.widget
if self.drawn: canvas.delete(self.drawn)
objectId = self.shape(self.start.x, self.start.y, event.x, event.y)
if trace: print objectId
self.drawn = objectId
def onClear(self, event):
event.widget.delete('all')
def onMove(self, event):
if self.drawn:
if trace: print self.drawn
canvas = event.widget
diffX, diffY = (event.x - self.start.x), (event.y - self.start.y)
canvas.move(self.drawn, diffX, diffY)
self.start = event
if __name__ == '__main__':
CanvasEventsDemo()
mainloop()
Code:
# Putting a gif image on a canvas with Tkinter # tested with Python24 by vegaseat 25jun2005 from Tkinter import * # create the canvas, size in pixels canvas = Canvas(width = 300, height = 200, bg = 'yellow') # pack the canvas into a frame/form canvas.pack(expand = YES, fill = BOTH) # load the .gif image file # put in your own gif file here, may need to add full path # like 'C:/WINDOWS/Help/Tours/WindowsMediaPlayer/Img/mplogo.gif' gif1 = PhotoImage(file = 'DrBunsen.gif') # put gif image on canvas # pic's upper left corner (NW) on the canvas is at x=50 y=10 canvas.create_image(50, 10, image = gif1, anchor = NW) # run it ... mainloop()
Code:
from Tkinter import *
import Image #PIL
import ImageTk #PIL
import sys
import getopt
trace = 0
class CanvasEventsDemo:
def __init__(self, parent=NONE):
canvas = Canvas(width=400,height=400)
canvas.pack()
im = Image.open("C:/Documents and Settings/John Perry/Desktop/GradSchool/Summer 07 python goshen/cd with CT's/patient10/se002.jpg")
photo = ImageTk.PhotoImage(im)
item=canvas.create_image(10, 10, image = photo)
canvas.bind('<ButtonPress-1>', self.onStart)
canvas.bind('<B1-Motion>', self.onGrow)
canvas.bind('<Double-1>', self.onClear)
canvas.bind('<ButtonPress-3>', self.onMove)
self.canvas = canvas
self.drawn = None
self.kinds = [canvas.create_oval, canvas.create_rectangle]
def onStart(self, event):
self.shape = self.kinds[0]
self.kinds = self.kinds[1:] + self.kinds[:1]
self.start = event
self.drawn = None
def onGrow(self, event):
canvas = event.widget
if self.drawn: canvas.delete(self.drawn)
objectId = self.shape(self.start.x, self.start.y, event.x, event.y)
if trace: print objectId
self.drawn = objectId
def onClear(self, event):
event.widget.delete('all')
def onMove(self, event):
if self.drawn:
if trace: print self.drawn
canvas = event.widget
diffX, diffY = (event.x - self.start.x), (event.y - self.start.y)
canvas.move(self.drawn, diffX, diffY)
self.start = event
if __name__ == '__main__':
CanvasEventsDemo()
mainloop()
Thanks,
JP
Comment