TypeError甚至已转换

时间:2017-08-21 13:55:37

标签: python math tkinter

我尝试制作限制计算器,但Python一直告诉我:

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Users\User\AppData\Local\Programs\Python\Python36-
32\lib\tkinter\__init__.py", line 1699, in __call__
    return self.func(*args)
  File "C:\Users\User\Desktop\practice1.py", line 44, in submit
    print(type(str(limit+1))==type(12))
TypeError: must be str, not int

我的代码在这里:

import re
from math import *
from tkinter import *


root=Tk()
root.title('Limit')
root.geometry('300x300')

lab=Label(text='This is limit calculator.\nCheck out the functions to use in the command line.')
explain=Label(text='limit as x goes to...')
entry=Entry()
Expl=Label(text='The equation is...')
lim=Entry()


print('''X to the power of y is typed in as x**y,
x times y is typed in as x*y, and
x divided by y is typed in as x/y.
You can also use functions like:
    1. trigonometry functions like sin, asin, asinh, cos, etc
    2. ceil (ceiling function) and floor

    3. degrees (radians to degrees) and radians (degrees to radians)
    4. fabs (decimal absolute value)
    5. factorial and gamma
    6. log (type log ( number, base))
    7. sqrt (square root)
    8. e and pi
    9. boolean operators
    10. other minor functions''')

def submit():
    a=str(entry.get())
    limit=lim.get()
    entry.delete(0,'end')
    lim.delete(0,'end')

    if 'x' not in a:
        lab.configure(text=str(eval(a)))
        return None

    print(type('x')==type(12))
    print(type(str(limit+1))==type(12))
    print(type(a)==type(1))
    b=re.sub('x',str(limit+1),a)
    c=re.sub('x',str(limit-1),a)
    try:
        ans1=eval(b)
        ans2=eval(c)
        ans_fin=(ans1+ans2)/2
        lab.configure(text=str(ans_fin))
    except Exception:
        b1=eval(re.sub(str(limit+1),b))
        b2=eval(re.sub(str(limit-1),b))
        c1=eval(re.sub(str(limit+1),c))
        c2=eval(re.sub(str(limit-1),c))
        cans=(c1+c2)/2
        bans=(b1+b2)/2
        real_ans=(cans+bans)/2
        lab.configure(text=str(real_ans))

button=Button(text='Calculate!',command=submit)

lab.pack()
explain.pack()
lim.pack()
Expl.pack()
entry.pack()

button.pack()


root.mainloop()

请说明导致此程序出现故障的错误。 我是Python的先驱。如果可能,请说明发现这些错误的位置以及解决方法。

这是我得到的所有信息。

我的电脑是Windows 10,64位。

1 个答案:

答案 0 :(得分:0)

您的limit对象可能是str,而不是int,因此您无法为其添加1。 你可以尝试:

print(type(str(int(limit)+1))==type(12))

如果限制应该是int