티스토리 뷰

누리모

3% 계산기, 파이선

Mike Sierra 2025. 2. 23. 00:24

주식 투자에서 3%를 기준으로 단타를 때리는 '놀이'를 하고 있다. 이건 버튼 누르기 놀이일 수 밖에 없는게 타이밍/ 종목 잘못타면 3%고 뭐고 파사삭~ 날라가기 때문이다.  그러니까 주요 투자는 잘 박아두고, 날아가도 술 한잔 거하게 한 셈치면 될 투기용 머니(?)가지고 난 이 게임을 한다. 

타이밍이 중요한 이 게임을 위해 파이선으로 계산기를 만들었다. 초간단하게 가격 넣으면 3% 위 아래로 계산해주는 윈도우 앱.

바로 이 친구~

코드는 따로짤 것도 없이 제미나이에게 이렇게 해줘~ 하니 짜줬다.

import tkinter as tk
from tkinter import ttk  # For modern widgets

def calculate_prices():
    try:
        current_price = float(entry_price.get())

        if current_price <= 0:
            raise ValueError("Price must be a positive number.")

        increase_price = current_price * 1.03  # 3% increase
        decrease_price = current_price * 0.97  # 3% decrease

        label_increase.config(text=f"3% Higher: {increase_price:.2f}")
        label_decrease.config(text=f"3% Lower: {decrease_price:.2f}")

    except ValueError as e:
        label_increase.config(text=f"Error: {e}")  # Display error message
        label_decrease.config(text="") # Clear the other label


# Create the main window
window = tk.Tk()
window.title("Stock Price Calculator")

# Price Input
label_current = ttk.Label(window, text="Current Price:")  # Use ttk Label
label_current.grid(row=0, column=0, padx=5, pady=5, sticky="w") # Sticky aligns to west

entry_price = ttk.Entry(window)  # Use ttk Entry
entry_price.grid(row=0, column=1, padx=5, pady=5)

# Calculate Button
button_calculate = ttk.Button(window, text="Calculate", command=calculate_prices)  # Use ttk Button
button_calculate.grid(row=1, column=0, columnspan=2, pady=(10, 5))  # Span across columns

# Output Labels
label_increase = ttk.Label(window, text="3% Higher:")  # Use ttk Label
label_increase.grid(row=2, column=0, columnspan=2, padx=5, pady=(5,0), sticky="w")

label_decrease = ttk.Label(window, text="3% Lower:")  # Use ttk Label
label_decrease.grid(row=3, column=0, columnspan=2, padx=5, pady=(0,5), sticky="w")


window.mainloop()

비주얼 스튜디오 코드에 오려붙여 넣고 가지고 노는 중이다. 뭐... 더 더해볼까? 주식 티커로 현재 가격을 읽어와서 자동으로 계산해주는 앱?

댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
«   2025/02   »
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28
글 보관함