Advertisement
rs6000

main_form2.py

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