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: - 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()
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: -
# 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()
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: - 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 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
6 3462
To fix line 12, use raw strings for Windows path names (because the "\" has a special purpose in python strings) and use the "\" in literal path names: - #
-
# Use raw strings #
-
im = Image.open(r"C:\Documents and Settings\John Perry\Desktop\GradSchool\Summer 07 python goshen\cd with CT's\patient10\se002.jpg")
or - #
-
# escape the backslash #
-
im = Image.open("C:\\Documents and Settings\\John Perry\\Desktop\\GradSchool\\Summer 07 python goshen\\cd with CT's\\patient10\\se002.jpg")
Hey all,
I implemented the raw string, however, the image will still not display on the canvas at all. It still remains as a white canvas bg with the mouse event functions. Any ideas, or can you try and get it to display an actual jpg, gif, etc image in the canvas to draw shapes over?
Thanks,
JP
Hey all,
I implemented the raw string, however, the image will still not display on the canvas at all. It still remains as a white canvas bg with the mouse event functions. Any ideas, or can you try and get it to display an actual jpg, gif, etc image in the canvas to draw shapes over?
Thanks,
JP
I'll try to get some time to work on this for you. I'm swamped at the moment.
I'll try to get some time to work on this for you. I'm swamped at the moment.
In the mean time, have you seen
An Introduction to Tkinter?
I solved the issue. I had to reference my class correctly.
here is the fix: - from Tkinter import *
-
import Tkinter
-
import Image #PIL
-
import ImageTk #PIL
-
import sys
-
import getopt
-
trace = 0
-
-
class CanvasEventsDemo:
-
def __init__(self, parent=NONE):
-
canvas = Canvas(win)
-
canvas.pack(fill=BOTH)
-
self.img = PhotoImage(file="C:/Documents and Settings/John Perry/Desktop/GradSchool/Summer 07 python goshen/cd with CT's/patient10/se002.gif")
-
canvas.config(width=self.img.width(), height=self.img.height())
-
canvas.create_image(0,0, image=self.img, anchor=NW)
-
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_rectangle]
-
def onStart(self, event):
-
event.widget.delete('all')
-
self.canvas.create_image(0,0, image=self.img, anchor=NW)
-
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
-
self.xorig=self.start.x
-
self.yorig=self.start.y
-
self.xfinal=event.x
-
self.yfinal=event.y
-
def onClear(self, event):
-
event.widget.delete('all')
-
self.canvas.create_image(0,0, image=self.img, anchor=NW)
-
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__':
-
win=Tk()
-
jop=CanvasEventsDemo()
-
mainloop()
JP
Awesome! Thanks for keeping us up-to-date with you progress.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Tim Flynn |
last post by:
Hi,
I'm trying to write a simple image viewer using Tkinter and ImageTk from
the PIL toolkit. The images are stored in memory and I don't want to
create temporary files for them.
My code...
|
by: Aguilar, James |
last post by:
My previous example used the concept of a Shape class heirarchy, so I will
continue with that.
Suppose I have something like fifty different shapes, and I am trying to
instantiate one of them. ...
|
by: Kamran |
last post by:
Hi
I have very little experience of C++, nevertheless I have
been asked to write a gui using QT/QWT.
I know that I should direct the question to the relevant
mailing list and I have done that but...
|
by: namratapatil |
last post by:
Hi...i am trying to load an image that has been saved at the following path: C:\WTK22\apps\MyProject\res After exectuing the program, the NullPointerException is thrown...Kindly let me know how to...
|
by: Eric_Dexter |
last post by:
I know this might be the wrong place to ask but I recently modified
the ogl example from wxpython and it works fine in spe but when I call
it from other programs it wierds out because of the run.py...
|
by: Nebulism |
last post by:
Hi everyone!
Earlier I asked a question about mouse interaction with a GUI. I have found a pretty comprehensive script that is supposed to work <mod edit: link removed, source inserted>:# This...
|
by: Nebulism |
last post by:
Hi everyone,
I am working on a module for my GUI that shows one image with an index value below and would use a scrollbar to control which of the images are displayed. The images are stored in a...
|
by: blaine |
last post by:
Hey everyone!
I'm not very good with Tk, and I am using a very simple canvas to
draw some pictures (this relates to that nokia screen emulator I had a
post about a few days ago).
Anyway, all is...
|
by: joshdw4 |
last post by:
I hate to do this, but I've thoroughly exhausted google search. Yes,
it's that pesky root window and I have tried withdraw to no avail. I'm
assuming this is because of the methods I'm using. I...
|
by: erikbower65 |
last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps:
1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal.
2. Connect to...
|
by: erikbower65 |
last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA:
1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
|
by: kcodez |
last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
|
by: DJRhino1175 |
last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this -
If...
|
by: DJRhino |
last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer)
If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _
310030356 Or 310030359 Or 310030362 Or...
|
by: lllomh |
last post by:
Define the method first
this.state = {
buttonBackgroundColor: 'green',
isBlinking: false, // A new status is added to identify whether the button is blinking or not
}
autoStart=()=>{
|
by: lllomh |
last post by:
How does React native implement an English player?
|
by: Mushico |
last post by:
How to calculate date of retirement from date of birth
|
by: DJRhino |
last post by:
Was curious if anyone else was having this same issue or not....
I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
| |