You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python Tkinter计算器开发遇Unexpected EOF错误求助

解决Tkinter计算器的Unexpected EOF错误及功能异常

你的代码存在几个关键问题,直接导致了Unexpected EOF错误和计算器功能失效,以下是问题分析和修正方案:

核心问题点

  • 错误的eval调用:insert函数里的eval(var)是错误的,var是StringVar对象而非字符串,且此处无需提前执行计算,完全多余。
  • 等号按钮绑定逻辑错误:command = equals(var.get())会在程序初始化时立即执行equals函数,而非点击按钮时触发,需要用lambda延迟执行。
  • 不兼容的运算符:÷和×不是Python支持的运算符,eval无法解析,需在插入时替换为/和*,同时保留按钮显示的原符号。
  • clear函数的错误设置:var.set(" ")会留下空格,导致后续计算出错,应改为空字符串""。

修正后的完整代码

from tkinter import *

e = Tk(className="Krishna's Calculator")
e.geometry("460x614")
e.resizable(0,0)

def insert(value):
    # 替换特殊运算符为Python可识别的符号
    if value == "÷":
        value = "/"
    elif value == "×":
        value = "*"
    var.set(var.get() + value)

def button(text, width, font, highlightbackground, x, y):
    tkinter_button = Button(e, text=text, width=width, command=lambda: insert(text), font=font, highlightbackground=highlightbackground)
    tkinter_button.place(x=x, y=y)

def clear():
    var.set("")

def equals(vare):
    try:
        # 捕获计算错误,避免程序崩溃
        result = eval(vare)
        var.set(str(result))
    except Exception as err:
        var.set("Error")

helvetica = "Helvetica 50"
arial = "Arial 50 bold"
var = StringVar()
label = Label(e, textvariable=var, bd=5, width=16, relief=SOLID, font="Arial 50 ", bg="white", fg="black", activebackground="#bb99ff", height=5, pady=3)
label.place(x=0,y=0)

button(text="÷", width=4, font=helvetica, highlightbackground='#8533ff', x=344, y=301)
button(text="×", width=4, font=helvetica, highlightbackground='#8533ff', x=344, y=364)
button(text="-", width=4, font=helvetica, highlightbackground='#8533ff', x=344, y=426)
button(text="+", width=4, font=helvetica, highlightbackground='#8533ff', x=344, y=488)

# 修正command绑定,用lambda延迟执行
equal = Button(e, text="=", width=4, command=lambda: equals(var.get()), font="Helvetica 50", highlightbackground='#8533ff')
equal.place(x=344, y=551)

aclear = Button(e, text="AC", width=8, command=clear, font="Helvetica 50", highlightbackground='#737373')
aclear.place(x=0, y=302)

button(text="%", width=4, font=helvetica, highlightbackground='#737373', x=228, y=302)
button(text=".", width=4, font=arial, highlightbackground='#ffffff', x=228, y=550)
button(text="0", width=8, font=arial, highlightbackground='#ffffff', x=0, y=550)
button(text="1", width=4, font=arial, highlightbackground='#ffffff', x=0, y=488)
button(text="2", width=4, font=arial, highlightbackground='#ffffff', x=114, y=488)
button(text="3", width=4, font=arial, highlightbackground='#ffffff', x=228, y=488)
button(text="4", width=4, font=arial, highlightbackground='#ffffff', x=0, y=426)
button(text="5", width=4, font=arial, highlightbackground='#ffffff', x=114, y=426)
button(text="6", width=4, font=arial, highlightbackground='#ffffff', x=228, y=426)
button(text="7", width=4, font=arial, highlightbackground='#ffffff', x=0, y=364)
button(text="8", width=4, font=arial, highlightbackground='#ffffff', x=114, y=364)
button(text="9", width=4, font=arial, highlightbackground='#ffffff', x=228, y=364)

e.mainloop()

额外添加了try-except块捕获计算错误(如除以零、非法表达式),避免程序直接崩溃。

内容的提问来源于stack exchange,提问作者Ippos

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.23 07:24:24