How to display picture and get mouse click coordinate on it?

Yes it is possible and pretty easy once you understand tkinter, here's a quick script.

Yes it is possible and pretty easy once you understand tkinter, here's a quick script: from Tkinter import * from tkFileDialog import askopenfilename import Image, ImageTk if __name__ == "__main__": root = Tk() #setting up a tkinter canvas with scrollbars frame = Frame(root, bd=2, relief=SUNKEN) frame. Grid_rowconfigure(0, weight=1) frame. Grid_columnconfigure(0, weight=1) xscroll = Scrollbar(frame, orient=HORIZONTAL) xscroll.

Grid(row=1, column=0, sticky=E+W) yscroll = Scrollbar(frame) yscroll. Grid(row=0, column=1, sticky=N+S) canvas = Canvas(frame, bd=0, xscrollcommand=xscroll. Set, yscrollcommand=yscroll.

Set) canvas. Grid(row=0, column=0, sticky=N+S+E+W) xscroll. Config(command=canvas.

Xview) yscroll. Config(command=canvas. Yview) frame.

Pack(fill=BOTH,expand=1) #adding the image File = askopenfilename(parent=root, initialdir="C:/",title='Choose an image. ') img = ImageTk. PhotoImage(Image.

Open(File)) canvas. Create_image(0,0,image=img,anchor="nw") canvas. Config(scrollregion=canvas.

Bbox(ALL)) #function to be called when mouse is clicked def printcoords(event): #outputting x and y coords to console print (event. X,event. Y) #mouseclick event canvas.

Bind("",printcoords) root.mainloop() Unedited it will print using the default window coordinate system to the console. The canvas widget makes the top left corner the 0,0 point so you may have to mess around with the printcoords function. To get the loaded picture dimension you would use canvas.

Bbox(ALL), and you might want to switch to using canvasx and canvasy coords instead of how it is. If you're new to tkinter; google should be able to help you finish it from here :).

You can use Tkinter to do this and its built in to python already. pythonware.com/library/tkinter/introduction.

Here's a version I had cobbled together a while ago using wxPython and various wxPython tutorials. This prints the mouse click coordinates to a separate output window. (Uses Python 2.6.2, wxPython 2.8.10.1) Enter the path to your image in the filepath variable at the bottom.

Import wx class MyCanvas(wx. ScrolledWindow): def __init__(self, parent, id = -1, size = wx. DefaultSize, filepath = None): wx.ScrolledWindow.

__init__(self, parent, id, (0, 0), size=size, style=wx. SUNKEN_BORDER) self. Image = wx.

Image(filepath) self. W = self.image.GetWidth() self. H = self.image.GetHeight() self.

Bmp = wx. BitmapFromImage(self. Image) self.

SetVirtualSize((self. W, self. H)) self.

SetScrollRate(20,20) self. SetBackgroundColour(wx. Colour(0,0,0)) self.

Buffer = wx. EmptyBitmap(self. W, self.

H) dc = wx. BufferedDC(None, self. Buffer) dc.

SetBackground(wx. Brush(self. GetBackgroundColour())) dc.Clear() self.

DoDrawing(dc) self. Bind(wx. EVT_PAINT, self.

OnPaint) self. Bind(wx. EVT_LEFT_UP, self.

OnClick) def OnClick(self, event): pos = self. CalcUnscrolledPosition(event.GetPosition()) print '%d, %d' %(pos. X, pos.

Y) def OnPaint(self, event): dc = wx. BufferedPaintDC(self, self. Buffer, wx.

BUFFER_VIRTUAL_AREA) def DoDrawing(self, dc): dc. DrawBitmap(self. Bmp, 0, 0) class MyFrame(wx.

Frame): def __init__(self, parent=None, id=-1, filepath = None): wx.Frame. __init__(self, parent, id, title=filepath) self. Canvas = MyCanvas(self, -1, filepath = filepath) self.canvas.

SetMinSize((self.canvas. W, self.canvas. H)) self.canvas.

SetMaxSize((self.canvas. W, self.canvas. H)) self.canvas.

SetBackgroundColour(wx. Colour(0, 0, 0)) vert = wx. BoxSizer(wx.

VERTICAL) horz = wx. BoxSizer(wx. HORIZONTAL) vert.

Add(horz,0, wx. EXPAND,0) vert. Add(self.

Canvas,1,wx. EXPAND,0) self. SetSizer(vert) vert.

Fit(self) self.Layout() if __name__ == '__main__': app = wx.App() app. SetOutputWindowAttributes(title='stdout') wx. InitAllImageHandlers() filepath = 'ENTER FILEPATH HERE' if filepath: print filepath myframe = MyFrame(filepath=filepath) myframe.Center() myframe.Show() app.MainLoop().

I don't think he is talking about javascript and DOM manipulation. – Alberteddu Mar 31 '11 at 14:29.

A good alternative to Tkinter is using QT in Python. You can achieve that with PyQT, or PySide.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions