Python Forum
Simple Button click on image file to create action?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Simple Button click on image file to create action?
#1
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()
Reply


Messages In This Thread
Simple Button click on image file to create action? - by jpezz - Mar-26-2019, 10:54 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  tkinter - touchscreen, push the button like click the mouse John64 5 829 Jan-06-2024, 03:45 PM
Last Post: deanhystad
  Figure Gets Larger Every time I click a show button joshuagreineder 2 1,326 Aug-11-2022, 06:25 AM
Last Post: chinky
  simple tkinter question function call not opening image gr3yali3n 5 3,424 Aug-02-2022, 09:13 PM
Last Post: woooee
  [Tkinter] Why does the image for the button not appear? finndude 4 2,086 Oct-21-2021, 06:41 PM
Last Post: deanhystad
  looking for scripts that do simple image display Skaperen 10 4,244 Sep-13-2021, 05:35 PM
Last Post: FullOfHelp
  [Tkinter] image inside button rwahdan 4 7,055 Jul-12-2021, 08:49 PM
Last Post: deanhystad
  tkinter showing image in button rwahdan 3 5,616 Jun-16-2021, 06:08 AM
Last Post: Yoriz
  [Tkinter] Modify Class on Button Click KDog 4 3,949 May-11-2021, 08:43 PM
Last Post: KDog
  How to create a simple GUI GRS26 7 3,628 Mar-27-2021, 02:38 PM
Last Post: FernandoCabral
  tkinter button image Nick_tkinter 4 4,029 Mar-04-2021, 11:33 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020