Search code examples
pythontkintereval

eval() takes no keyword argument


Code:

import tkinter as tk

win = tk.Tk()

text = tk.Text(win, width = 50, height = 20, bg = 'black', fg = 'lightgreen', wrap = tk.WORD)
text.pack()

text.tag_configure('prompt', foreground = 'magenta')
text.tag_configure('output', foreground = 'yellow')

text.insert('end', '>>> ', 'prompt')

def on_return(*args):
  cmd = text.get('prompt.last', 'end')
  if cmd:
     try:
       output = str(eval(cmd, globals = {"__builtins__":None}))
     except Exception as e:
       output = str(e)
  text.insert('end', '\n' + output, 'output')
  text.insert('end', '\n>>> ' + output, 'prompt')
  return 'break'

text.bind('<Return>', on_return)

win.mainloop()

In order to get this output: eval() takes no keyword argument, just type anything on the text widget and press enter.

What should I do to avoid this output?


Solution

  • In your code you need to remove the 'globals =' from the parameter passing in the dictionary. The correct line is:

    output = str(eval(cmd, {"__builtins__":None}))