menu

This function is used to allow the user to pick from a list of items.

You can pass a dictionary instead of a list, for when you're doing special formatting.

Initialization

It's a simple function call. It is blocking, and returns the user's choice. When the choices argument is a dictionary though, things are different. I'll explain that in a bit.

userChoice = tpil.gui.menu(["Choice 1", "Choice 2", "Choice 3"])

This would return the string the user picked, for example, "Choice 1"

Using dictionaries instead of lists

When using special, dynamic formatting, returning the raw string the user picked is not very useful.

choices = ["String {}".format(x) for x in range(3)]
userChoice = tpil.gui.menu(choices) # "String 1" or "String 2" or "String 3"

if userChoice == ???:
    # do something...

Of course, you can always figure something out, but that's a dirty workaround. Using dictionaries is a much easier way, with no workarounds. It shows the key to the user, but returns the key's value instead.

choices = {"String {}".format(x):x for x in range(3)} # {"String 1": 1..}
userChoice = tpil.gui.menu(choices) # Instead of returning "String 1" or "String 2" \
# it'll return the key's value. In this example, it could be 1, 2, or 3.

print("you picked \"String {}\"".format(userChoice))

This isn't the best example, but you get the point. There's many situations where this would be useful (at least in my cases).

Kwargs

For when you need the user's input, you can set disableBack to True, which forces the user to pick one of your choices.

Last updated