【问题标题】:Calculator in TkinterTkinter 中的计算器
【发布时间】:2017-04-28 21:29:56
【问题描述】:

我正在为一个学校项目制作一个计算器,老师告诉我该怎么做,它奏效了。或者我想当我清理程序时我意识到它只能工作“+”并且没有其他按钮。任何帮助将不胜感激。

tempstr=my_text.get()
loopval=len(tempstr)
op=tempstr.index("+")
if (op>0):
    leftnum=tempstr[0:op]
    rightnum=tempstr[op+1:loopval+1]
    answe=int(leftnum)+int(rightnum)
    print (answe)
    my_text.insert(END,str(answe))
op=tempstr.index("-")
if (op>0):
    leftnum=tempstr[0:op]
    rightnum=tempstr[op+1:loopval+1]
    answe=int(leftnum)-int(rightnum)
    print (answe)
    my_text.insert(END,str(answe))
op=tempstr.index("*")
if (op>0):
    leftnum=tempstr[0:op]
    rightnum=tempstr[op+1:loopval+1]
    answe=int(leftnum)*int(rightnum)
    print (answe)
    my_text.insert(END,str(answe))
op=tempstr.index("/")
if (op>0):
    leftnum=tempstr[0:op]
    rightnum=tempstr[op+1:loopval+1]
    answe=int(leftnum)/int(rightnum)
    print (answe)
    my_text.insert(END,str(answe))

【问题讨论】:

    标签: python-3.x tkinter calculator


    【解决方案1】:

    index 如果未找到子字符串,则会引发错误。 tkinter 中的错误会停止函数但不会停止循环,因此您可能没有注意到它。您的意思是使用 find 如果找不到则返回 -1。

    最好是这样:

    if "+" in tempstr:
        op=tempstr.index("+")
        leftnum=tempstr[0:op]
        rightnum=tempstr[op+1:loopval+1]
        answe=int(leftnum)+int(rightnum)
        print (answe)
        my_text.insert(END,str(answe))
    elif '-' in tempstr:
        op=tempstr.index("-")
        leftnum=tempstr[0:op]
        rightnum=tempstr[op+1:loopval+1]
        answe=int(leftnum)-int(rightnum)
        print (answe)
        my_text.insert(END,str(answe))
    

    【讨论】:

      猜你喜欢
      • 2013-11-24
      • 2020-03-18
      • 1970-01-01
      • 2021-04-03
      • 2016-12-09
      • 2013-04-29
      • 2015-09-25
      • 2020-04-20
      • 2021-03-25
      相关资源
      最近更新 更多