错误读作:
Exception in Tkinter callback
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/tkinter/__init__.py", line 1533, in __call__
return self.func(*args)
File "/Users/JackPottage/Documents/PythonProjects/Calculator/Calculator.py", line 76, in equals
while num1[g] is not "+" or "-" or "*" or "/":
IndexError: string index out of range
代码是:
import sys
from tkinter import *
root=Tk()
root.title("Calculator")
frame=Frame(root)
frame.pack()
topframe=Frame(root)
topframe.pack()
s=1
def clear():
txtDisplay.delete(0,END)
return
def one():
global s
txtDisplay.insert(s,"1")
s+=1
def two():
global s
txtDisplay.insert(s,"2")
s+=1
def three():
global s
txtDisplay.insert(s,"3")
s+=1
def four():
global s
txtDisplay.insert(s,"4")
s+=1
def five():
global s
txtDisplay.insert(s,"5")
s+=1
def six():
global s
txtDisplay.insert(s,"6")
s+=1
def seven():
global s
txtDisplay.insert(s,"7")
s+=1
def eight():
global s
txtDisplay.insert(s,"8")
s+=1
def nine():
global s
txtDisplay.insert(s,"9")
s+=1
def zero():
global s
txtDisplay.insert(s,"0")
s+=1
def plus():
global s
txtDisplay.insert(s,"+")
s+=1
def minus():
global s
txtDisplay.insert(s,"-")
s+=1
def times():
global s
txtDisplay.insert(s,"*")
s+=1
def divide():
global s
txtDisplay.insert(s,"/")
s+=1
def equals():
global num1
print(num1)
g=0
number1=str("")
while num1[g] is not "+" or "-" or "*" or "/":
number1=str(number1)+str(num1[g])
print(number1)
g=+1
One= Button(topframe, bd=8, text="1", bg="green", command=one)
One.grid(row=1, column=0)
Two= Button(topframe, bd=8, text="2", bg="green", command=two)
Two.grid(row=1, column=1)
Three= Button(topframe, bd=8, text="3", bg="green", command=three)
Three.grid(row=1, column=2)
Four= Button(topframe, bd=8, text="4", bg="green", command=four)
Four.grid(row=2, column=0)
Five= Button(topframe, bd=8, text="5", bg="green", command=five)
Five.grid(row=2, column=1)
Six= Button(topframe, bd=8, text="6", bg="green", command=six)
Six.grid(row=2, column=2)
Seven= Button(topframe, bd=8, text="7", bg="green", command=seven)
Seven.grid(row=3, column=0)
Eight= Button(topframe, bd=8, text="8", bg="green", command=eight)
Eight.grid(row=3, column=1)
Nine= Button(topframe, bd=8, text="9", bg="green", command=nine)
Nine.grid(row=3, column=2)
Zero= Button(topframe, bd=8, text="0", bg="green", command=zero)
Zero.grid(row=4, column=0)
num1=""
txtDisplay=Entry(frame, textvariable=num1, insertwidth=1, font=30, bg="Dark Orange")
txtDisplay.grid(columnspan=3)
Equals= Button(topframe, bd=8, text="=", bg="green", command=equals)
Equals.grid(row=4, column=2)
Clear= Button(topframe, bd=8, text="C", bg="green", command=clear)
Clear.grid(row=4, column=1)
Plus= Button(topframe, bd=8, text="+", bg="green", command=plus)
Plus.grid(row=1, column=3)
Minus= Button(topframe, bd=8, text="-", bg="green", command=minus)
Minus.grid(row=2, column=3)
Times= Button(topframe, bd=8, text="*", bg="green", command=times)
Times.grid(row=3, column=3)
Divide= Button(topframe, bd=8, text="/", bg="green", command=divide)
Divide.grid(row=4, column=3)
root.mainloop()
我的主要问题不仅是我的代码中存在哪些错误,而是指的是:字符串索引超出范围。
任何帮助都会受到赞赏,因为我对编程仍然很陌生。 提前谢谢。
值得注意的是,该计划并未完成。
答案 0 :(得分:0)
首次初始化num1
时,它会设置为""
并且永远不会更改,因此在尝试访问索引0时会引发错误。
但这不是唯一的问题,请参阅Martijn的评论。