Advertisement
rs6000

Multiple_Form_OK

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