Changing the caption of a checkbox.

Hi All,
I am trying to make a simple python GUI that has a checkbox. When the user clicks on the check box, it changes it’s caption from “on” to “off”.


chkDefault = Draw.Toggle("On", 200, 190, 350, 30, 20, chkDefault.val, "Click to check.")

For some reason, chkDefaul.val does not work. I also tried .state and that does not work.
In my event handler I have something like this:


 #Checkboxes start at 200.
 elif (evt == 200): 
  if (chkDefault == 1):
   chkDefault = 0
  else:
   chkDefault = 1
  Draw.Redraw(1)

What I also want i something like this: (VB pseudo code)


 #Checkboxes start at 200.
 elif (evt == 200): 
  if (chkDefault == 1):
   chkDefault = 0
   chkDefault.caption = "Off"
  else:
   chkDefault = 1
   chkDefaultCaption = "On"
  Draw.Redraw(1)

couldn’t you do something like:


#in your drawing function
captions = ["off", "on"] # list with the names

# ...

chkDefault = Draw.Toggle("On", 200, 190, 350, 30, 20, captions[chkDefault.val], "Click to check.")

Haven’t tested it, but it takes advantage of the int-values of the booleans. At least IIRC that’s how it works.

What you’d want to do is something like (mix of pseudo code and real python, sorry…)


#Untested Code!
chkCaption = "Off" #Default caption
chkDefault= Draw.Create(0)

def bevents(evt, val):
     ...
     If Toggle Event:
          if not chkDefault.val:
               chkCaption = "Off"
          else:
               chkCaption = "On"
          Draw.Redraw(1)
     ...

...later in your GUI code...
chkDefault = Draw.Toggle(chkCaption, ...your parameters for size/placement..., chkDefault.val, "Tool Tip")


I believe that chkCaption and chkDefault need to be global variables, as well, but its worth a try with them as normal ones as well.

Sorry, my suggestion above is incorrect. Here’s a tested version:


from Blender import Draw

chkDefault = Draw.Create(True) # put your default here

def draw():	 #your drawing function
	global chkDefault
	captions = ["off", "on"] # list of caption strings

	# ...

	chkDefault = Draw.Toggle( captions[chkDefault.val], 200, 190, 350, 30, 20, chkDefault.val, "Click to check." )
	
def event(evt,val):  # Define mouse and keyboard press events
	if evt == Draw.ESCKEY: # Example if esc key pressed
		Draw.Exit()		# then exit script
		return				 # return from the function

def button(evt):	 # Define what to do if a button is pressed
	#some code here, but event 200 not necessary to handle
	Draw.Redraw()

#
# 	Run!
#

Draw.Register(draw,event,button)

I’d add that you don’t need the code in your event handler - toggling on and off happens automatically. So leave out all your toggle boxes that you don’t want to directly trigger something else or behave different from default.

Thanks for the feedback.
Here is my “Empty Script” for starting out a project.


#!BPY
"""
Name: '(ap) Empty'
Blender: 244
Group: 'Wizards'
Tooltip: 'A simple empty script to get you going.'
"""
__author__ = ["Atomic Perception"]
__version__ = "0.0.2"
__name__ = "Empty"
############################################################################
# Import Stuff
############################################################################
import Blender
import math
from Blender.Draw import *
from Blender.BGL import *
from Blender import Mathutils
from Blender.Mathutils import *
from Blender import *
from math import *
import sys, traceback
############################################################################
# Globals     
############################################################################
name  = __name__
version = __version__
createdObjs = []
#When you add another variable to hold the default value from a gui element
#remember to add a global reference to the "gui_events"  and "gui" defs below.
numbSteps = Create(2) #Holds the default value for the slider.
chkDefault = Create(1) #Holds the default value for the checkbox.
############################################################################
# Console init greetings.
############################################################################
print "Initializing..."
print "" + name + " - version: " + version 
print ""
############################################################################
# GUI    
############################################################################
def gui():
 global chkDefault, numbSteps
 glClearColor(0.5,0.5,0.5, 0.0)
 glColor3f(0.0,0.0,0.0,)
 glClear(GL_COLOR_BUFFER_BIT)
 glRasterPos2i(10, 410)
 Draw.Text(name + " - V" + version, "large")
 numbSteps = Slider("Number: ", 100, 10, 260, 290, 20, numbSteps.val, 1, 100, 1, "Number of some kind of units.")
 glRasterPos2i(117, 355)
 Draw.Text("Checkbox:")
 captions = ["off", "on"]
 chkDefault = Draw.Toggle(captions[chkDefault.val], 200, 190, 350, 30, 20, chkDefault.val, "Click to check.")
 #glRasterPos2i(117, 355)
 #Draw.Text("Click To Generate:")
 Button("Generate", 1, 10, 10, 230, 40, "Execute code!")
 Button("Exit", 2, 250, 10, 50, 40, "Exit script")
############################################################################
# GUI Event Handlers  
############################################################################
def gui_events(eventIndex):
 global chkDefault, numbSteps
 #Buttons start at 1.
 if (eventIndex == 1):
  doCode()
 elif (eventIndex == 2):
  print "
End code...
"
  Exit()
 
 #Sliders start at 100.
 elif (eventIndex == 100):
  print "
numbSteps:",numbSteps
 
 #Checkboxes start at 200.
 elif (eventIndex == 200): 
  if (chkDefault == 1):
   print "
On
"
  else:
   print "
Off
"
 Draw.Redraw() 
############################################################################
# system Event Handlers
############################################################################
def sys_events(eventIndex, value):
 if (eventIndex == ESCKEY and not value):
  Exit()
 
############################################################################
# doCode() 
############################################################################
def doCode():
 try:
  print "
Executing code...
"
 except:
  print "
Error...
"
  traceback.print_exc(file=sys.stdout)
 
 Draw.Redraw(1)
 
############################################################################
# register handlers.
############################################################################
Register(gui, sys_events, gui_events)