1

I have a set of images available. If I click on one of those images is there a way to determine which of the images has been clicked on in wxPython?

3
  • yes .... what have you tried ... because basically it works just like you would expect it to... Apr 4, 2013 at 17:34
  • I have tried googling but haven't found anything relevant; how do I bind the click on that image to my function which decides the response. If I put it sequentially wouldn't they be called for each image?
    – zer0nes
    Apr 4, 2013 at 17:41
  • you dont tell us anything about how you are displaying your images? are you blitting them right on the dc? are you creating panels for them? etc... properly setting up your project is important Apr 4, 2013 at 19:22

2 Answers 2

3

You will almost certainly have to calculate it for yourself. The most straight-forward method would be to use a mouse event like wx.EVT_LEFT_DOWN and grab the mouse's coordinates in the event handler. Then use that information to tell you where on your wxPython window you clicked. Each of your image widgets or DCs or whatever you're using can report it's size and position, so if the mouse coordinates are in X image's boundaries, you know it's been clicked on. You might also be able to use the HitTest() method, depending on what you're using to show the images.

EDIT: Here is how you would do it if you were using a wx.StaticBitmap, which actually lets you attach an wx.EVT_LEFT_DOWN to it:

import wx

class PhotoCtrl(wx.Frame):
    def __init__(self):
        size = (400,800)
        wx.Frame.__init__(self, None, title='Photo Control', size=size)

        self.panel = wx.Panel(self)


        img = wx.EmptyImage(240,240)
        self.imageCtrl = wx.StaticBitmap(self.panel, wx.ID_ANY, 
                                         wx.BitmapFromImage(img),
                                         name="emptyImage")
        imageCtrl2 = wx.StaticBitmap(self.panel, wx.ID_ANY, 
                                     wx.BitmapFromImage(img),
                                     name="anotherEmptyImage")

        self.imageCtrl.Bind(wx.EVT_LEFT_DOWN, self.onClick)
        imageCtrl2.Bind(wx.EVT_LEFT_DOWN, self.onClick)

        mainSizer = wx.BoxSizer(wx.VERTICAL)
        mainSizer.Add(self.imageCtrl, 0, wx.ALL, 5)
        mainSizer.Add(imageCtrl2, 0, wx.ALL, 5)

        self.panel.SetSizer(mainSizer)
        self.Show()

    #----------------------------------------------------------------------
    def onClick(self, event):
        """"""
        print event.GetPosition()
        imgCtrl = event.GetEventObject()
        print imgCtrl.GetName()


if __name__ == '__main__':
    app = wx.App(False)
    frame = PhotoCtrl()
    app.MainLoop()
3
  • I found it for Tkinter in this link but couldn't find a sample code for wxpython
    – zer0nes
    Apr 4, 2013 at 18:20
  • +1 better answer than mine ... I was just annoyed at how little info OP gave Apr 4, 2013 at 19:34
  • I agree which is why I write an example at first. This probably won't work for DCs Apr 4, 2013 at 20:03
0

you dont tell us anything about how you are displaying your images? are you blitting them right on the dc? are you creating panels for them? etc... properly setting up your project is important. basically you give us zero information to help you with.

Keeping all that in mind, something like this would work fine (this is called a self contained code example, you should always provide one with your questions, to make it easier for people to help you)

import wx
a = wx.App(redirect=False)
f= wx.Frame(None,-1,"Some Frame",size = (200,200))
sz = wx.BoxSizer(wx.HORIZONTAL)
def OnClick(evt):
    print "Clicked:",evt.GetId()-10023
for i,img in enumerate(["img1","img2","img3"]):
    id = 10023+i
    p = wx.Panel(f,-1)
    sz.Add(p)
    sz1 = wx.BoxSizer()
    p.Bind(wx.EVT_LEFT_UP,OnClick)
    bmp = wx.Image(img).ConvertToBitmap()
    b = wx.StaticBitmap(p,-1,bmp)
    sz1.Add(b)
    p.SetSizer(sz1)

f.SetSizer(sz)
f.Layout()
f.Fit()
f.Show()
a.MainLoop()

Keep in mind I didnt test it... but theoretically it should work...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.