"Splinter Calculator" - A Simple Maya Tool Made with Python3

Tutorial / 30 October 2023
import maya.cmds as cmds


# Create a window
window = cmds.window(title="Python Calculator", sizeable=False)
cmds.columnLayout(adjustableColumn=True)


# Create input fields for numbers
number1 = cmds.textField(text='0', height=30)
number2 = cmds.textField(text='0', height=30)


# Create buttons for operations
cmds.rowLayout(numberOfColumns=4, columnWidth4=(100, 100, 100, 100))
add_button = cmds.button(label='+', command=lambda x: add_numbers(number1, number2, result))
subtract_button = cmds.button(label='-', command=lambda x: subtract_numbers(number1, number2, result))
multiply_button = cmds.button(label='*', command=lambda x: multiply_numbers(number1, number2, result))
divide_button = cmds.button(label='/', command=lambda x: divide_numbers(number1, number2, result))
cmds.setParent('..')


# Create a text field for the result
result = cmds.textField(editable=False, height=30)


# Define the functions for the operations
def add_numbers(num1, num2, result_field):
    num1 = cmds.textField(num1, query=True, text=True)
    num2 = cmds.textField(num2, query=True, text=True)
    result = int(num1) + int(num2)
    cmds.textField(result_field, edit=True, text=str(result))


def subtract_numbers(num1, num2, result_field):
    num1 = cmds.textField(num1, query=True, text=True)
    num2 = cmds.textField(num2, query=True, text=True)
    result = int(num1) - int(num2)
    cmds.textField(result_field, edit=True, text=str(result))


def multiply_numbers(num1, num2, result_field):
    num1 = cmds.textField(num1, query=True, text=True)
    num2 = cmds.textField(num2, query=True, text=True)
    result = int(num1) * int(num2)
    cmds.textField(result_field, edit=True, text=str(result))


def divide_numbers(num1, num2, result_field):
    num1 = cmds.textField(num1, query=True, text=True)
    num2 = cmds.textField(num2, query=True, text=True)
    result = int(num1) / int(num2)
    cmds.textField(result_field, edit=True, text=str(result))


# Show the window
cmds.showWindow(window)