Python eval()不接受关键字参数 代码: 将tkinter作为tk导入 win=tk.tk() text=tk.text(win,宽=50,高=20,背景为“黑色”,前景为“浅绿色”,环绕为tk.WORD) text.pack() text.tag_configure('prompt',前台='magenta') text.tag\u configure('output',前台='yellow') text.insert('end'、'>>>'、'prompt') 返回时的def(*args): cmd=text.get('prompt.last','end') 如果cmd: 尝试: 输出=str 例外情况除外,如e: 输出=str(e) text.insert('end','\n'+输出,'output') text.insert('end','\n>>>'+输出,'prompt') 返回“中断” text.bind(“”,返回时) win.mainloop()
为了获得这个输出:eval()不接受关键字参数,只需在文本小部件上键入任何内容并按enter键Python eval()不接受关键字参数 代码: 将tkinter作为tk导入 win=tk.tk() text=tk.text(win,宽=50,高=20,背景为“黑色”,前景为“浅绿色”,环绕为tk.WORD) text.pack() text.tag_configure('prompt',前台='magenta') text.tag\u configure('output',前台='yellow') text.insert('end'、'>>>'、'prompt') 返回时的def(*args): cmd=text.get('prompt.last','end') 如果cmd: 尝试: 输出=str 例外情况除外,如e: 输出=str(e) text.insert('end','\n'+输出,'output') text.insert('end','\n>>>'+输出,'prompt') 返回“中断” text.bind(“”,返回时) win.mainloop(),python,tkinter,eval,Python,Tkinter,Eval,为了获得这个输出:eval()不接受关键字参数,只需在文本小部件上键入任何内容并按enter键 我应该怎么做才能避免这种输出?在代码中,需要从传入字典的参数中删除“globals=”。正确的行是: 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.ta
我应该怎么做才能避免这种输出?在代码中,需要从传入字典的参数中删除“globals=”。正确的行是:
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()
不按关键字传递
globals
参数?它只是位置性的,只需删除globals=
。错误信息非常清楚。不要使用关键字参数。请参阅文档。@TigerhawkT3首先,如果它是清楚的,我会理解的。像clear这样的词是给蹩脚的解释者的。它确实很清楚。如果您不理解,这意味着您不仅在不知道关键字参数是什么的情况下使用了关键字参数,而且即使在错误消息中指定了关键字参数,也没有查找它们。这不是本网站的目的。@TigerhawkT3我发现这些文档对我来说不清楚。
output = str(eval(cmd, {"__builtins__":None}))