Blog

Text Fields in Maya Explained (Python)

Posted by:

Text fields are confusing.  It’s easy to display one, but how do you get information from one?  How do you update them?  Maybe you should look to the Autodesk website for detailed information… HaHaHahahaaaa.  Sorry, that one always gets me.

This is the code that goes in the GUI layout:

cmds.textField()
Simple.
So to see that in action in a GUI copy this code:
  1. import maya.cmds as cmds #this imports the maya commands library
  2.  
  3.  
  4. windowID = 'SomeGoddamedWindow' #This just sets the window's identification so that if there is a window already open by that exact name then the code knows about it and will delete it
  5.  
  6. if cmds.window(windowID, exists=True): #deleting the window if it already exists.
  7. cmds.deleteUI(windowID)
  8.  
  9. #Here we are identifing the GUI window, windowID is assigned (defined above), the title is what is in the title bar and it is 500 Pixels wide
  10. cmds.window(windowID, title='Text Field Demo', sizeable=True, width = 500)
  11. #All GUI stuff, buttons and text fields and the like need to reside in a layout. There are many types of layouts but a row column is the easiest to get started with.
  12. #In the rowColumnLayout you are telling it that it has 1 column, that the first (and only) column width is 500 pix.
  13. cmds.rowColumnLayout(numberOfColumns=1, columnWidth=[(1,500)])
  14. cmds.textField()#Creating the text field
  15.  
  16. cmds.setParent('..')#This is sort of the end statement of the rowColumnLayout.
  17.  
  18. cmds.showWindow()#Displaying the window (important if you want to see it)

Great, that is a text field.  But how do you use it?  Well let’s say you want to read the entry from that text field.  Well we will need a function to query the text field:

 

 

0
  Related Posts
  • No related posts found.