【发布时间】:2013-12-19 09:10:26
【问题描述】:
我正在使用 tkinter 在 python 中制作一个 GUI 计算器。计算器工作得很好,现在我想添加一个退格功能,以便清除显示屏上的最后一个数字。例如 321 将变为 32。我尝试定义函数“退格”和“bind_all”方法,但我不太确定它们是如何工作的,从而导致错误消息。如果有人能告诉我如何处理并解释它,将不胜感激。
非常感谢任何帮助。
from tkinter import *
def quit ():
root.destroy()
# the main class
class Calc():
def __init__(self):
self.total = 0
self.current = ""
self.new_num = True
self.op_pending = False
self.op = ""
self.eq = False
#setting the variable when the number is pressed
def num_press(self, num):
self.eq = False
temp = text_box.get()
temp2 = str(num)
if self.new_num:
self.current = temp2
self.new_num = False
else:
if temp2 == '.':
if temp2 in temp:
return
self.current = temp + temp2
self.display(self.current)
# event=None to use function in command= and in binding
def clearLastDigit(self, event=None):
current = self.text_box.get()[:-1]
self.text_box.delete(0, END)
self.text_box.current(INSERT, text)
def calc_total(self):
self.eq = True
self.current = float(self.current)
if self.op_pending == True:
self.do_sum()
else:
self.total = float(text_box.get())
#setting up the text display area
def display(self, value):
text_box.delete(0, END)
text_box.insert(0, value)
#Opperations Button
def do_sum(self):
if self.op == "add":
self.total += self.current
if self.op == "minus":
self.total -= self.current
if self.op == "times":
self.total *= self.current
if self.op == "divide":
self.total /= self.current
self.new_num = True
self.op_pending = False
self.display(self.total)
def operation(self, op):
self.current = float(self.current)
if self.op_pending:
self.do_sum()
elif not self.eq:
self.total = self.current
self.new_num = True
self.op_pending = True
self.op = op
self.eq = False
#Clear last entry
def cancel(self):
self.eq = False
self.current = "0"
self.display(0)
self.new_num = True
#Clear all entries
def all_cancel(self):
self.cancel()
self.total = 0
#backspace button
def backspace(self):
self.cancel()
self.display(len(self.text_box.get())-1)
#Changing the Sign (+/-)
def sign(self):
self.eq = False
self.current = -(float(text_box.get()))
self.display(self.current)
#Global Varibles that are used within Attributes
sum1 = Calc()
root = Tk()
calc = Frame(root)
calc.grid()
#Creating the window for the calculator
root.title("Calculator")
text_box = Entry(calc, justify=RIGHT)
text_box.grid(row = 0, column = 0, columnspan = 3, pady = 5)
text_box.insert(0, "0")
#buttons 1-9 (Displayed row by row)
numbers = "789456123"
i = 0
bttn = []
for j in range(1,4):
for k in range(3):
bttn.append(Button(calc, text = numbers[i], width= 5, height = 2, bg="#fe0000"))
bttn[i].grid(row = j, column = k)
bttn[i]["command"] = lambda x = numbers[i]: sum1.num_press(x)
i += 1
#button 0
bttn_0 = Button(calc, text = "0", width= 5, height = 2, bg="#fe0000")
bttn_0["command"] = lambda: sum1.num_press(0)
bttn_0.grid(row = 4, column = 1, pady = 5)
#button / (Divide)
bttn_div = Button(calc, text = chr(247), width= 5, height = 2, bg="#00b0f0" )
bttn_div["command"] = lambda: sum1.operation("divide")
bttn_div.grid(row = 1, column = 3, pady = 5)
#button x (Times)
bttn_mult = Button(calc, text = "x", width= 5, height = 2, bg="#00b0f0")
bttn_mult["command"] = lambda: sum1.operation("times")
bttn_mult.grid(row = 2, column = 3, pady = 5)
#button - (Minus)
minus = Button(calc, text = "-", width= 5, height = 2, bg="#00b0f0")
minus["command"] = lambda: sum1.operation("minus")
minus.grid(row = 4, column = 3, pady = 5)
#button + (Plus)
add = Button(calc, text = "+", width= 5, height = 2, bg="#00b0f0")
add["command"] = lambda: sum1.operation("add")
add.grid(row = 3, column = 3, pady = 5)
#button + or - (Plus/minus)
neg= Button(calc, text = "+/-", width= 5, height = 2, bg="#7030a0")
neg["command"] = sum1.sign
neg.grid(row = 5, column = 0, pady = 5)
#button Clear (Clear)
clear = Button(calc, text = "C", width= 5, height = 2, bg="yellow")
clear["command"] = sum1.cancel
clear.grid(row = 5, column = 1, pady = 5)
#button All Clear ( All Clear)
all_clear = Button(calc, text = "CE", width= 5, height = 2, bg="yellow")
all_clear["command"] = sum1.all_cancel
all_clear.grid(row = 5, column = 2, pady = 5)
#button . (Decimal)
point = Button(calc, text = ".", width= 5, height = 2, bg="#c00000")
point["command"] = lambda: sum1.num_press(".")
point.grid(row = 4, column = 0, pady = 5)
#button = (Equals)
equals = Button(calc, text = "=", width= 5, height = 2, bg="#7030a0")
equals["command"] = sum1.calc_total
equals.grid(row = 4, column = 2, pady = 5)
#button Quit
quit_bttn = Button(calc, text ="Quit", width=5, height = 2, bg="green")
quit_bttn["command"] = quit
quit_bttn.grid(row = 5, column = 3)
#button BackSpace
backspace_bttn = Button(calc, text = "Backspace", width= 15, height = 2, bg="yellow")
backspace_bttn["command"] = sum1.backspace
backspace_bttn.grid(row = 6, column = 0, columnspan = 4)
root.mainloop()
【问题讨论】:
-
@furas 这是我收到的错误
line 106, in backspace self.display(len(self.text_box.get())-1) AttributeError: 'Calc' object has no attribute 'text_box' -
在原始版本中(请参阅我的答案中的链接)所有按钮都在课堂内,并且有
self.text_box但你有所有的课外所以你有text_box而不是self.text_box -
如果你在类之外有代码,你应该放弃使用类并将所有函数(经过一些修改)从类移到外面。有人可能会问“为什么你把一些函数放在类里面,而把另一些放在外面?你真的知道如何使用类吗?”
标签: python tkinter calculator