๐Ÿ‘ฆ ๋‚ด์ผ๋ฐฐ์›€์บ ํ”„/์‚ฌ์ „๊ณผ์ œ

[๋‚ด์ผ๋ฐฐ์›€์บ ํ”„] ์‚ฌ์ „๊ณผ์ œ - ๊ณ„์‚ฐ๊ธฐ ๋งŒ๋“ค๊ธฐ

  • -

github : https://github.com/DHL68/github-DHL68/tree/main/22_04_07_calculator

 

GitHub - DHL68/github-DHL68: github-DHL68-practice

github-DHL68-practice. Contribute to DHL68/github-DHL68 development by creating an account on GitHub.

github.com

 

GUI ๊ณ„์‚ฐ๊ธฐ ๋งŒ๋“ค๊ธฐ ๊ฐ•์˜ ๋‚ด์šฉ

import tkinter as tk


# ๋ช…๋ น์–ด ๋ณ€์ˆ˜
operator = {'+':1, '-':2, '/':3,
            '*':4, 'C':5, '=':6}
disValue = 0 # ๊ฒฐ๊ณผ๊ฐ’ ๋ณ€์ˆ˜ / ์ •์ˆ˜
stoValue = 0 # ์ €์žฅ๋œ ๊ฐ’
opPre = 0 # ์ด์ „ ๋ช…๋ น์–ด

# ์ˆซ์ž ํด๋ฆญ
def number_click(value):
    # print('์ˆซ์ž', value)
    global disValue
    disValue = (disValue * 10) + value
    str_value.set(disValue)

# ๋ฆฌ์…‹
def clear():
    global disValue, stoValue, opPre
    stoValue = 0
    opPre = 0
    disValue = 0
    str_value.set(disValue)

# ํ•ด๋‹น ๋ช…๋ น ์กฐ๊ฑด
def operator_click(value):
    # print('๋ช…๋ น', value)
    global disValue, operator, stoValue, opPre
    op = operator[value]
    if op == 5: # C
        clear()
    elif disValue == 0:
        opPre = 0
    elif opPre == 0:
        opPre = op
        stoValue = disValue
        disValue = 0
        str_value.set(disValue)
    elif op == 6: # =
        if opPre == 1:
            disValue = stoValue + disValue
        if opPre == 2:
            disValue = stoValue - disValue
        if opPre == 3:
            disValue = stoValue / disValue
        if opPre == 4:
            disValue = stoValue * disValue

        str_value.set(str(disValue))
        disValue = 0
        stoValue = 0
        opPre = 0
    else:
        clear()

    

# ๋ฒ„ํŠผ
def button_click(value):
    # print(value)
    try:
        value = int(value)
        number_click(value)
    except:
        operator_click(value)

# ๊ณ„์‚ฐ๊ธฐ ํผ
win = tk.Tk() # ํด๋ž˜์Šค ์ƒ์„ฑ
win.title('๊ณ„์‚ฐ๊ธฐ') # ํƒ€์ดํ‹€ ์ž…๋ ฅ

str_value = tk.StringVar()
str_value.set(str(disValue)) # disValue์˜ ์ •์ˆ˜๋ฅผ str์„ ํ†ตํ•ด ๋ฌธ์ž์—ด๋กœ ๋ณ€ํ™˜ / .set ์œผ๋กœ str_value ์— ์ž…๋ ฅ
dis = tk.Entry(win, textvariable = str_value, justify = 'right', bg = 'white', fg = 'red') # [๊ณ„์‚ฐ๊ธฐ ์ž…๋ ฅ ํ‘œ์‹œ ์˜์—ญ] textvariable ๊ฐ’์„ ์ž๋™์œผ๋กœ ๊ฐฑ์‹  / justify : ๋ฐฐ์น˜
dis.grid(column = 0, row = 0, columnspan = 4, ipadx = 80, ipady = 30) # columnspan : colum์˜ span ์œ„์น˜ ์กฐ์ • / ipad x,y input ์ฐฝ์˜ ํฌ๊ธฐ ์กฐ์ •


calItem = [['1', '2', '3', '4'],
           ['5', '6', '7', '8'],
           ['9', '0', '+', '-'],
           ['/', '*', 'C', '=']]

for i, items in enumerate(calItem):
    for k, item in enumerate(items):
        try:
            color = int(item) # ์ •์ˆ˜ ์—๋Ÿฌ๋ฅผ ์ด์šฉํ•ด์„œ ์ •์ˆ˜์ผ ๊ฒฝ์šฐ ๊ฒ€์€์ƒ‰, ์—๋Ÿฌ๊ฐ€ ๋‚  ๊ฒฝ์šฐ ์ดˆ๋ก์ƒ‰
            color = 'black'
        except:
            color = 'green'

        # [๊ณ„์‚ฐ๊ธฐ ์ˆซ์ž ๋ฒ„ํŠผ ์ž…๋ ฅ ์˜์—ญ]
        bt = tk.Button( # ๋ณ€์ˆ˜ ์„ ์–ธ
            win,
            text = item, # item ์„ ์–ธ
            width = 10, # ๊ฐ€๋กœ
            height = 5, # ์„ธ๋กœ
            bg = color, # ๋ฐฐ๊ฒฝ์ƒ‰
            fg = 'white', # ๊ธ€์ž์ƒ‰
            command = lambda cmd = item: button_click(cmd) # item ์„ cmd์— ๋ฐ›์•„ ๋ฒ„ํŠผ ํด๋ฆญ ์‹œ cmd์„ ๋ฒ„ํŠผ ๊ธฐ๋Šฅ์— ์ž…๋ ฅ
            )
        bt.grid(column = k, row = i + 1) # ๋ฐ˜๋ณต๊ตฌ๋ฌธ์„ ํ†ตํ•ด ๋ฆฌ์ŠคํŠธ์˜ ์ˆœ์„œ๋ฅผ ๋ฐ˜๋ณต ํ•ฉ์‚ฐํ•˜์—ฌ ์œ„์น˜ ์ง€์ •

win.mainloop() # ๊ณ„์‚ฐ๊ธฐ ๋ชจ๋“ˆ ์‹คํ–‰

 

์ถœ์ฒ˜ : https://youtu.be/rqif36EDSOI

Contents

ํฌ์ŠคํŒ… ์ฃผ์†Œ๋ฅผ ๋ณต์‚ฌํ–ˆ์Šต๋‹ˆ๋‹ค

์ด ๊ธ€์ด ๋„์›€์ด ๋˜์—ˆ๋‹ค๋ฉด ๊ณต๊ฐ ๋ถ€ํƒ๋“œ๋ฆฝ๋‹ˆ๋‹ค.