Python Tkinter计算器
我确实尝试过Python 3.3和2.7,它们都在“def_init_u2;(self)”之后指出语法错误: 有没有什么补救办法或其他什么办法?提前谢谢Python Tkinter计算器,python,tkinter,Python,Tkinter,我确实尝试过Python 3.3和2.7,它们都在“def_init_u2;(self)”之后指出语法错误: 有没有什么补救办法或其他什么办法?提前谢谢 from Tkinter import * class Calc(): def_init_(self): self.total = 0 self.current = "" self.new_num = True self.op_pending = False
from Tkinter import *
class Calc():
def_init_(self):
self.total = 0
self.current = ""
self.new_num = True
self.op_pending = False
self.op = ""
self.eq_flag = False
def num_press(self, num):
temp = text_box.get()
self.eq_flag = False
temp2 = str(num)
if self.new_num == True:
self.current = temp2
self.new_num = False
else:
if temp2 == '.':
if temp2 in temp:
return
self.current = temp + temp2
text_box.delete(0, END)
text_box.insert(0, self.current)
def calc_total(self):
if self.op_pending == True:
self.do_sum()
self.op_pending = False
def do_sum(self):
self.current = float(self.current)
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
text_box.delete(0, END)
text_box.insert(0, self.total)
self.new_num = True
def operation(self, op):
if self.op_pending == True:
self.do_sum()
self.op = op
else:
self.op_pending = True
if self.eq_flag == False:
self.total = float(text_box.get())
else:
self.total = self.current
self.new_sum = True
self.op = op
self.eq_flag = False
def cancel(self):
text_box.delete(0, END)
text_box.insert(0, "0")
self.new_num = True
def all_cancel(self):
self.cancel()
self.total = 0
def sign(self):
self.current = -(float(text_box.get()))
text_box.delete(0, END)
text_box.insert(0, self.current())
numbers = "789456123"
i = 0
bttn= []
for k in range(1,4):
for k in range(3):
bttn.append(Button(calc, text = numbers[i]))
bttn[i].grid(row = j, column = k, pady = 5)
bttn[i]["command"] = lambda x = numbers[i]: sum1.num_press(x)
i += 1
在def
和函数名之间添加空格。如果要指定特殊的初始化方法,请确保它们是双下划线
def_init_(self):
然后,您的程序将在语法上正确(尽管它仍然不会运行,因为
calc
未在全局范围中定义)请在此处发布代码,而不是在链接中发布代码。如果太长,只发布相关部分。对不起,我是stackoverflow.com的新手
def __init__(self):