Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import tkinter as tk
- from tkinter import ttk
- from tkinter import messagebox
- import csv
- # 20201127 新增股價計算機
- # form 2 新的視窗頁面
- # 1128 treeview
- # 新增O 修改(不完整) 刪除O 存讀檔O
- # 1129 剩 介面美化 跟 主畫面做連結
- root = tk.Tk()
- root.title("股價報酬計算機")
- root.geometry("520x340")
- def savefile():
- with open("mystock_price.csv", "w", newline='') as myfile:
- csvwriter = csv.writer(myfile, delimiter=',')
- for row_id in tree.get_children():
- row = tree.item(row_id)['values']
- #print('save row:', row)
- csvwriter.writerow(row)
- def loadfile():
- clean_treeview()
- with open("mystock_price.csv") as myfile:
- csvread = csv.reader(myfile, delimiter=',')
- for row in csvread:
- #print('load row:', row)
- tree.insert("", 'end', values=row)
- n=1
- def summy():
- global n
- # 股價損益計算公式
- # (賣價x單位)-(((賣價x單位)*0.0025)+((賣價x單位)*0.0025)*0.12+((賣價x單位)*0.006)+((賣價x單位)*0.00005)+((賣價x單位)*0.0001)))-((買價x單位)+((買價x單位)*0.0025)+((買價x單位)*0.0025)*0.12+((買價x單位)*0.00005)+((買價x單位)*0.0001)
- # (currentprice*qty)-(((currentprice*qty)*0.0025)+((currentprice*qty)*0.0025)*0.12+((currentprice*qty)*0.006)+((currentprice*qty)*0.00005)+((currentprice*qty)*0.0001))-((bidprice*qty)+((bidprice*qty)*0.0025)+((bidprice*qty)*0.0025)*0.12+((bidprice*qty)*0.00005)+((bidprice*qty)*0.0001))
- symbol = txt_input_symbol.get().upper()
- bidprice = txt_input_bidprice.get()
- qty = txt_input_qty.get()
- currentprice = txt_input_currentprice.get()
- if symbol == '' or bidprice == '' or qty == '' or currentprice == '':
- messagebox.showwarning("錯誤訊息", "請輸入資料!!!")
- else:
- try:
- bidprice = float(bidprice)
- qty = float(qty)
- currentprice = float(currentprice)
- buy = float((bidprice*qty)+((bidprice*qty)*0.0025)+((bidprice*qty)
- * 0.0025)*0.12+((bidprice*qty)*0.00005)+((bidprice*qty)*0.0001))
- # 賣出淨額(稅後)
- sell = float((currentprice*qty)-((currentprice*qty)*0.0025)-((currentprice*qty)*0.0025) *
- 0.12-((currentprice*qty)*0.006)-((currentprice*qty)*0.00005)-((currentprice*qty)*0.0001))
- # 利潤
- net_protfit = format((sell-buy), '.2f')
- # 利潤 % 數
- net_protfit_percent = format(((sell-buy)/buy), '.3%')
- # 欄位: 編號 symbol 買進價格 單位 現價 利潤 利潤%數
- # 只會用到 symbol 現價 其他5欄位的資料來自 輸入框 & 計算
- if len(tree.get_children()) >=10:
- messagebox.showwarning("提示訊息", "資料只能10筆以下")
- else:
- i = [int(n), str(symbol), str(bidprice), str(qty), str(
- currentprice), str(net_protfit), str(net_protfit_percent)]
- tree.insert('', 'end', values=i)
- n += 1
- except:
- messagebox.showwarning("錯誤訊息", "請輸入正確格式!!!")
- def treeSel(event):
- item = tree.selection()
- itemvalues = tree.item(item, 'values')
- '''
- 測試用 看有幾筆資料
- print(len(tree.get_children()))
- 測試用 看item值
- print(len(itemvalues),itemvalues)
- n=0
- for i in itemvalues:
- print('item{}={}'.format(n,i))
- n+=1
- '''
- # 清除輸入框
- clearEntry()
- # 更新輸入框的值
- txt_input_symbol.insert(0, itemvalues[1])
- txt_input_bidprice.insert(0, itemvalues[2])
- txt_input_qty.insert(0, itemvalues[3])
- txt_input_currentprice.insert(0, itemvalues[4])
- def modify_column():
- pass
- def del_column():
- try:
- selected_item = tree.selection()[0] # get selected item
- tree.delete(selected_item)
- except:
- messagebox.showwarning("錯誤訊息", "請點選要刪除的資料!!!")
- def clearEntry():
- txt_input_symbol.delete(0, 'end')
- txt_input_bidprice.delete(0, 'end')
- txt_input_qty.delete(0, 'end')
- txt_input_currentprice.delete(0, 'end')
- '''
- #end改成引號+小寫
- #https://bit.ly/36g3GXN
- #python - How to clear the Entry widget after a button is pressed in Tkinter? - Stack Overflow
- #原本的寫法 不會過
- sidEntry.delete(0, END)
- '''
- def clean_treeview():
- tree.delete(*tree.get_children())
- # ===================================================================================
- #功能表
- filemenu = tk.Menu(root)
- root.config(menu=filemenu) # 綁定主要選單
- menu1 = tk.Menu(filemenu) # 創建子選單欄綁在父容器下
- menu2 = tk.Menu(filemenu)
- menu1.add_command(label='讀取股價損益名單(O)', command=loadfile) # 新增子選單1內的項目一
- menu1.add_command(label='儲存股價損益名單(S)', command=savefile) # 新增子選單1內的項目二
- # menu2.add_command(label='複製') #新增子選單2內的項目一
- # menu2.add_command(label='刪除') #新增子選單2內的項目二
- filemenu.add_cascade(label='檔案(F)', menu=menu1) # 命名父選單第一欄的名稱, 並綁定子選單1所有項目
- # filemenu.add_cascade(label='編輯', menu=menu2) #命名父選單第二欄的名稱, 並綁定子選單2所有項目
- filemenu.add_cascade(label='說明(H)') # 命名父選單第三欄的名稱
- # ===================================================================================
- lbl_label1 = tk.Label(root)
- lbl_label1.grid(row=0, column=0, columnspan=8)
- # ===================================================================================
- # 欄位: 編號 symbol 買進價格 單位 現價 利潤 利潤%數
- columns = ('0', '1', '2', '3', '4', '5', '6')
- tree = ttk.Treeview(root, show='headings', columns=columns)
- tree.column('0', width=22, anchor='e')
- tree.column('1', width=60, anchor='center')
- tree.column('2', width=60, anchor='center')
- tree.column('3', width=60, anchor='center')
- tree.column('4', width=80, anchor='center')
- tree.column('5', width=80, anchor='center')
- tree.column('6', width=60, anchor='center')
- # 欄位顏色設定 source: https://bit.ly/2TurTTc
- tree.tag_configure('+', background='red')
- tree.tag_configure('-', background='green')
- tree.heading('0', text='#')
- tree.heading('1', text='Symbol')
- tree.heading('2', text='Bid Price')
- tree.heading('3', text='QTY')
- tree.heading('4', text='Current Price')
- tree.heading('5', text='Net Protfit')
- tree.heading('6', text='%')
- tree.grid(row=1, column=0, columnspan=8)
- # ===================================================================================
- lbl_symbol = tk.Label(root, text="Symbol:", fg="green")
- lbl_symbol.grid(row=2, column=0)
- txt_input_symbol = tk.Entry(root, width=10)
- txt_input_symbol.grid(row=2, column=1)
- lbl_bidprice = tk.Label(root, text="買進價格:", fg="green")
- lbl_bidprice.grid(row=2, column=2)
- txt_input_bidprice = tk.Entry(root, width=10)
- txt_input_bidprice.grid(row=2, column=3)
- lbl_qty = tk.Label(root, text="單位:", fg="green")
- lbl_qty.grid(row=2, column=4)
- txt_input_qty = tk.Entry(root, width=10)
- txt_input_qty.grid(row=2, column=5)
- lbl_currentprice = tk.Label(root, text="賣出價格:", fg="red")
- lbl_currentprice.grid(row=2, column=6)
- txt_input_currentprice = tk.Entry(root, width=10)
- txt_input_currentprice.grid(row=2, column=7)
- # ===================================================================================
- btn_confirm = tk.Button(root, text='新增數據', command=summy)
- btn_confirm.grid(row=3, column=0, columnspan=1)
- btn_confirm = tk.Button(root, text='修改數據', command=modify_column)
- btn_confirm.grid(row=3, column=1, columnspan=2)
- btn_confirm = tk.Button(root, text='刪除數據', command=del_column)
- btn_confirm.grid(row=3, column=2, columnspan=3)
- btn_save = tk.Button(root, text='儲存數據', command=savefile)
- btn_save.grid(row=3, column=3, columnspan=4)
- btn_save = tk.Button(root, text='載入數據', command=loadfile)
- btn_save.grid(row=3, column=4, columnspan=5)
- # ===================================================================================
- # 測試用資料
- tree.insert('', 'end', values=['1', 'BDO', '101.0',
- '1000', '105.0', '2762.30', '2.727%'])
- tree.insert('', 'end', values=['2', 'BPI', '0', '0', '0', '0', '0'])
- tree.insert('', 'end', values=['3', 'SMC', '0', '0', '0', '0', '0'])
- tree.insert('', 'end', values=['4', 'SM', '0', '0', '0', '0', '0'])
- tree.insert('', 'end', values=['5', 'ALI', '0', '0', '0', '0', '0'])
- # ===================================================================================
- tree.bind('<<TreeviewSelect>>', treeSel)
- # ===================================================================================
- root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement