파이썬_실전 프로젝트

객체지향 코드 - 계산기 step 2

함수까지 구현된 계산기 코드의 객체지향 코드입니다.

# Step-2 => OOP rewriting

from tkinter import *
from tkinter import ttk

class Calculator:
    
    # 연산자 값 저장.
    operation = ''
    # 연산자 앞에 입력한수 임시저장.
    temp_number = 0  
    #결과출력 상태 저장.
    answer_trigger = False
    
    def button_pressed(self, value):
        #함수형 코드에서는 함수 바깥의 변수를 global로 재선언해야했으나,
        # 객체형 코드에서는 self. 
        if value=='AC':
            self.number_entry.delete(0,'end')
            self.operation = ''
            self.answer_trigger = False
            print("AC pressed")
        else:
            #숫자를 눌렀을때 결과출력상태가 TRUE이면, 지우고 새로 입력.
            if self.answer_trigger:
                self.number_entry.delete(0,"end")
                self.answer_trigger = False
            self.number_entry.insert("end",value)
            print(value,"pressed")
    
    # 연산결과 float형 처리.
    def float_filter(self, value):
        try:
            int(value)
            return int(value)
        except ValueError:
            return float(value)
    
    # 소수점이하 없을때는 정수형으로.
    def int_changer(self, value):
        if int(value) == float(value):
            return int(value)
        else:
            return float(value)

    
    def math_button_pressed(self, value):
        if not self.number_entry.get() == '':
            self.operation = value
            self.temp_number = self.float_filter(self.number_entry.get())
            self.number_entry.delete(0,'end')
            print(self.temp_number,self.operation)

    def equal_button_pressed(self):
        if not (self.operation =='' and self.number_entry.get()==''):
            number = int(self.number_entry.get())
            if self.operation == '/':
                solution = self.temp_number/number
            elif self.operation == '*':
                solution = self.temp_number*number
            elif self.operation == '+':
                solution = self.temp_number+number
            else :
                solution = self.temp_number-number

            solution = self.int_changer(solution)
            self.number_entry.delete(0,'end')
            self.number_entry.insert(0,solution)
            print(self.temp_number,self.operation,number,"=",solution)
            self.operation = ''
            self.temp_number = 0
            #계산후 결과출력 상태를 True 로 변경.
            self.answer_trigger = True
        
    def __init__(self, root):
        root.title("Calculator")
        root.geometry("300x200")

        self.entry_value = StringVar(root, value='')

        self.number_entry = ttk.Entry(root, textvariable = self.entry_value, width=20)
        self.number_entry.grid(row=0, columnspan=3) 


        self.button7 = ttk.Button(root, text="7", command = lambda:self.button_pressed('7'))
        self.button7.grid(row=1, column=0)
        self.button8 = ttk.Button(root, text="8", command = lambda:self.button_pressed('8'))
        self.button8.grid(row=1, column=1)
        self.button9 = ttk.Button(root, text="9", command = lambda:self.button_pressed('9'))
        self.button9.grid(row=1, column=2)

        self.button_div = ttk.Button(root, text="/", command = lambda:self.math_button_pressed('/'))
        self.button_div.grid(row=1, column=3)

        self.button4 = ttk.Button(root, text="4", command = lambda:self.button_pressed('4'))
        self.button4.grid(row=2, column=0)
        self.button5 = ttk.Button(root, text="5", command = lambda:self.button_pressed('5'))
        self.button5.grid(row=2, column=1)
        self.button6 = ttk.Button(root, text="6", command = lambda:self.button_pressed('6'))
        self.button6.grid(row=2, column=2)

        self.button_mult = ttk.Button(root, text="*", command = lambda:self.math_button_pressed('*'))
        self.button_mult.grid(row=2, column=3)

        self.button1 = ttk.Button(root, text="1", command = lambda:self.button_pressed('1'))
        self.button1.grid(row=3, column=0)
        self.button2 = ttk.Button(root, text="2", command = lambda:self.button_pressed('2'))
        self.button2.grid(row=3, column=1)
        self.button3 = ttk.Button(root, text="3", command = lambda:self.button_pressed('3'))
        self.button3.grid(row=3, column=2)

        self.button_add = ttk.Button(root, text="+", command = lambda:self.math_button_pressed('+'))
        self.button_add.grid(row=3, column=3)


        self.button_ac = ttk.Button(root, text="AC", command = lambda:self.button_pressed('AC'))
        self.button_ac.grid(row=4, column=0)
        self.button0 = ttk.Button(root, text="0", command = lambda:self.button_pressed('0'))
        self.button0.grid(row=4, column=1)
        self.button_equal = ttk.Button(root, text="=", command = lambda:self.equal_button_pressed())
        self.button_equal.grid(row=4, column=2)
        self.button_sub = ttk.Button(root, text="-", command = lambda:self.math_button_pressed('-'))
        self.button_sub.grid(row=4, column=3)

    root = Tk()
    calc = Calculator(root)
    root.mainloop()

 

댓글

댓글 본문
작성자
비밀번호
graphittie 자세히 보기