2

I am using pygtk with glade to make a simple program which (by now) consists on:

GUI: A grid 4x4, each one showing an name. Under the grid goes a text Entry. The program runs in fullscreen.

BEHAVIOR: Each name will be "selectable" for 10secs in an infinite loop. "selectable" means that wherever the user makes a click the name which 10secs time is running is selected and put on the TextEntry.

I already made the GUI, but don't know how to handle it for detecting the click anywhere on the screen. Any help?

2
  • By using the "clicked" signal of the widget that is clicked ? (Note that some widgets have also a "selected" signal.) Commented Jan 20, 2012 at 18:48
  • In my case, it doesn't matter which widget is clicked, just that a click was made Commented Jan 20, 2012 at 19:03

1 Answer 1

2

To detect a click anywhere on the screen you can connect a callback for the button-press-event. Note that this is a gdk event and you must add this event to the mask with the add_events method.

The following small program should be useful:

import gtk

def callback(window, event):
    assert event.type == gtk.gdk.BUTTON_PRESS
    print 'Clicked at x={0}, y={0}'.format(event.x, event.y)

window = gtk.Window()
window.add_events(gtk.gdk.BUTTON_PRESS_MASK)
window.connect('button-press-event', callback)
window.connect('destroy', lambda w: gtk.main_quit())
window.show_all()

gtk.main()
Sign up to request clarification or add additional context in comments.

7 Comments

this is definitely usefull, thanks a lot. I am new with pygtk and glade. Where could I find more information about 'button-press-event', BUTTON_PRESS_MARK and related-stuff? I have a lot to learn. By example, I will also need to manage the double-click event
pyGTK is well documented. Take a look at developer.gnome.org/pygtk/stable and pygtk.org/pygtk2tutorial/index.html
@jeanc Please have a look at the links in may answer. With regard to double click, you will need a different gdk mask: gtk.gdk._2BUTTON_PRESS. The complete list of gdk event type constans is here.
@jcollado is there a trick to work with textviews? I have one in my window and when the click is in that area, it doesn't work :(
@jeanc If you use the same add_events and connect calls for the text views, your callback will be executed also when the mouse is clicked over them.
|

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.