[Tutor] Segmentation Fault shenanigans with wx Gui

Marco Mistroni mmistroni at gmail.com
Sat Oct 20 00:05:58 CEST 2012


Hi all
 i have written a wx GUI which downloads json data from a server, and
populate a listbox.
Every time i populate a listbox, i am receiving Segmentation Faults.
I have tried to retrieve data from the URL via separate thread, and to use
events, but i am still getting a segmentation fault

could anyone assist pls?

here's relevant code. I m running python on Ubuntu 10.04

class WxSharesGUI(wx.Frame):


    def __init__(self, parent, id, title):
        wx.Frame.__init__(self, parent, id, title, size=(300, 300))
    self.InitUI()

    def InitUI(self):
    self.load_button = wx.Button(self, wx.ID_ANY, "load ListBox")
        self.clear_button = wx.Button(self, wx.ID_ANY, "clear ListBox")
    self.load_button.Bind(wx.EVT_BUTTON, self.load_buttonClick)
        self.clear_button.Bind(wx.EVT_BUTTON, self.clear_buttonClick)
    self.Bind(EVT_RESULT, self.OnResult)
    self.lock = threading.RLock()

    # Creating Menu bar
        menubar = wx.MenuBar()
        newsMenu = wx.Menu()
    newsMenu.Append(ID_EDGAR, 'Edgar News', 'Edgar news')
        newsMenu.Append(ID_GLOBAL, 'Global Economy', 'Global economy')
        newsMenu.Append(ID_ECONOMY, 'Economy Data', 'Economy data')
        newsMenu.Append(ID_FX, 'FX', 'Fx data')
    futuresItem = newsMenu.Append(ID_FUTURES, 'FUTURES', 'FUTURES')
        exitItem = newsMenu.Append(wx.ID_EXIT, 'Quit', 'Quit application')
        menubar.Append(newsMenu, '&News')

    mgmtMenu = wx.Menu()
    shareMgmtItem = mgmtMenu.Append(ID_UPDATE_SHARE, 'Share Management',
'Share Management')
        ptfMgmtItem = mgmtMenu.Append(ID_UPDATE_PORTFOLIO, 'Portfolio
Management', 'Portfolio Mgmt')
    resetItem = mgmtMenu.Append(ID_RESET, 'Reset Listbox', 'Portfolio Mgmt')
        menubar.Append(mgmtMenu, '&Management')


    self.SetMenuBar(menubar)

        self.Bind(wx.EVT_MENU, self.OnQuit, exitItem)
    self.Bind(wx.EVT_MENU, self.OnFutures, futuresItem)
    self.Bind(wx.EVT_MENU, self.OnShareMgmt, shareMgmtItem)
    self.Bind(wx.EVT_MENU, self.OnPtfMgmt, ptfMgmtItem)

    self.listBox1 = wx.ListBox(self, wx.ID_ANY, choices=[],
            style=wx.LB_EXTENDED)
    self.listBox1.Append('MAIN')
    self.listBox1.Append('INDEXES')
    self.listBox1.Append('CURRENCIES')
    self.listBox1.Append('HEDGE')
    self.listBox1.Append('CDS SPREADS')
        self.listBox1.Bind(wx.EVT_LISTBOX, self.OnListBox1Listbox,
    id=ID_FRAME1LISTBOX1)
    self.Bind(wx.EVT_MENU, self.OnResetList, resetItem)

    hbox = wx.BoxSizer(wx.HORIZONTAL)

        panel = wx.Panel(self, -1)

    self.list = AutoWidthListCtrl(panel)
        self.list.InsertColumn(0, 'Ticker')#, width=150)
        self.list.InsertColumn(1, 'Name')#', width=100)
    self.list.InsertColumn(2, 'MovingAvg200')#', width=100)
    self.list.InsertColumn(3, 'MovingAvg50')#', width=100)
    self.list.InsertColumn(4, 'Price')#, width=80)
    self.list.InsertColumn(5, 'Previous')#, width=80)
    self.list.InsertColumn(6, 'Change')#, width=80)
    self.list.InsertColumn(7, 'TotalValue')#, width=80)
    self.list.InsertColumn(8, 'Position')#', width=100)

    hbox.Add(self.list, 2, wx.EXPAND)
        panel.SetSizer(hbox)


        self.SetSize((900, 500))
        self.SetTitle('Camel App Python Gui')
        self.Centre()

    sizer = wx.GridBagSizer(vgap=8, hgap=10)
        # pos=(row, column)  span=(rowspan, columnspan)
        # wx.ALL puts the specified border on all sides
        sizer.Add(self.load_button, pos=(0, 0), flag=wx.ALL, border=5)
        # listbox spans 6 rows and 2 columns
        sizer.Add(self.clear_button, pos=(0, 25), flag=wx.ALL, border=5)


    sizer.Add(self.listBox1, pos=(2, 4), span=(8, 8),
            flag=wx.ALL|wx.EXPAND, border=5)

    sizer.Add(panel, pos=(10,1),span=(10, 30),
            flag=wx.ALL|wx.EXPAND, border=5)
    self.SetSizer(sizer)
    #self.Fit()

        self.Show(True)

    def OnQuit(self, e):
        self.Close()

    def OnResult(self, event):
    print 'onResult'
    ptf_data = event.data
    for item in ptf_data['portfolio']['items']:
        index = self.list.InsertStringItem(sys.maxint, item['ticker'])
        self.list.SetStringItem(index, 1, item['name'])
        self.list.SetStringItem(index, 2, str(item['movingAvg200']))
        self.list.SetStringItem(index, 3, str(item['movingAvg50']))
        self.list.SetStringItem(index, 4, str(item['price']))
        self.list.SetStringItem(index, 5, str(item['previous']))
        self.list.SetStringItem(index, 6, str(item['price'] -
item['previous']) )
        self.list.SetStringItem(index, 7, str(item['totalValue']))
        self.list.SetStringItem(index, 8, str(item['position']))



    def load_buttonClick(self,e):
    idx = self.listBox1.GetSelections()[0]

    ptf = self.listBox1.GetStrings()[idx]

    thread.start_new_thread(self.fetch, (ptf,))
    self.list.ClearAll()

    def fetch(self, ptf):
    data= {'action':'portfolio', 'portfolioName':ptf}
    ptf_data = jsonLoader.openJsonRequest(data)
    print ptf_data['portfolio']['cash']
        wx.PostEvent(self, ResultEvent(data=ptf_data))
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20121019/f5d8dff1/attachment.html>


More information about the Tutor mailing list