rs6000

stock_calculator

Nov 29th, 2020
1,649
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 9.11 KB | None | 0 0
  1. import tkinter as tk
  2. from tkinter import ttk
  3. from tkinter import messagebox
  4. import csv
  5.  
  6. # 1128 treeview
  7. # 新增O 修改(不完整) 刪除O 存讀檔O
  8. # 1129 剩 介面美化 跟 主畫面做連結
  9. # 20201127 新增股價計算機
  10. # form 2 新的視窗頁面
  11.  
  12. root = tk.Tk()
  13. root.title("股價報酬計算機")
  14. root.geometry("520x340")
  15.  
  16.  
  17. def go():
  18.  
  19.     txt = '視窗左上角座標為:({},{})\n 視窗高度為:{} 視窗寬度為:{}'.format(
  20.         root.winfo_x(), root.winfo_y(),  root.winfo_height(), root.winfo_width())
  21.     lbl_label1.configure(text=txt)
  22.     root.after(1, go)
  23.  
  24.  
  25. def savefile():
  26.     with open("mystock_price.csv", "w", newline='') as myfile:
  27.         csvwriter = csv.writer(myfile, delimiter=',')
  28.  
  29.         for row_id in tree.get_children():
  30.             row = tree.item(row_id)['values']
  31.             #print('save row:', row)
  32.             csvwriter.writerow(row)
  33.  
  34.  
  35. def loadfile():
  36.     clean_treeview()
  37.     with open("mystock_price.csv") as myfile:
  38.         csvread = csv.reader(myfile, delimiter=',')
  39.  
  40.         for row in csvread:
  41.             #print('load row:', row)
  42.             tree.insert("", 'end', values=row)
  43.  
  44.  
  45. n = 1
  46.  
  47.  
  48. def summy():
  49.     global n
  50.     # 股價損益計算公式
  51.     # (賣價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)
  52.     # (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))
  53.  
  54.     symbol = txt_input_symbol.get().upper()
  55.     bidprice = txt_input_bidprice.get()
  56.     qty = txt_input_qty.get()
  57.     currentprice = txt_input_currentprice.get()
  58.  
  59.     if symbol == '' or bidprice == '' or qty == '' or currentprice == '':
  60.         messagebox.showwarning("錯誤訊息", "請輸入資料!!!")
  61.  
  62.     else:
  63.         try:
  64.             bidprice = float(bidprice)
  65.             qty = float(qty)
  66.             currentprice = float(currentprice)
  67.  
  68.             buy = float((bidprice*qty)+((bidprice*qty)*0.0025)+((bidprice*qty)
  69.                                                                 * 0.0025)*0.12+((bidprice*qty)*0.00005)+((bidprice*qty)*0.0001))
  70.             # 賣出淨額(稅後)
  71.             sell = float((currentprice*qty)-((currentprice*qty)*0.0025)-((currentprice*qty)*0.0025) *
  72.                          0.12-((currentprice*qty)*0.006)-((currentprice*qty)*0.00005)-((currentprice*qty)*0.0001))
  73.             # 利潤
  74.             net_protfit = format((sell-buy), '.2f')
  75.             # 利潤 % 數
  76.             net_protfit_percent = format(((sell-buy)/buy), '.3%')
  77.  
  78.             # 欄位:  編號 symbol 買進價格 單位 現價 利潤 利潤%數
  79.             # 只會用到 symbol 現價 其他5欄位的資料來自 輸入框 & 計算
  80.             if len(tree.get_children()) >= 10:
  81.                 messagebox.showwarning("提示訊息", "資料只能10筆以下")
  82.             else:
  83.                 i = [int(n), str(symbol), str(bidprice), str(qty), str(
  84.                     currentprice), str(net_protfit), str(net_protfit_percent)]
  85.                 tree.insert('', 'end', values=i)
  86.                 n += 1
  87.         except:
  88.             messagebox.showwarning("錯誤訊息", "請輸入正確格式!!!")
  89.  
  90.  
  91. def treeSel(event):
  92.     item = tree.selection()
  93.     itemvalues = tree.item(item, 'values')
  94.  
  95.     '''
  96.    測試用 看有幾筆資料
  97.    print(len(tree.get_children()))
  98.    測試用 看item值
  99.     print(len(itemvalues),itemvalues)
  100.    n=0
  101.    for i in itemvalues:
  102.        print('item{}={}'.format(n,i))
  103.        n+=1
  104.    '''
  105.     # 清除輸入框
  106.     clearEntry()
  107.     # 更新輸入框的值
  108.     txt_input_symbol.insert(0, itemvalues[1])
  109.     txt_input_bidprice.insert(0, itemvalues[2])
  110.     txt_input_qty.insert(0, itemvalues[3])
  111.     txt_input_currentprice.insert(0, itemvalues[4])
  112.  
  113.  
  114. def modify_column():
  115.     pass
  116.  
  117.  
  118. def del_column():
  119.     try:
  120.         selected_item = tree.selection()[0]  # get selected item
  121.         tree.delete(selected_item)
  122.     except:
  123.         messagebox.showwarning("錯誤訊息", "請點選要刪除的資料!!!")
  124.  
  125.  
  126. def clearEntry():
  127.     txt_input_symbol.delete(0, 'end')
  128.     txt_input_bidprice.delete(0, 'end')
  129.     txt_input_qty.delete(0, 'end')
  130.     txt_input_currentprice.delete(0, 'end')
  131.  
  132.     '''
  133.    #end改成引號+小寫
  134.    #https://bit.ly/36g3GXN
  135.    #python - How to clear the Entry widget after a button is pressed in Tkinter? - Stack Overflow
  136.    #原本的寫法 不會過
  137.    sidEntry.delete(0, END)
  138.  
  139.    '''
  140.  
  141.  
  142. def clean_treeview():
  143.     tree.delete(*tree.get_children())
  144.  
  145.  
  146. # ===================================================================================
  147. # 功能表
  148. filemenu = tk.Menu(root)
  149. root.config(menu=filemenu)  # 綁定主要選單
  150. menu1 = tk.Menu(filemenu)  # 創建子選單欄綁在父容器下
  151. menu2 = tk.Menu(filemenu)
  152. menu1.add_command(label='讀取股價損益名單(O)', command=loadfile)  # 新增子選單1內的項目一
  153. menu1.add_command(label='儲存股價損益名單(S)', command=savefile)  # 新增子選單1內的項目二
  154. # menu2.add_command(label='複製') #新增子選單2內的項目一
  155. # menu2.add_command(label='刪除') #新增子選單2內的項目二
  156. filemenu.add_cascade(label='檔案(F)', menu=menu1)  # 命名父選單第一欄的名稱, 並綁定子選單1所有項目
  157. # filemenu.add_cascade(label='編輯', menu=menu2) #命名父選單第二欄的名稱, 並綁定子選單2所有項目
  158. filemenu.add_cascade(label='說明(H)')  # 命名父選單第三欄的名稱
  159.  
  160. # ===================================================================================
  161. lbl_label1 = tk.Label(root)
  162. lbl_label1.grid(row=0, column=0, columnspan=8)
  163.  
  164. # ===================================================================================
  165. # 欄位:  編號 symbol 買進價格 單位 現價 利潤 利潤%數
  166. columns = ('0', '1', '2', '3', '4', '5', '6')
  167. tree = ttk.Treeview(root, show='headings', columns=columns)
  168. tree.column('0', width=22, anchor='e')
  169. tree.column('1', width=60, anchor='center')
  170. tree.column('2', width=60, anchor='center')
  171. tree.column('3', width=60, anchor='center')
  172. tree.column('4', width=80, anchor='center')
  173. tree.column('5', width=80, anchor='center')
  174. tree.column('6', width=60, anchor='center')
  175.  
  176.  
  177. # 欄位顏色設定 source: https://bit.ly/2TurTTc
  178. tree.tag_configure('+', background='red')
  179. tree.tag_configure('-', background='green')
  180.  
  181. tree.heading('0', text='#')
  182. tree.heading('1', text='Symbol')
  183. tree.heading('2', text='Bid Price')
  184. tree.heading('3', text='QTY')
  185. tree.heading('4', text='Current Price')
  186. tree.heading('5', text='Net Protfit')
  187. tree.heading('6', text='%')
  188. tree.grid(row=1, column=0, columnspan=8)
  189. # ===================================================================================
  190.  
  191. lbl_symbol = tk.Label(root, text="Symbol:", fg="green")
  192. lbl_symbol.grid(row=2, column=0)
  193. txt_input_symbol = tk.Entry(root, width=10)
  194. txt_input_symbol.grid(row=2, column=1)
  195.  
  196. lbl_bidprice = tk.Label(root, text="買進價格:", fg="green")
  197. lbl_bidprice.grid(row=2, column=2)
  198. txt_input_bidprice = tk.Entry(root, width=10)
  199. txt_input_bidprice.grid(row=2, column=3)
  200.  
  201. lbl_qty = tk.Label(root, text="單位:", fg="green")
  202. lbl_qty.grid(row=2, column=4)
  203. txt_input_qty = tk.Entry(root, width=10)
  204. txt_input_qty.grid(row=2, column=5)
  205.  
  206. lbl_currentprice = tk.Label(root, text="賣出價格:", fg="red")
  207. lbl_currentprice.grid(row=2, column=6)
  208. txt_input_currentprice = tk.Entry(root, width=10)
  209. txt_input_currentprice.grid(row=2, column=7)
  210.  
  211.  
  212. # ===================================================================================
  213.  
  214.  
  215. btn_confirm = tk.Button(root, text='新增數據', command=summy)
  216. btn_confirm.grid(row=3, column=0, columnspan=1)
  217.  
  218. btn_confirm = tk.Button(root, text='修改數據', command=modify_column)
  219. btn_confirm.grid(row=3, column=1, columnspan=2)
  220.  
  221.  
  222. btn_confirm = tk.Button(root, text='刪除數據', command=del_column)
  223. btn_confirm.grid(row=3, column=2, columnspan=3)
  224.  
  225. btn_save = tk.Button(root, text='儲存數據', command=savefile)
  226. btn_save.grid(row=3, column=3, columnspan=4)
  227.  
  228. btn_save = tk.Button(root, text='載入數據', command=loadfile)
  229. btn_save.grid(row=3, column=4, columnspan=5)
  230.  
  231. # ===================================================================================
  232. # 測試用資料
  233.  
  234. tree.insert('', 'end', values=['1', 'BDO', '101.0',
  235.                                '1000', '105.0', '2762.30', '2.727%'])
  236. tree.insert('', 'end', values=['2', 'BPI', '0', '0', '0', '0', '0'])
  237. tree.insert('', 'end', values=['3', 'SMC', '0', '0', '0', '0', '0'])
  238. tree.insert('', 'end', values=['4', 'SM', '0', '0', '0', '0', '0'])
  239. tree.insert('', 'end', values=['5', 'ALI', '0', '0', '0', '0', '0'])
  240.  
  241. # ===================================================================================
  242.  
  243. tree.bind('<<TreeviewSelect>>', treeSel)
  244. # ===================================================================================
  245.  
  246. go()
  247. root.mainloop()
Advertisement
Comments
  • min98583
    3 years
    # Python 0.37 KB | 0 0
    1. 你好 我是一個很初學的人但如果我已經學玩了一些基礎的迴圈遞減那些後,我應該要往哪個方向學習才可以像你一樣可以編輯一個自己的程式,因為我之後可能也想做一些數據分析的程式,不知道你能否給我一些建議呢?因為我在這裡無法私訊你,不知道還有沒有其他方式能跟你聯繫
Add Comment
Please, Sign In to add comment