Python Tkinter计算器

时间:2014-03-18 18:46:17

标签: python tkinter

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_(self)'后的语法错误 有什么修复或者什么吗?提前致谢

1 个答案:

答案 0 :(得分:1)

def_init_(self):

def和函数名称之间添加一个空格。如果要指定特殊的初始化方法,请确保它们是双重下划线。

def __init__(self):

您的程序在语法上是正确的(尽管它仍然无法运行,因为calc未在全局范围内定义)