Mar-26-2019, 10:54 PM
I am looking for a simple example of a python program that would display an image file and then be able to tell if the user clicked on the image with the left mouse button so that an action could be performed. I found one that seems pretty simple but it needs the "clutter" module which I can't find. Running python 2.7. Now I'm not a python programmer. I'm an old unix guy who is a volunteer at a museum writing an application on a Raspberry Pi. The main program I found that I hacked to do what I wanted works great. Now I need some more user interface on a touchscreen so all the user has is the equivalent of a left mouse button to make selections for the program. Here is the example I found of a graphical display with the ability to return info. I believe I can simplify this to just detect a button click then use Popen to call a program but if someone has some code ... or even knows where I can find a compatible clutter, I can probably plod my way through. I've tried to use graphical programs like feh but they do not do what I need.
import clutter #create a clutter stage and set the display size stage = clutter.Stage() stage.set_size(400, 400) #load the image for the buttons img=clutter.cogl.texture_new_from_file('button.png',clutter.cogl.TEXTURE_NO_SLICING, clutter.cogl.PIXEL_FORMAT_ANY) #example create button from class start class button(clutter.Texture): def __init__(self,id,row=1,padding=10): clutter.Texture.__init__(self) self.row=row self.id=id self.set_size (100,100) self.set_position (id*100,self.row*100) self.set_cogl_texture(img) self.set_reactive(True) #call click method on button clicked self.connect("button-press-event",self.clicked) def clicked(self,stage, event): print "class click="+str(self.id)+" row "+str(self.row) #example class with collection of button class class buttons: def __init__(self): self.buttonlist=[] self.count=0 for i in range(0,10): self.buttonlist.append(button(self.count,row=2)) #stage.add(self.buttonlist[self.count]) self.count+=1 #iter method so we can step through all buttons in a for loop def __iter__(self): for i in self.buttonlist: yield i #append a new button def append(self,btton): self.buttonlist.append(self.count) self.count+=1 #crate instance of buttons class #append buttons class to stage buttonlist2=buttons() for b in buttonlist2: stage.add(b) #example of creating button with function calls end #show everything in the stage stage.show_all() stage.connect("destroy",clutter.main_quit) #main clutter loop clutter.main()