PDA

View Full Version : Some help with a GUI Calc done in Python


IconicDrei
11-16-2012, 09:48 PM
Hi Guys,

I'm currently in the process of scripting a BMI Calculator in Python and Maya for a uni project. I have decided to create a GUI for it and am now at the stage where I am trying to search for code that will help me apply two user inputs (height & weight), apply a calculation and print a result.

http://andreishah.co.uk/Host/bmi_screen.jpg

Oddly enough, I've scripted this to work fine without a GUI, I have code like this:

height = float(raw_input("Message Appears Here"
weight = float(raw_input("Message Appears Here")

if bmi > (number) and bmi < (number):
print "(Message Here")

and so on..

This just doesn't work with my new script that I've written, the GUI code seems to be responsible for this I think, (in my very non-python experienced mind).

If you have any tips, hints, help, whatever, I'd be greatly appreciative. Rather annoyingly I know it's not the most complicated piece of code, but one that's taking me ages to figure out. Just a point in the right direction would be good.

Thanks :)

NaughtyNathan
11-17-2012, 02:22 PM
you've not posted any GUI code so we can't tell you what you may have done wrong, or need to add, so I'll go over the entire process. There is more than one way to approach this, but I'm going to give you the "easy" way (this is usually the way I would do it :) )

Basically you name your input controls so that you can later query them. Then you write a function which queries them, does the maths (or other operation(s)) and prints a result. You then "attach" this function to your GUI button.

def calculateBMI(*args):
height = mc.floatFieldGrp('bmiHeightField', q=True, v=True) # get the height value from our field
weight = mc.floatFieldGrp('bmiWeightField', q=True, v=True) # get the weight value from our field

bmi = height[0]*weight[0] # or whatever the actual BMI equation is?

print 'the BMI is: %0.2f \n' % bmi,

def bmiUI():
win = 'myBMICalcWin'
if mc.window(win, exists=True):
mc.deleteUI(win)
mc.window(win)
mc.columnLayout(adj=1)
mc.floatFieldGrp('bmiHeightField', label='Height')
mc.floatFieldGrp('bmiWeightField', label='Weight')
mc.button('Calculate BMI', c=calculateBMI)
mc.showWindow()

bmiUI()

obviously this is very basic, but the theory is still valid for your purposes. You could also add a third (named) float field to display the resulting BMI rather than printing it (just change the calc function to edit the third float field value with the result).

zoharl
11-19-2012, 01:17 AM
Scripting a BMI calculator won't help the situation. You need to watch your diet and exercise ;)

MrDream
11-20-2012, 07:34 PM
Hi ,
I am on the same stream, trying to sort out some problem on my BMI camculator
This is my script :

import maya.cmds as cmds

def calculateBMI(*args):
Height = cmds.floatField ("Float_1", q = True, v = True)
Weight = cmds.floatField ("Float_2", q = True, v = True)


BMI = Weight[0] / Height[0]*Height[0]

print "The BMI is: %0.2f \n" % BMI,
def UI():

#check to see if our window exixst
if cmds.window("exampleUI", exists = True):
cmds.deleteUI("exampleUI")

#Create out Window
cmds.window("exampleUI", w = 400, h = 200, mnb = False, mxb = False, sizeable = False, title = "BMI Calculator")

#Create a main layaout
mainLayout = cmds.columnLayout (w = 400, h = 200)
#Create row Layout
rowLayout = cmds.rowColumnLayout (numberOfColumns = 2, columnWidth = [(1, 190), (2, 190)], columnOffset = [(1, "both", 5),(2, "both", 5)])
cmds.separator (h = 10, style = "none")
cmds.separator (h = 10, style = "none")
Text_1 = cmds.text ("Welcome to the BMI Calcultation.", align = ("left"))
Text_2 = cmds.text ("")
cmds.separator (h = 10, style = "none")
cmds.separator (h = 10, style = "none")
Text_3 = cmds.text ("Insert your Values Below", align = ("left"))
Text_4 = cmds.text (" ")
cmds.separator (h = 10, style = "none")
cmds.separator (h = 10, style = "none")
Text_5 = cmds.text ("Insert Your Height in Meters:", align = ("left"))
Float_1 = cmds.floatField ()
cmds.separator (h = 10, style = "none")
cmds.separator (h = 10, style = "none")
Text_6 = cmds.text ("Insert Your Weight in Kilograms:", align = ("left"))
Float_2 = cmds.floatField ()
cmds.separator (h = 10, style = "none")
cmds.separator (h = 10, style = "none")

#Create Button
cmds.button (label = "Calculate BMI", c = calculateBMI, w = 400, h = 50, parent = mainLayout, align = ("center"))





#Show window
cmds.showWindow("exampleUI")
UI()
This is the problem :
# Error: RuntimeError: file <maya console> line 5: Object 'Float_1' not found. #

Any help would be appreciated
Cheers.

NaughtyNathan
11-21-2012, 10:00 AM
you haven't named your floatFields in the UI code. So when the calculateBMI function tries to query the named float field, there isn't one that has the given name!

MrDream
11-21-2012, 10:13 AM
Hi,
Thanks I solved it
I was wondering how would you edit a floatfiledgpr to print my BMI?? Or lets say I want to print a text on my UI EG: you are overweighted.... I got all the if done but it is only printing it in the script editor.
I would like to make it appear in my UI
Thanks again

CGTalk Moderation
11-21-2012, 10:13 AM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.