Search code examples
pythonuser-interfacecomboboxwxpythonwxformbuilder

GetSelection method for combobox doesn't work in wxPython


I'm trying to make a GUI for youtube-dl

I used wxFormBuilder to make the GUI layout and then went in to code in another script by importing the code generated by wxFormBuilder. the GUI works right but I cant get the index of the values I select from the drop down menu.

This is the section of the code that defines the properties of the combobox

quality_selection_drop_downChoices = [ u"720p", u"Best Quality Available", u"Audio (mp3)", u"Non Youtube" ]
self.quality_selection_drop_down = wx.ComboBox( self, wx.ID_ANY, wx.EmptyString, wx.DefaultPosition, wx.Size( -1,-1 ), quality_selection_drop_downChoices, 0 )
self.quality_selection_drop_down.SetSelection( 4 )
gbSizer1.Add( self.quality_selection_drop_down, wx.GBPosition( 2, 1 ), wx.GBSpan( 1, 3 ), wx.ALL|wx.EXPAND, 5 )

This is the code I wrote to check if the combobox values are returned correctly

def video_dl(self, event):
    print(self.quality_selection_drop_down.GetSelection)

The video_dl command is set as an event for a button in the GUI. It returns

<built-in method GetSelection of ComboBox object at 0x0000017820621670>

Instead of giving the index of the selection I'm choosing. I tried it with GetSelection, GetCurrentSelection, GetValue, GetString, GetStringSelection. All of them return an output in the same vein as the one mentioned above. The entire code is on github so you can take a look at the whole code : github repo

Any help is greatly appreciated!! Thank you!


Solution

  • The errror is that when you get the selection you do this:

    self.quality_selection_drop_down.GetSelection
    

    instead of this:

    self.quality_selection_drop_down.GetSelection() #returns the index of the combobox
    self.quality_selection_drop_down.GetStringSelection() #returns the string associated to the combobox
    

    remember that when you call a method or a function you have to call it with opening and closing brackets like foo(). Calling it without brackets you call its __repr__() method, that gives you general info about the method or the object which it belongs to