So you are a Vb coder? If so, I would think that it wouldn’t be that much different coding in python than in Vb. There are some differences in python that would make coding like a Vb programmer difficult.
To my experience, Python’s object scope would be one hard point that I would have diffulty in. Usually, python’s program scope variables require a “global varname” when used within its functions. Python functions are also different. When defining functions in python, you don’t need something like a “end sub” or “end function” at the end of the function. Function declaration in Python goes like this…
“def functionName([varlist]):”
The body of the function follows the line after the declaration and must be tabbed correctly.To end the function, simply leave a blank line.
Here’s an example…
#This is a comment python alternative to '
#This function defines the txtName_Change event
def txtName_Change():
print "The new value for txtName is " + txtName.val
# ^^^ blank line ends the function.
About the gui… creating an event driven graphical user interface was easier than differentiating between Vb code and Python code. To achieve the structure neccesary for an event driven gui like Vb, you need the following…
-
Program init function - this is much like the form_load of vb6.
-
Program uninit function - this is much like the form_unload of vb6
-
Blender event handler - Has arguments (evt). Responds to user actions happening in gui.
-
keyboard and mouse event handler - Has argumants (evt,val). Responds to keyboard and mouse events.
-
gui function - used to render your guiUse your init function to start your program. Within the init function, use the Draw.Register(Gui,event,bevent) function. That function starts up your gui.
In your gui function, all gui elements such as text boxes and buttons are variables. define them as global first, then give them a value. Each gui variable would have an event number which would be passed to the blender event handler.
In your blender event handler, check to see which event happened and call your functions accordingly. Since python has no switch statement equivilent, use the if…elif…else statement.
As for the Keyboard and mouse event handler, the schematic is the same as the blender event handler.
Here’s the gui setup as a reference…
def bevent(evt):
if evt == 1:
txtObjName_Change()
elif evt==10:
cmdExit_Click()
elif evt==11:
chkEnabled_Click()
else:
return
def event(evt,val):
if evt == Draw.ESCKEY:
#end application
progUnInit()
def Gui():
#These variables belong to the GUI of the program.
global txtObjName
global cmdExit
global chkEnabled
txtObjName = Draw.String("Object Name:",1,10,29,258,17,strObjName,20)
cmdExit = Draw.PushButton("Exit",10,94,192,81,33)
chkEnabled = Draw.Toggle("Enabled",11,10,200,89,17,blnEnabled)
Draw.Redraw(1)
def progInit():
Draw.Register(Gui,event,bevent)
def progUnInit():
#terminate gui interface
Draw.Exit()
def txtObjName_Change():
global strObjName
strObjName = txtObjName.val
def cmdExit_Click():
progUnInit()
def chkEnabled_Click():
global blnEnabled
blnEnabled = chkEnabled.val
.....
.....
.....
#======#
#When all functions are defined, initialize the program...
#======#
progInit()
I hope this post wasn’t too long. I also hope this would make your Vb -> Python experience much more enjoyable. Good luck with your endevor.