Search code examples
pythonlistboxwxpythonweb-crawler

WxPython using Listbox and other UserInput with a Button


I am trying to create a web crawler based on specific user input. For example, the User Input I am trying to receive is from a ListBox and a text field. Once I have that information, I would like the user to click a button to start the search with the information collected.

This is where I have been getting problems. The EVT function doesn't recognize the listbox since its been linked to the Button evt. Is there a way to solve the problem? Can EVT information be shared with other EVTs?

Here is what I have so far:

import wx  # for gui


class MyFrame(wx.Frame):
    def __init__(self, parent, id):
        wx.Frame.__init__(self, parent, id, 'Title', size=(300,200))
        tournLevel = ['$10,000', '$15,000', '$20,000', '$50,000','$75,000','$100,000']
        levelBox = wx.ListBox(panel, -1, (40, 50), (90, 90), tournLevel)
        levelBox.SetSelection(1)  # default selection
        checkButton = wx.Button(panel, label= "Check Now", pos = (150, 50), size = (90, 40))
        self.Bind(wx.EVT_BUTTON, self.OnClick, checkButton)

    def OnClick(self, event):
        currLevel = event.GetSelection()
        print(currLevel) # to test if GetSelection is working


if __name__ == '__main__':
    app = wx.App()
    frame = MyFrame(parent=None, id=-1)
    frame.Show()
    app.MainLoop()

I would be very happy if I could just get the button to recognize the ListBox results. Thank you for your time!


Solution

  • You can just grab it from the listbox, you don't need it from the event. See below:

    import wx  # for gui
    
    
    class MyFrame(wx.Frame):
        def __init__(self, parent, id):
            wx.Frame.__init__(self, parent, id, 'Title', size=(300,200))
            tournLevel = ['$10,000', '$15,000', '$20,000', '$50,000','$75,000','$100,000']
            self.levelBox = wx.ListBox(panel, -1, (40, 50), (90, 90), tournLevel)
            self.levelBox.SetSelection(1)  # default selection
            self.checkButton = wx.Button(panel, label= "Check Now", pos = (150, 50), size = (90, 40))
            self.Bind(wx.EVT_BUTTON, self.OnClick, self.checkButton)
    
        def OnClick(self, event):
            currLevel = self.levelBox.GetSelection()
            print(currLevel) # to test if GetSelection is working
    
    
    if __name__ == '__main__':
        app = wx.App()
        frame = MyFrame(parent=None, id=-1)
        frame.Show()
        app.MainLoop()
    

    More specifically, if you store levelBox as self.levelBox, it will be accessible inside the OnClick method as a MyFrame attribute. You can then use the GetSelection method for this object (not the event), which will get the current selection.