2018年3月7日 星期三

嘗試用 wx, python 實做 群益附的範例 SKCOMtester 的功能

2021 edit: 群益的 python demo 範例已經提供 tkinter 很好的實作範例了


最近嘗試用 python GUI 實做群益附的範例 SKCOMtester 的功能,一開始用tkinter來做,但都會當掉,當我按下Login 鈕後,Console 會出現一連串類似下圖的訊息




後來改用 wx  雖然還是有同樣的錯誤訊息,但確仍能繼續執行, 有人知道怎麼解決這個錯誤嗎?

初次使用python GUI, 不太孰悉,未來GUI的layout 應該要單獨一個檔,才會比較好管理,目前已能動為優先。

很多人跟我說  Event 出不來,我覺得有GUI介面的話,很高機率可以解決這個問題!

使用流程,
1. 輸入ID&PW後Login,會等個幾秒,console出現一串錯誤訊息
2. 按 Connect 登錄報價伺服器,當出現 nCode=3003後,  表示完成登錄
3. 在Quote 欄填寫代碼,按下 Request 即可取得報價,預設是取台指近月期貨(TX00)

環境:
WinXP 32 bit, Capital API 2.3.9, python 3.4.3

#demoGUI code
import wx, time
import comtypes.client as cc
cc.GetModule('C:\\SKCOM\\2.13.9\\x86\\SKCOM.dll')
import comtypes.gen.SKCOMLib as sk

#Event class
class skQ_events:
    def __init__(self):
        tc1.write('skQ event class created'+ '\n')    
    def OnConnection(self, nKind, nCode):
        tc1.write('SKQ_OnConnection, ' + 'nKind= ' +
              str(nKind) + ', nCode= ' + str(nCode) + '\n')     
    def OnNotifyQuote(self, sMarketNo, sStockIdx):
        getStock(sMarketNo, sStockIdx, ts)

class skR_events:
    def __init__(self):
        tc1.write('skR event class created' + '\n')
    def OnConnect(self, bstrUserID, bstrData):
        tc1.write(str(bstrUserID) + ", skR_OnConnect" + '\n')
    def OnComplete(self, bstrUserID):
        tc1.write('skR OnCompleted' + '\n')
    def OnSolaceReplyConnection(self, bstrUserID, nErrorCode):
        tc1.write('skR OnSolaceReplyConnection, ' + str(bstrUserID)
                  + ', nErrorCode= '+ str(nErrorCode) + '\n')

#custom functions
def getStock(nMarket, nIndex, ts):
    nCode=skQ.SKQuoteLib_GetStockByIndex(nMarket, nIndex, ts)
    tc2.write(str(ts.bstrStockNo) + ' ' +
              str(ts.nClose/10**ts.sDecimal) + '\n')

#GUI         
class Example(wx.Frame):
    def __init__(self, *args, **kw):
        super(Example, self).__init__(*args, **kw)
        self.InitUI()
        self.CreatSKCOMObject()
        self.CreatSKCOMEvent()

    def InitUI(self):
        pnl = wx.Panel(self)
        vbox = wx.BoxSizer(wx.VERTICAL)
        hbox1 = wx.BoxSizer(wx.HORIZONTAL)
        hbox2 = wx.BoxSizer(wx.HORIZONTAL)
        hbox3 = wx.BoxSizer(wx.HORIZONTAL)

        #labels
        st1 = wx.StaticText(pnl, label='ID     ')
        st2 = wx.StaticText(pnl, label='PW   ')
        st3 = wx.StaticText(pnl, label='Quote')

        #text input
        self.tcID = wx.TextCtrl(pnl, size=(80, -1))
        self.tcPW = wx.TextCtrl(pnl, size=(80, -1), style=wx.TE_PASSWORD)
        self.tcQuery = wx.TextCtrl(pnl, size=(80, -1), value="TX00")

        global tc1, tc2
        tc1 = wx.TextCtrl(pnl, style=wx.TE_MULTILINE)
        tc2 = wx.TextCtrl(pnl, style=wx.TE_MULTILINE)

        self.bt_login = wx.Button(pnl, label='1. Login')
        self.bt_connect = wx.Button(pnl, label='2. Connect')
        self.bt_request = wx.Button(pnl, label='3. Request')

        #layout
        hbox1.Add(st1, flag=wx.LEFT, border=10)
        hbox1.Add(self.tcID, flag=wx.LEFT, border=35)
        hbox1.Add(self.bt_login, flag=wx.LEFT, border=35)
        hbox1.Add(self.bt_connect, flag=wx.LEFT, border=35)

        hbox2.Add(st2, flag=wx.LEFT, border=10)
        hbox2.Add(self.tcPW, flag=wx.LEFT, border=35)

        hbox3.Add(st3, flag=wx.LEFT, border=10)
        hbox3.Add(self.tcQuery, flag=wx.LEFT, border=35)
        hbox3.Add(self.bt_request, flag=wx.LEFT, border=35)

        vbox.Add(hbox1, flag=wx.TOP, border=10)
        vbox.Add(hbox2, flag=wx.TOP, border=10)
        vbox.Add(hbox3, flag=wx.TOP, border=10)
        vbox.Add(tc1, proportion=1, flag=wx.EXPAND | wx.TOP |
                 wx.RIGHT | wx.LEFT, border=15)
        vbox.Add(tc2, proportion=1, flag=wx.EXPAND | wx.TOP |
                 wx.RIGHT | wx.LEFT, border=15)

        #Set event handlers
        self.Bind(wx.EVT_BUTTON, self.OnLogin, self.bt_login)
        self.Bind(wx.EVT_BUTTON, self.OnConnect, self.bt_connect)
        self.Bind(wx.EVT_BUTTON, self.OnRequest, self.bt_request)

        pnl.SetSizer(vbox)
        self.SetSize((400,600))
        self.SetTitle('pySKCOMtester')
        self.Centre()
        self.Show(True)

    def CreatSKCOMObject(self):
        global ts, skC, skQ, skR

        #股票物件
        ts=sk.SKSTOCK()

        #SKCOM object
        skC=cc.CreateObject(sk.SKCenterLib,interface=sk.ISKCenterLib)
        skQ=cc.CreateObject(sk.SKQuoteLib,interface=sk.ISKQuoteLib)
        skR=cc.CreateObject(sk.SKReplyLib,interface=sk.ISKReplyLib)

    def CreatSKCOMEvent(self):
        global EveQ, EveR, ConQ, ConR

        #instant of event sinks
        EveQ=skQ_events()
        EveR=skR_events()

        #make connection to event sink
        ConQ = cc.GetEvents(skQ, EveQ)        
        ConR = cc.GetEvents(skR, EveR)

    #Callback        

    def OnLogin(self, e):
        ID=self.tcID.GetValue()
        PW=self.tcPW.GetValue()
        nCode=skC.SKCenterLib_Login( ID, PW)
        tc1.write('Login, nCode= ' + str(nCode) + '\n')
        nCode=skR.SKReplyLib_ConnectByID(ID)
        tc1.write('skR Connected, nCode= ' + str(nCode) + '\n')
        e.Skip()

    def OnConnect(self, e):
        nCode=skQ.SKQuoteLib_EnterMonitor()
        tc1.write('skQ EnterMonitor, nCode= ' + str(nCode) + '\n')

    def OnRequest(self, e):
        Stocks=self.tcQuery.GetValue()
        nCode=skQ.SKQuoteLib_RequestStocks(1, Stocks)
        tc1.write('skQ RequestStocks ' + Stocks + ', nCode= ' + str(nCode[1])+ '\n')
        e.Skip()

def main():
    ex = wx.App()
    Example(None)
    ex.MainLoop()    

if __name__ == '__main__':
    main()

3 則留言:

  1. 請問一下,有使用過pyQt5嗎?

    回覆刪除
  2. 沒有耶,應該也可以吧!! 原本打算wx不行的時候換試 Qt
    你可以試試看,有成功也請多多分享一些心得呀!!

    回覆刪除