Using a Toggle button...

Hello all. I’m been trying to figure out how to use Toggle buttons and I’m not having much success. Noobness.

I seem to remember an example script that had a few GUI elements in it. One of which was a Toggle button that when pressed said something like “The button is pressed.”, and when you depressed it, it said something like “The button is depressed.” That’s all I’m really trying to do just to see how they work.

In my ‘bevent’ function I have


elif evt == EVENT_TOG:
--print "something"

but it does it whenever I click it, I was trying to get it to display a message similar to that mentioned above. I can’t seem to tell the script how to tell the difference of it being pressed/depressed.

Any help would be appreciated. Much thanks.

I’m confused…

So I did this:


elif evt == EVENT_TOG:
--print T_tog

And it prints 0’s and 1’s when I press it/depress it.

But if I add this:


elif evt == EVENT_TOG:
--if T_tog == 0:
----print "this"
--elif T_tog == 1:
----print "that"

And it outputs nothing.

  elif (evt == 8):
    t_rgb.val = not t_gray.val
    Redraw()

this is from one of my scripts - it toggles the state of 2 toggle-buttons so that one of them is always down

t_rgb and t_gray are the two toggle button objects,
8 is the event number that’s raised when t_rgb is klicked
and redraw is called to update the gui

Ok…so apparently I had to have:


T_tog = Create(0)

And then in the ‘bevent’ function have:


if T_tog.val == 0...
if T_tog.val == 1...

Couldn’t figure out the point of the ‘Create’ statement. But with this it’s a little more apparent.

Create() creates a button object which you’ll need

in your gui_draw() you use the Toggle(…) command, this returns a button object which you’ll assign to the created button object T_tog

t_tog = Create(0)
...
(inside your draw)
  global t_tog
  t_tog = Toggle(..., t_tog.val, ...)
...
(inside your bevent)
  if evt==x:
    if t_tog.val==1:
      print "toggle down"

have a look at the blender python docs for a better example :smiley:

Have it all straightened out now. Thanks for the help. :stuck_out_tongue: