Here is my example code that I always use as a starting point to create windows in Python.
This should help you. Just copy the code in a file named windowExample.py for the execution code at the end to work
[codebox]import maya.cmds as mc
def window():
""" create a window """
window = "myWindow"
windowTitle = "My Window | version 1.0"
if (mc.window(window, exists=True)):
mc.deleteUI(window, window=True)
if( mc.windowPref(window, exists=True) ):
# mc.windowPref(window, remove=True)
pass
window = mc.window(window,sizeable=True, toolbox=False, resizeToFitChildren=True, title=windowTitle)
# Create window form layout.
windowForm = mc.formLayout("windowForm")
# Create the tab Layout.
mainForm = mc.formLayout("mainForm")
co = mc.columnLayout('columnLeft', adjustableColumn=True, columnAlign='left', columnAttach=['both',0])
buttonForm = mc.formLayout(parent=windowForm)
buttonHeight = 26
applyButton = mc.button( height=buttonHeight, label='Apply', command='windowExample.\_\_windowApply()' )
applyCloseButton = mc.button( height=buttonHeight, label='Apply and Close', command='windowExample.\_\_windowApply("%s",True)' %window)
closeButton = mc.button( height=buttonHeight, label='Close', command='windowExample.\_\_windowClose("%s")' %window )
# Apply attachments to the buttons so that they appear centred
# in the window.
#
space = 4 # Space between buttons.
mc.formLayout(buttonForm, edit=True, numberOfDivisions=100,
attachForm=[(applyButton,'top',0), (applyButton,'left',0), (applyButton,'bottom',0), (applyCloseButton,'top',0), (applyCloseButton,'bottom',0), (closeButton,'top',0), (closeButton,'bottom',0), (closeButton,'right',0)],
attachPosition=[(applyButton,'right',space/2,33), (applyCloseButton,'left',space/2,33), (applyCloseButton,'right',space/2,66), (closeButton,'left',space/2,66)])
# Attach the mainTab and the buttonRow to the windowForm
mc.formLayout(windowForm, edit=True,
attachForm=[(mainForm,'top',3),(mainForm,'right',3),(mainForm,'left',3), (buttonForm,'right',3),(buttonForm,'left',3),(buttonForm,'bottom',3)],
attachControl=[(mainForm,'bottom',3,buttonForm)],
attachNone=[(buttonForm,'top')])
mc.showWindow(window)
def __windowApply(window=None, closeWindow=False ):
""" read the option in the window and call the proper function """
# read window value
print 'getting window values'
# close the window
if closeWindow == True and window is not None:
mc.deleteUI(window, window=True)
def __windowClose(window=None):
if window is not None:
mc.deleteUI(window, window=True)
"""
#example
import windowExample
reload(windowExample)
windowExample.window()
"""[/codebox]