Change cursor on a single widget

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Change cursor on a single widget

Timo List
I seem to be unable to change the cursor on one widget only, not an entire window. How is this done? Below is a testscript which I thought was the right way.

Run normal to use PyGI, run with -p argument for PyGTK. Both act the same.

# Start of code
def run_pygi():
    from gi.repository import Gtk
    from gi.repository import Gdk

    def disable_cb(widget):
        widget.set_sensitive(False)
        cursor = Gdk.Cursor(Gdk.CursorType.WATCH)
        widget.get_window().set_cursor(cursor)

    win = Gtk.Window()
    win.resize(400, 400)
    win.set_title("PyGI example")
    win.connect('delete-event', Gtk.main_quit)

    b1 = Gtk.Button('Disable me')
    b1.connect('clicked', disable_cb)
    b2 = Gtk.Button('I do nothing')
    box = Gtk.VBox()
    box.pack_start(b1, False, False, 0)
    box.pack_start(b2, False, False, 0)
    win.add(box)
    win.show_all()

    Gtk.main()

def run_pygtk():
    import gtk

    def disable_cb(widget):
        widget.set_sensitive(False)
        cursor = gtk.gdk.Cursor(gtk.gdk.WATCH)
        widget.window.set_cursor(cursor)

    win = gtk.Window()
    win.resize(400, 400)
    win.set_title("PyGTK example")
    win.connect('delete-event', gtk.main_quit)

    b1 = gtk.Button('Disable me')
    b1.connect('clicked', disable_cb)
    b2 = gtk.Button('I do nothing')
    box = gtk.VBox()
    box.pack_start(b1, False, False, 0)
    box.pack_start(b2, False, False, 0)
    win.add(box)
    win.show_all()

    gtk.main()

import sys
if "-p" in sys.argv:
    run_pygtk()
else:
    run_pygi()

_______________________________________________
pygtk mailing list   [hidden email]
http://www.daa.com.au/mailman/listinfo/pygtk
Read the PyGTK FAQ: http://faq.pygtk.org/
Reply | Threaded
Open this post in threaded view
|

Re: Change cursor on a single widget

Neil Muller-10
On 11 March 2013 00:37, Timo <[hidden email]> wrote:
> I seem to be unable to change the cursor on one widget only, not an entire
> window. How is this done? Below is a testscript which I thought was the
> right way.
>
> Run normal to use PyGI, run with -p argument for PyGTK. Both act the same.

> def run_pygtk():
>     import gtk
>
>     def disable_cb(widget):
>         widget.set_sensitive(False)
>         cursor = gtk.gdk.Cursor(gtk.gdk.WATCH)
>         widget.window.set_cursor(cursor)

This works for widgets which have their own gtk.gdkWindows. It's
mentioned in the gtk docs that this isn't always the case, and several
widgets will return their parent's gtk.gdk.Window. You can check for
this using the get_has_window method.

One approach is to stuff the buttons inside an EventBox, since that
will definitely provide it's own gtk.gdk.Window  - something along the
lines of:

def run_pygtk():
    import gtk

    def disable_cb(widget, eventbox):
        widget.set_sensitive(False)
        cursor = gtk.gdk.Cursor(gtk.gdk.WATCH)
        eventbox.window.set_cursor(cursor)

    win = gtk.Window()
    win.resize(400, 400)
    win.set_title("PyGTK example")
    win.connect('delete-event', gtk.main_quit)

    eventbox = gtk.EventBox()

    b1 = gtk.Button('Disable me')
    b1.connect('clicked', disable_cb, eventbox)
    eventbox.add(b1)
    b2 = gtk.Button('I do nothing')
    box = gtk.VBox()
    box.pack_start(eventbox, False, False, 0)
    box.pack_start(b2, False, False, 0)
    win.add(box)
    win.show_all()

--
Neil Muller
[hidden email]

I've got a gmail account. Why haven't I become cool?
_______________________________________________
pygtk mailing list   [hidden email]
http://www.daa.com.au/mailman/listinfo/pygtk
Read the PyGTK FAQ: http://faq.pygtk.org/
Reply | Threaded
Open this post in threaded view
|

Re: Change cursor on a single widget

Timo List
Op 11-03-13 15:49, Neil Muller schreef:

> On 11 March 2013 00:37, Timo <[hidden email]> wrote:
>> I seem to be unable to change the cursor on one widget only, not an entire
>> window. How is this done? Below is a testscript which I thought was the
>> right way.
>>
>> Run normal to use PyGI, run with -p argument for PyGTK. Both act the same.
>> def run_pygtk():
>>      import gtk
>>
>>      def disable_cb(widget):
>>          widget.set_sensitive(False)
>>          cursor = gtk.gdk.Cursor(gtk.gdk.WATCH)
>>          widget.window.set_cursor(cursor)
> This works for widgets which have their own gtk.gdkWindows. It's
> mentioned in the gtk docs that this isn't always the case, and several
> widgets will return their parent's gtk.gdk.Window. You can check for
> this using the get_has_window method.
Thanks for that. I did read that part, but foolishly assumed it were
just those few mentioned widgets which don't have a GdkWindow. In my
real life application, I have stuff packed into a frame=>alignment=>box
and neither of those have a GdkWindow.

The get_has_window method comes in handy!

Cheers,
Timo

>
> One approach is to stuff the buttons inside an EventBox, since that
> will definitely provide it's own gtk.gdk.Window  - something along the
> lines of:
>
> def run_pygtk():
>      import gtk
>
>      def disable_cb(widget, eventbox):
>          widget.set_sensitive(False)
>          cursor = gtk.gdk.Cursor(gtk.gdk.WATCH)
>          eventbox.window.set_cursor(cursor)
>
>      win = gtk.Window()
>      win.resize(400, 400)
>      win.set_title("PyGTK example")
>      win.connect('delete-event', gtk.main_quit)
>
>      eventbox = gtk.EventBox()
>
>      b1 = gtk.Button('Disable me')
>      b1.connect('clicked', disable_cb, eventbox)
>      eventbox.add(b1)
>      b2 = gtk.Button('I do nothing')
>      box = gtk.VBox()
>      box.pack_start(eventbox, False, False, 0)
>      box.pack_start(b2, False, False, 0)
>      win.add(box)
>      win.show_all()
>

_______________________________________________
pygtk mailing list   [hidden email]
http://www.daa.com.au/mailman/listinfo/pygtk
Read the PyGTK FAQ: http://faq.pygtk.org/