계산기 만들기
import tkinter as tk
operation = '' #연산자 저장 변수
temp_number = 0 #이전값 저장 변수
def button(val):
if val == 'C':
number.delete(0, 'end')
else:
number.insert('end', val)
print(val)
def cal(val):
global operation
global temp_number
if number.get() != '':
operation = val
temp_number = int(number.get())
number.delete(0, 'end')
print(temp_number, operation)
def equl():
global operation
global temp_number
if operation != '' and number.get() !='':
num = int(number.get())
if operation == '+':
solution = temp_number+num
elif operation == '-':
solution = temp_number - num
elif operation == '*':
solution = temp_number * num
else:
solution = temp_number/num
number.delete(0,'end')
number.insert(0,solution)
print(temp_number,operation,num,'=',solution)
operation =''
temp_number = 0
win = tk.Tk()
win.title('Calculator')
win.geometry('300x400+100+100')
win.resizable(False, False)
label = tk.Label(win, height=4)
label.grid(row=0, column=0)
entry_value = StringVar(win, value='')
number = ttk.Entry(win, textvariable = entry_value, width=20) #숫자 및 결과 표시창
number.grid(row=0, columnspan=3)
b1 = tk.Button(text='1', width=7, height=4, command=lambda:button('1'))
b1.grid(row=1, column=0)
b2 = tk.Button(text='2', width=7, height=4, command=lambda:button('2'))
b2.grid(row=1, column=1)
b3 = tk.Button(text='3', width=7, height=4, command=lambda:button('3'))
b3.grid(row=1, column=2)
b4 = tk.Button(text='+', width=7, height=4, command=lambda:cal('+'))
b4.grid(row=1, column=3)
b5 = tk.Button(text='4', width=7, height=4, command=lambda:button('4'))
b5.grid(row=2, column=0)
b6 = tk.Button(text='5', width=7, height=4, command=lambda:button('5'))
b6.grid(row=2, column=1)
b7 = tk.Button(text='6', width=7, height=4, command=lambda:button('6'))
b7.grid(row=2, column=2)
b8 = tk.Button(text='-', width=7, height=4, command=lambda:cal('-'))
b8.grid(row=2, column=3)
b9 = tk.Button(text='7', width=7, height=4, command=lambda:button('7'))
b9.grid(row=3, column=0)
b10 = tk.Button(text='8', width=7, height=4, command=lambda:button('8'))
b10.grid(row=3, column=1)
b11 = tk.Button(text='9', width=7, height=4, command=lambda:button('9'))
b11.grid(row=3, column=2)
b12 = tk.Button(text='*', width=7, height=4, command=lambda:cal('*'))
b12.grid(row=3, column=3)
b13 = tk.Button(text='0', width=7, height=4, command=lambda:button('0'))
b13.grid(row=4, column=0)
b14 = tk.Button(text='C', width=7, height=4, command=lambda:f1('C'))
b14.grid(row=4, column=1)
b15 = tk.Button(text='=', width=7, height=4, command=lambda:equl())
b15.grid(row=4, column=2)
b16 = tk.Button(text='/', width=7, height=4, command=lambda:cal('/'))
b16.grid(row=4, column=3)
win.mainloop()
아직 계산 오류(소숫점 등)를 고려하지 않은 상태이다.
앗, eval 함수를 쓰면 cal, equl 함수 없이도 되는구나..
def f1(val):
s = label.cget('text')
s += val #s:12
label.config(text=s)#config():위젯 속성의 값을 읽거나 설정
def calc():
s = label.cget('text') #s:'5-1+6*2'
print(s)
res = eval(s)
label.config(text=str(res))
def clear():
label.config(text='')
win = tk.Tk()#Tk객체 생성=>기본 윈도우와 ui api를 제공하는 객체
win.title('window title')
win.geometry('300x400+100+100')
win.resizable(False, False)
#위젯 생성 및 배치
label = tk.Label(win, height=4)
label.grid(row=0, column=0)
b1 = tk.Button(text='1', width=7, height=4, command=lambda:f1('1'))
b1.grid(row=1, column=0)
b2 = tk.Button(text='2', width=7, height=4, command=lambda:f1('2'))
b2.grid(row=1, column=1)
b3 = tk.Button(text='3', width=7, height=4, command=lambda:f1('3'))
b3.grid(row=1, column=2)
b4 = tk.Button(text='+', width=7, height=4, command=lambda:f1('+'))
b4.grid(row=1, column=3)
b5 = tk.Button(text='4', width=7, height=4, command=lambda:f1('4'))
b5.grid(row=2, column=0)
b6 = tk.Button(text='5', width=7, height=4, command=lambda:f1('5'))
b6.grid(row=2, column=1)
b7 = tk.Button(text='6', width=7, height=4, command=lambda:f1('6'))
b7.grid(row=2, column=2)
b8 = tk.Button(text='-', width=7, height=4, command=lambda:f1('-'))
b8.grid(row=2, column=3)
b9 = tk.Button(text='7', width=7, height=4, command=lambda:f1('7'))
b9.grid(row=3, column=0)
b10 = tk.Button(text='8', width=7, height=4, command=lambda:f1('8'))
b10.grid(row=3, column=1)
b11 = tk.Button(text='9', width=7, height=4, command=lambda:f1('9'))
b11.grid(row=3, column=2)
b12 = tk.Button(text='*', width=7, height=4, command=lambda:f1('*'))
b12.grid(row=3, column=3)
b13 = tk.Button(text='0', width=7, height=4, command=lambda:f1('0'))
b13.grid(row=4, column=0)
b14 = tk.Button(text='C', width=7, height=4, command=clear)
b14.grid(row=4, column=1)
b15 = tk.Button(text='=', width=7, height=4, command=calc)
b15.grid(row=4, column=2)
b16 = tk.Button(text='/', width=7, height=4, command=lambda:f1('/'))
b16.grid(row=4, column=3)
win.mainloop()
심지어 버튼 구현 노가다 없이 for문으로도 가넝...
def f1(val):
s = label.cget('text')
s += val
label.config(text=s) #config():위젯 속성의 값을 읽거나 설정
def calc():
s = label.cget('text')
print(s)
res = eval(s) #eval 함수: 자동으로 계산해주는 함수
label.config(text=str(res))
def clear():
label.config(text='')
win = tk.Tk()
win.title('window title')
win.geometry('300x400+100+100')
win.resizable(False, False)
label = tk.Label(win, height=4)
label.grid(row=0, column=0)
nums = [['1','2','3','+'],['4','5','6','-'],['7','8','9','*'],['0','C','=','/']]
for i in range(0, 4):
for j in range(0, 4):
num = nums[i][j]
if num =='C':
b = tk.Button(text=num, width=7, height=4, command=clear)
elif num == '=':
b = tk.Button(text=num, width=7, height=4, command=calc)
else:
b = tk.Button(text=num, width=7, height=4, command=lambda val=num :f1(val))
b.grid(row=(i+1), column=j)
win.mainloop()
'파이썬II' 카테고리의 다른 글
파이썬II #Web Data Read (0) | 2021.07.02 |
---|---|
파이썬II # 공공데이터 분석(1) (0) | 2021.07.01 |
파이썬II #파일 open 후 데이터 추출하기 (0) | 2021.06.30 |