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:
- import maya.cmds as cmds #this imports the maya commands library
- 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
- if cmds.window(windowID, exists=True): #deleting the window if it already exists.
- #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
- cmds.window(windowID, title='Text Field Demo', sizeable=True, width = 500)
- #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.
- #In the rowColumnLayout you are telling it that it has 1 column, that the first (and only) column width is 500 pix.
- cmds.rowColumnLayout(numberOfColumns=1, columnWidth=[(1,500)])
- cmds.textField()#Creating the text field
- cmds.setParent('..')#This is sort of the end statement of the rowColumnLayout.
- 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: