get a string for previous def()

Asked by Yannick Tremblay

Hi all,

I have this script :

def choosetemplate():
 template=input("Choississez un Template" +
 Key.ENTER + "1 - Add Access")
 click( )
 if (template=="1"):
  type("a\n")
 else:
  type("aa\n")

def descriptionfield():

 if (template=="1"):
  type("add access")

choosetemplate()
wait(1)
click( )
descriptionfield()

The whole script is more elaborated than that but this is a small part that i have problem with. I'm new with python since i found this sikuli software and I would like to know how I can reuse the template string from ''choosetemplate()'' in ''descriptionfield'' def function.

Basically the choosetemplate function is to ask the user to choose a template from 31 choice(that part is done and work well) and the descriptionfield one is to add a description in a text field related to the template the user had chosen. So i want to reuse the input string of choosetemplate.

Thank you.

Question information

Language:
English Edit question
Status:
Solved
For:
SikuliX Edit question
Assignee:
No assignee Edit question
Solved by:
RaiMan
Solved:
Last query:
Last reply:
Revision history for this message
Best RaiMan (raimund-hocke) said :
#1

def choosetemplate():
 template=input("Choississez un Template\n1 - Add Access")
 click( )
 if (template=="1"):
  type("a\n")
 else:
  type("aa\n")
 return template

def descriptionfield(template):
 if (template=="1"):
  type("add access")

t = choosetemplate()
wait(1)
click( )
descriptionfield(t)

comment:
Do not use the Key constants outside of type() or keyDown(). their content might change with localization of type().
Just \n is what you need for a newline with all text related stuff

Revision history for this message
Yannick Tremblay (bladewar80) said :
#2

Thanks RaiMan, that solved my question.

Revision history for this message
RaiMan (raimund-hocke) said :
#3

If you are interested in being more Python like:

# describe your environment
templates = {} # empty dictonary
templates['1'] = ['1 - Add Access', 'a', 'add access']
templates['2‘] = ['2 - Some Text', 'aa', 'something else']
.... you might have it ;-)

# the following def might be made even more general
def choosetemplate():
 message="Choississez un Template"
 for i in range(1, len(templates)+1):
  message += "\n" + templates[str(i)][0]
 n = input(message)
 while not n in templates:
   n = input(message)
 return n

no need for a def descriptionfield(template):

chosen = choosetemplate()
click() # this I would not do inside the def - it is workflow
type(templates[chosen][1])
wait(1)
click( )
type(templates[chosen][2])