Having trouble switching button images w/togglebutton

More
01 Feb 2019 02:49 #125445 by pferrick
Hi all-

I'm attempting to learn how to toggle the image in a button on a GladeVCP panel. Given that I'm working my way through various python tutorials and have yet to start hitting the gtk ones, it's probably not surprising that I'h having some trouble getting it to work...!

Here's what I've done so far:

1. Put a HAL togglebutton named toggle (in a HAL state sensitive table) on my panel (mini_panel.glade).
2. Given this button custom content (a Gtkimage, machine_off.png)
2. Selected the on_toggle_toggled handler for its 'toggled' signal.
3. Used the following line in my ini:

GLADEVCP = -u pkf.py mini_panel.glade

4. Here's what pkf.py looks like:
import gtk
import os

class HandlerClass:
    def __init__(self, halcomp,builder,useropts):

        self.halcomp = halcomp
        self.builder = builder

        # a reference to the image path - adjust to your path
        self.IMAGES = os.path.expanduser('./IMAGES')
        print 'IMAGE PATH:', os.path.exists(self.IMAGES)

        # set up a reference to an image object for true state of toggle
        self.image_toggle_true = gtk.Image()
        self.image_toggle_true.set_from_file(os.path.join(self.IMAGES, 'machine_on.png'))

        # set up a reference to an image object for false state of toggle
        self.image_toggle_false = gtk.Image()
        self.image_toggle_false.set_from_file(os.path.join(self.IMAGES, 'machine_off.png'))

        # set default image for button - this image doesn't change
        self.builder.get_object('button_image').set_from_file(os.path.join(self.IMAGES, 'configure-2.png'))

        # set the default image to match the default state at start up
        self.builder.get_object('toggle').set_image(self.image_toggle_false)

    def on_button_pressed(self,widget,data=None):
        print widget, 'pressed'

    def on_toggle_toggled(self,widget,data=None):
        print widget, widget.get_active()
        if widget.get_active():
            widget.set_image(self.image_toggle_true)
        else:
            widget.set_image(self.image_toggle_false)

# boiler code
def get_handlers(halcomp,builder,useropts):
    return [HandlerClass(halcomp,builder,useropts)]

5. Made sure that images are all where they are supposed to be.

...all of which seems to me like it should switch back and forth between machine_off.png and machine_on.png when the button is toggled. [Using Halmeter I can see gladevcp.toggle changing from TRUE to FALSE, so OK there.] But the image stays at machine_off.png no matter what state the button is in. I've tried all sorts of permutations of GladeVCP tables, buttons, button settings (like Related Actions) with no luck. The good news, of course, is that I'm getting incrementally better at dealing with all these smaller-scale problems..there is light at the end of the tunnel.

I'm pretty sure that there's something amiss in the python code, but unfortunately I'm not quite to the point that I can figure out what it is! By the way, the code is courtesy cmorley in connection with Jerry's AXIS exploits, which I am shamelessly stealing following with great interest!

Thanks very much (again!)....

Patrick

Please Log in or Create an account to join the conversation.

More
01 Feb 2019 06:58 #125453 by cmorley
At a quick glace it all looks good.
I'd check that the GLADE file has the signal you think it does and is spelled right.
It's easy to edit a signal name and if ou don;t press return it reverts back (IIRC)

Also I would add the -d debugging switch to the gladevcp loading command.
If you are loading it as a panel in AXIS it probably looks similar to this (I added the -d switch):

GLADEVCP= -d -u hitcounter.py manual-example.ui

The -d tells gladevcp to send debugging info to the terminal (so you must run linuxcnc from the terminal)

Here is a typical output:
adding import dir .
module 'hitcounter' imported OK
module 'hitcounter' : 'get_handlers' function found
Registering handlers in module hitcounter object <hitcounter.HandlerClass instance at 0x7f0f66c16e60>
Register callback 'on_button_press' in <hitcounter.HandlerClass instance at 0x7f0f66c16e60>
/home/chris/emc-dev/bin/gladevcp:218: RuntimeWarning: missing handler 'on_togglebutton1_toggled'
  builder.connect_signals(handlers)
activating GTK bug workaround for gtkrc files

Note the 'Register callback' line. There will be one for every function that gladevcp finds in you handler file.

Also Note the 'RuntimeWarning: missing handler' line. This can tell you if you misspelled a signal or that you forgot to add it to the handler file. (because the signal and the function name must match)

If you can't find the problem doing that then please post the handler,glade and INI files.

Chris M

Please Log in or Create an account to join the conversation.

Time to create page: 0.116 seconds
Powered by Kunena Forum