python - 如何修复 "- _tkinter.TclError: no events specified in binding"

标签 python tkinter

我在互联网上找到了源代码 一个简单的程序,用于创建带有选项的窗口 从键盘输入数学运算并显示 结果。这是代码:

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.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,但我不知道解决方案。

最佳答案

您报告的错误很清楚,

“...绑定(bind)中未指定事件”

改变这个

    entry.bind('<event>', evaluate)

有了这个

    entry.bind("<Return>",evaluate)

enter image description here

关于python - 如何修复 "- _tkinter.TclError: no events specified in binding",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56343229/

相关文章:

python - 如何使递归重命名操作(Linux,使用python)可逆?

python - _tkinter.tclerror : bad option set tcl value number must be activate, 获取、配置

python - 当 tkinter 窗口关闭时,python 程序并未结束

Python 3.x - 在 tkinter 中切换全屏

python - 将数据分组到数组中(python)

python - Theano 高级张量索引,共享索引

python - Tkinter 事件处理程序无法查看全局变量

python - 从文件加载数据并在 tkinter 中使用它

python - 将 Flask 应用部署为 Windows 服务

python - 基于 Django 类的 View 在 session 中保留 url 参数