2014-03-18 88 views
0
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 

我曾嘗試的Python 3.3和2.7都表示,以後的語法錯誤「def_init_(個體經營):」 是否有任何修復或東西是什麼?在此先感謝Python的Tkinter的計算器

+2

在這裏,而不是發佈您的代碼中的鏈接。如果太長,只發布相關部分。 – iCodez

+0

對不起,我是stackoverflow.com的新成員 – user3250207

回答

1
def_init_(self): 

def和函數名稱之間加上一個空格。如果你想指定特殊的初始化方法,請確保它們是雙下劃線。然後

def __init__(self): 

你的計劃將是語法正確的(雖然它仍然不會運行,因爲calc沒有在全球範圍內定義)