0

The error reads as:

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

The code is:

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()

My main question would be not only what errors exist in my code, but what is meant by: string index out of range.

Any help would be appreciated, as I am still rather new to programming. Thanks in advance.

It is worth noting the program is by no means finished.

JPott99
  • 1
  • 2
  • Not directly your error, but `num1[g] is not "+" or "-" or "*" or "/"` does not do what you think it does. See [How do I test one variable against multiple values?](http://stackoverflow.com/q/15112125) – Martijn Pieters Jan 19 '15 at 20:20
  • Scratch that, that is the direct cause, because that `while` loop never ends. – Martijn Pieters Jan 19 '15 at 20:20
  • Use `num1[g] in '+-*/'` instead. – Martijn Pieters Jan 19 '15 at 20:21
  • Also worth noting: use `==` to compare strings, not `is` (which only tests identity, and it's not necessarily true that any two strings which both have `"a"` as the value are the same string.) – DSM Jan 19 '15 at 20:25

1 Answers1

0

The first time you initialize num1, it's set to "" and never changes, therefore raising an error when trying to access the index 0. But that's not the only problem, see Martijn's comments for more.

TidB
  • 1,749
  • 1
  • 12
  • 14