Prevent focusing Text Field in UI when launching


#1

Hi, I’ve made a simple UI (with cmds only) and I got a cmds.textfield where the user can insert some text. The problem is the text field is always focused when the tool is executed and I want to be deselected by default, so he won’t be editing the textfield when he actually wanted to do some operation in Maya with the tool opened.

Reading through the old posts, there was a recommendation mentioning setFocus to the last used panel, but it didn’t work. It still opens the UI with the texfield selected and active.

current_panel = cmds.getPanel( withFocus=True )
# open UI command here
cmds.setFocus(current_panel)

Another extra problem is that since the textfield is focused on launching, it triggers the receiveFocusCommand flag.

A test code:

if cmds.window( 'UItest', exists=True): cmds.deleteUI('UItest', window = True)
thiswindow = cmds.window('UItest', title='This is a Test', w=250, h=100, sizeable=False )

cmds.frameLayout( label='Frame', lv=False )
cmds.textField( placeholderText='This is a Test' )
cmds.setParent('..')

cmds.showWindow(thiswindow)

#2

A friend just showed me a workaround on how to do this.

The answer is quite simple, make the textField non editable, or disabled (en=False, or ed=False) and then activate it after the showWindow command.
This will also prevent the receiveFocusCommand trigger

if cmds.window( 'UItest', exists=True): cmds.deleteUI('UItest', window = True)
thiswindow = cmds.window('UItest', title='This is a Test', w=250, h=100, sizeable=False )

cmds.frameLayout( label='Frame', lv=False )
cmds.textField( 'TestTxt', placeholderText='This is a Test', ed=False)
cmds.setParent('..')

cmds.showWindow(thiswindow)
cmds.textField( 'TestTxt', e=True, ed=True)