我正在尝试用Python 3.4.2制作一个计算器。我遇到的问题是我正在使用:
StringVar()
您可以通过错误消息看到这不适合我的目的。 还有其他选择吗?
以下是该计划:
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
num2=str(num1)
number1=str("")
while num1[g] is not "+"or"-"or"*"or"/":
number1=str(number1)+str(num2[g])
print(number1)
g=+1
One= Button(topframe, bd=20, text="1",bg="green", command=one)
One.grid(row=1, column=0)
Two= Button(topframe, bd=20, text="2", bg="green", command=two)
Two.grid(row=1, column=1)
Three= Button(topframe, bd=20, text="3", bg="green", command=three)
Three.grid(row=1, column=2)
Four= Button(topframe, bd=20, text="4", bg="green", command=four)
Four.grid(row=2, column=0)
Five= Button(topframe, bd=20, text="5", bg="green", command=five)
Five.grid(row=2, column=1)
Six= Button(topframe, bd=20, text="6", bg="green", command=six)
Six.grid(row=2, column=2)
Seven= Button(topframe, bd=20, text="7", bg="green", command=seven)
Seven.grid(row=3, column=0)
Eight= Button(topframe, bd=20, text="8", bg="green", command=eight)
Eight.grid(row=3, column=1)
Nine= Button(topframe, bd=20, text="9",bg="green", command=nine)
Nine.grid(row=3, column=2)
Zero= Button(topframe, bd=20, text="0", bg="green", command=zero)
Zero.grid(row=4, column=0)
num1=StringVar("")
txtDisplay=Entry(frame, textvariable=num1, insertwidth=1, font=30, bg="Dark Orange")
txtDisplay.grid(columnspan=3)
Equals= Button(topframe, bd=20, text="=", bg="green", command=equals)
Equals.grid(row=4, column=2)
Clear= Button(topframe, bd=20, text="C", bg="green", command=clear)
Clear.grid(row=4, column=1)
Plus= Button(topframe, bd=20, text="+", bg="green", command=plus)
Plus.grid(row=1, column=3)
Minus= Button(topframe, bd=20, text="-", bg="green", command=minus)
Minus.grid(row=2, column=3)
Times= Button(topframe, bd=20, text="*", bg="green", command=times)
Times.grid(row=3, column=3)
Divide= Button(topframe, bd=20, text="/", bg="green", command=divide)
Divide.grid(row=4, column=3)
root.mainloop()
该程序尚未完成,但我收到的错误消息是:
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/Python Projects/Calculator/Calculator.py", line 77, in equals
while num1[g] is not "+"or"-"or"*"or"/":
TypeError: 'StringVar' object does not support indexing
感谢您提供任何帮助。
答案 0 :(得分:1)
首先,请勿使用is
来比较字符串,while num1[g] is not "+"or"-"or"*"or"/":
将始终评估为True
为+
,因为布尔检查始终为True
}:
In [5]: bool("+")
Out[5]: True
In [6]: i = 3
In [7]: if i is not 4 or 3: # i is 3 but still we print 6
print 6
...:
6
使用类似:
while num1.get()[g] not in {"+","-","*","/"}
答案 1 :(得分:-1)
StringVar完全不是字符串。但是你可以用get()获得它的字符串值。试试这个:
while num1.get()[g] is not "+"or"-"or"*"or"/":