rs6000

form2

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