강의노트 계산기 작성
강의노트
• 조회수 2463
• 댓글 0
• 수정 1개월 전
- 윈도우 프로그램
계산기 작성
윈도우에서 제공하는 계산기를 기본으로 계산기를 만든다.
from tkinter import *
'''
s = '123-456*789/'
def f_cal(s):
for i in s:
if i.isnumeric():
if i != '*'
if s.find('*') != -1:
s = s.split('*')
else:
if s.find('-') == -1:
if s.find('*') == -1:
if s.find('/') == -1:
s1 = s.split('+')
for i in s.split('+'):
for j in i.split('-'):
if len(s1) == 1:
s1 = s.split('-')
'''
def f_s2n(s):
if s.find('.') == -1:
s = int(s)
else:
s = float(s)
return s
def f_digit(s):
ss = s[-4:]
if ss.startswith('e'):
aux = s[:10] + ss
print(aux)
else:
aux = s[:14]
print(aux)
return aux
def f_num(var):
global mem, expression, ind
s = lbl2.cget('text')
if ind:
s = str(var)
ind = False
else:
s = s + str(var)
lbl2.config(text=s)
def f_pm():
s = lbl2.cget('text')
s = f_s2n(s)
s = -1*s
s = str(s)
lbl2.config(text=s)
def f_point():
s = lbl2.cget('text')
if s.find('.') == -1:
s = s + '.'
lbl2.config(text=s)
def f_equal():
global mem, expression
s1 = lbl1.cget('text')
s2 = lbl2.cget('text')
m = f_s2n(mem)
s2 = f_s2n(s2)
match expression:
case '+':
s = m + s2
case '-':
s = m - s2
case '*':
s = m * s2
case '/':
s = m / s2
s1 = f'{s1}{s2}='
ind = False
s = str(s)
print(s)
if len(s)>14:
s = s[:14]
print(s)
lbl1.config(text=s1)
lbl2.config(text=s)
def f_plus():
global mem, expression, ind
aux = lbl2.cget('text')
mem = aux
expression = '+'
lbl1.config(text=f'{mem}+')
lbl2.config(text='')
def f_minus():
global mem, expression
aux = lbl2.cget('text')
mem = aux
expression = '-'
lbl1.config(text=f'{mem}-')
lbl2.config(text='')
def f_mul():
global mem, expression
aux = lbl2.cget('text')
mem = aux
expression = '*'
lbl1.config(text=f'{mem}*')
lbl2.config(text='')
def f_div():
global mem, expression
aux = lbl2.cget('text')
mem = aux
expression = '/'
lbl1.config(text=f'{mem}/')
lbl2.config(text='')
def f_sqrt():
import numpy as np
global mem, expression
aux = lbl2.cget('text')
s = np.sqrt(float(aux))
s = str(s)
if len(s)>14:
s = s[:14]
ss = f'sqrt({aux})='
lbl1.config(text=ss)
lbl2.config(text=s)
def f_pow():
import numpy as np
global mem, expression
aux = lbl2.cget('text')
s = (float(aux)**2)
s = str(s)
if len(s)>14:
s = s[:14]
ss = f'({aux})**2='
lbl1.config(text=ss)
lbl2.config(text=s)
def f_inv():
import numpy as np
global mem, expression
aux = lbl2.cget('text')
s = 1/float(aux)
s = str(s)
if len(s)>14:
s = s[:14]
ss = f'1/{aux}='
lbl1.config(text=ss)
lbl2.config(text=s)
def f_per():
import numpy as np
global mem, expression
aux = lbl2.cget('text')
s = 1/float(aux)
s = str(s)
ss = f'1/{aux}='
lbl1.config(text=ss)
lbl2.config(text=s)
def f_ce():
import numpy as np
global mem, expression
aux = lbl2.cget('text')
lbl2.config(text='')
def f_c():
import numpy as np
global mem, expression
mem = 0
expression = ''
lbl1.config(text='')
lbl2.config(text='')
def f_back():
import numpy as np
global mem, expression
s = lbl2.cget('text')
s = s[:-1]
lbl2.config(text=s)
win = Tk()
mem = ''
expression = ''
ind = False
lbl1 = Label(win,text="", font=('Arial',14), justify="right")
lbl2 = Label(win,text="", font=('Arial',24), justify="right")
btn0 = Button(win,text='0',font=('Arial',18),width=4,height=2,command=lambda:f_num(0))
btn1 = Button(win,text='1',font=('Arial',18),width=4,height=2,command=lambda:f_num(1))
btn2 = Button(win,text='2',font=('Arial',18),width=4,height=2,command=lambda:f_num(2))
btn3 = Button(win,text='3',font=('Arial',18),width=4,height=2,command=lambda:f_num(3))
btn4 = Button(win,text='4',font=('Arial',18),width=4,height=2,command=lambda:f_num(4))
btn5 = Button(win,text='5',font=('Arial',18),width=4,height=2,command=lambda:f_num(5))
btn6 = Button(win,text='6',font=('Arial',18),width=4,height=2,command=lambda:f_num(6))
btn7 = Button(win,text='7',font=('Arial',18),width=4,height=2,command=lambda:f_num(7))
btn8 = Button(win,text='8',font=('Arial',18),width=4,height=2,command=lambda:f_num(8))
btn9 = Button(win,text='9',font=('Arial',18),width=4,height=2,command=lambda:f_num(9))
btnpm = Button(win,text='+/-',font=('Arial',18),width=4,height=2,command=f_pm)
btnpoint = Button(win,text='.',font=('Arial',18),width=4,height=2,command=f_point)
btnequal = Button(win,text='=',font=('Arial',18),width=4,height=2,command=f_equal)
btnplus = Button(win,text='+',font=('Arial',18),width=4,height=2,command=f_plus)
btnminus = Button(win,text='-',font=('Arial',18),width=4,height=2,command=f_minus)
btnmul = Button(win,text='X',font=('Arial',18),width=4,height=2,command=f_mul)
btndiv = Button(win,text='/',font=('Arial',18),width=4,height=2,command=f_div)
btnsqrt = Button(win,text='sqrt',font=('Arial',18),width=4,height=2,command=f_sqrt)
btnpow = Button(win,text='pow',font=('Arial',18),width=4,height=2,command=f_pow)
btninv = Button(win,text='1/x',font=('Arial',18),width=4,height=2,command=f_inv)
btnper = Button(win,text='%',font=('Arial',18),width=4,height=2,command=f_per)
btnce = Button(win,text='CE',font=('Arial',18),width=4,height=2,command=f_ce)
btnc = Button(win,text='C',font=('Arial',18),width=4,height=2,command=f_c)
btnback = Button(win,text='<=',font=('Arial',18),width=4,height=2,command=f_back)
lbl1.grid(row=0,columnspan=16,sticky='e')
lbl2.grid(row=1,columnspan=16,sticky='e')
btn0.grid(row=7,column=1)
btn1.grid(row=6,column=0)
btn2.grid(row=6,column=1)
btn3.grid(row=6,column=2)
btn4.grid(row=5,column=0)
btn5.grid(row=5,column=1)
btn6.grid(row=5,column=2)
btn7.grid(row=4,column=0)
btn8.grid(row=4,column=1)
btn9.grid(row=4,column=2)
btnpm.grid(row=7,column=0)
btnpoint.grid(row=7,column=2)
btnequal.grid(row=7,column=3)
btnplus.grid(row=6,column=3)
btnminus.grid(row=5,column=3)
btnmul.grid(row=4,column=3)
btndiv.grid(row=3,column=3)
btnsqrt.grid(row=3,column=2)
btnpow.grid(row=3,column=1)
btninv.grid(row=3,column=0)
btnper.grid(row=2,column=0)
btnce.grid(row=2,column=1)
btnc.grid(row=2,column=2)
btnback.grid(row=2,column=3)
win.mainloop()
로그인 하면 댓글을 쓸 수 있습니다.