Tkinter : How to limit user to a number of functions Tkinter : How to limit user to a number of functions tkinter tkinter

Tkinter : How to limit user to a number of functions


Avoid eval() -- instead consider a datastructure of legal commands and treat the code that implements those commands as data. I've implemented an example below with just the turtle module to keep it simple:

import turtlecommands = {    'moveForward': lambda pixels: turtle.forward(pixels),    'moveBackward': lambda pixels: turtle.backward(pixels),    # ...}command, *arguments = input().strip().split()if command in commands:    (commands[command])(int(arguments[0]))turtle.mainloop()

Run the program and type 'moveForward 100' and it should do what you ask, no eval needed. This can get more sophisticated, with information in the dictionary on the number and types of the arguments for each command.