如何修复“- _tkinter.TclError:绑定中没有指定事件" [英] How to fix "- _tkinter.TclError: no events specified in binding"
问题描述
我在互联网上的某个地方找到了源代码一个简单的程序来创建一个带有选项的窗口输入数学运算并从键盘显示结果.这是代码:
I found the source code somewhere on the internet a simple program to create a window with the option entering mathematical operations and displaying from the keyboard the result.This is code:
import tkinter as tk
from math import *
def evaluate(event):
res.configure(text="Result: " + str(eval(entry.get())))
w = tk.Tk()
tk.Label(w, text="Your Expression:").pack()
entry = tk.Entry(w)
entry.bind("event", evaluate)
entry.pack()
res = tk.Label(w)
res.pack()
w.mainloop()
我收到一个错误:
C:\Users\rob\PycharmProjects\untitled2\venv\Scripts\python.exeC:/Users/rob/Desktop/new test.py"回溯(最近一次调用):文件C:/Users/rob/Desktop/new test.py",第 12 行,在entry.bind("",evaluate) 文件 "C:\Users\rob\AppData\Local\Programs\Python\Python37\lib\tkinter__init__.py",第 1248 行,绑定中返回 self._bind(('bind', self._w), sequence, func, add) 文件 "C:\Users\rob\AppData\Local\Programs\Python\Python37\lib\tkinter__init__.py",第 1203 行,在 _bind 中self.tk.call(what + (sequence, cmd))_tkinter.TclError:绑定中没有指定事件
C:\Users\rob\PycharmProjects\untitled2\venv\Scripts\python.exe "C:/Users/rob/Desktop/new test.py" Traceback (most recent call last): File "C:/Users/rob/Desktop/new test.py", line 12, in entry.bind("", evaluate) File "C:\Users\rob\AppData\Local\Programs\Python\Python37\lib\tkinter__init__.py", line 1248, in bind return self._bind(('bind', self._w), sequence, func, add) File "C:\Users\rob\AppData\Local\Programs\Python\Python37\lib\tkinter__init__.py", line 1203, in _bind self.tk.call(what + (sequence, cmd)) _tkinter.TclError: no events specified in binding
请帮忙.我开始学习 Python,但我不知道解决方案.
Please help. Im starting learning Python and I don't know a solution.
推荐答案
很明显你报告的错误,
...绑定中没有指定事件"
"...no events specified in binding"
改变这个
entry.bind('<event>', evaluate)
有了这个
entry.bind("<Return>",evaluate)
这篇关于如何修复“- _tkinter.TclError:绑定中没有指定事件"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!