Search code examples
wxpython

Assistance Text For ComboBox Entries


One of my screens has a ComboBox where the 'type of sport' a client (mainly professional sports people) is involved in can be selected. It has been done this way to avoid an overly long list of all possible sports. One example is 'Board sports' that relates to skateboarding, skiboarding, surfing, etc. A number off users of the software have asked if HelpText can be displayed if they hover over an option, e.g. if they hovered over 'Board sports' the sports mentioned would be displayed. After an intensive couple of hours Googling I cannot see how to do this. Is it possible and, if so, how?


Solution

  • Here is "a" solution, select and then hover:

    import wx
    
    class Myframe(wx.Frame):
        def __init__(self):
            wx.Frame.__init__(self, None)
            self.my_choices = ["Board Sports","Ball Sports","Fight Sports"]
            self.my_subchoices = [["Skate boarding","Surfing","Ski boarding"],["Football","Cricket","Rugby","Guess my nationality"],["Boxing","Wrestling","Karate"]]
            pan = wx.Panel(self)
            self.cbx = wx.ComboBox(pan, -1, pos=(10,30), choices=self.my_choices,style=wx.CB_DROPDOWN )
            self.cbx.Bind(wx.EVT_COMBOBOX, self.on_selection)
            self.cbx.SetValue("Choose a sport")
    
        def on_selection(self, evt):
            Choice = self.cbx.GetSelection()
            msg = ""
            for x in self.my_subchoices[Choice]:
                msg += x+"\n"
            self.cbx.SetToolTipString(msg)
    
    if __name__ == "__main__":  
        App = wx.App()
        Myframe().Show()
        App.MainLoop()