Search code examples
pythonpython-2.7wxpython

Resolve a problem ComboBox and a TextCtrl


I want to make that when selecting an element in the ComboBox print a message that is in the tuple in the TextCtrl, depending on the item I chose

When I did it the way I investigated, I throw an error.

import wx

#Mods
Nom_Mods = ["Alca v3", "Barcelone v1", "Anti-Fed (French)", "Elegance v3"]
Info_Mods = ["(ZL + Joystick2) To Open Menu\n(B) To close Menu\nCreate by KillerGamer81"]
#EndMods


class PageOne(wx.Panel):
    def __init__(self, parent):
    wx.Panel.__init__(self, parent)
    sz = wx.BoxSizer(wx.VERTICAL)

    #Controls
    self.Listade_Menus = wx.ComboBox(self, -1, pos=(10,80), size=(173,22), choices = Nom_Mods, style= wx.CB_READONLY)
    Cuadro_de_info = wx.TextCtrl(self, -1, "", pos=(200,80), size=(175,80), style = wx.TE_MULTILINE|wx.TE_NO_VSCROLL|wx.TE_READONLY)

class MainFrame(wx.Frame):
    def __init__(self):
        no_sys_menu = wx.DEFAULT_FRAME_STYLE & (~wx.RESIZE_BORDER) & (~wx.MAXIMIZE_BOX)
        wx.Frame.__init__(self, None, title="ProyectoU", style=no_sys_menu, size=(400,225))
        ico = wx.Icon('Recursos/icono.ico', wx.BITMAP_TYPE_ICO)
        self.SetIcon(ico)

        # Here we create a panel and a notebook on the panel
        p = wx.Panel(self)
        nb = wx.Notebook(p)

        # create the page windows as children of the notebook
        page1 = PageOne(nb)

        # add the pages to the notebook with the label to show on the tab
        nb.AddPage(page1, "Inyec/Conec")

        # finally, put the notebook in a sizer for the panel to manage
        # the layout
        sizer = wx.BoxSizer()
        sizer.Add(nb, 1, wx.EXPAND)
        p.SetSizer(sizer)


if __name__ == "__main__":
    app = wx.App()
    MainFrame().Show()
    app.MainLoop()

Solution

  • The first error, would be a indentation error!
    The problem is that you are not Binding to the ComboBox event i.e. when you make a selection an event will fire, which must be caught and acted upon.
    You need to catch that event and put the currently selected text from the combobox (or whatever) into the textctrl. Currently you are making no attempt to do that.

    Here is what I assume you want.

    import wx
    
    #Mods
    Nom_Mods = ["Alca v3", "Barcelone v1", "Anti-Fed (French)", "Elegance v3"]
    Info_Mods = ["(ZL + Joystick2) To Open Menu\n(B) To close Menu\nCreate by KillerGamer81"]
    #EndMods
    
    
    class PageOne(wx.Panel):
        def __init__(self, parent):
            wx.Panel.__init__(self, parent)
            sz = wx.BoxSizer(wx.VERTICAL)
    
        #Controls
            self.Listade_Menus = wx.ComboBox(self, -1, pos=(10,80), size=(173,22), choices = Nom_Mods, style= wx.CB_READONLY)
    
            #Bind a callback to the event emitted by the Combobox selection
            self.Listade_Menus.Bind(wx.EVT_COMBOBOX, self.Nom_Mods_Selected)
    
            self.Cuadro_de_info = wx.TextCtrl(self, -1, "", pos=(200,80), size=(175,80), style = wx.TE_MULTILINE|wx.TE_NO_VSCROLL|wx.TE_READONLY)
    
            # When a selection is made populate the textctrl with the selected text
        def Nom_Mods_Selected(self, event):
            self.Cuadro_de_info.SetValue(self.Listade_Menus.GetStringSelection())
    
    
    class MainFrame(wx.Frame):
        def __init__(self):
            no_sys_menu = wx.DEFAULT_FRAME_STYLE & (~wx.RESIZE_BORDER) & (~wx.MAXIMIZE_BOX)
            wx.Frame.__init__(self, None, title="ProyectoU", style=no_sys_menu, size=(400,225))
            ico = wx.Icon('Recursos/icono.ico', wx.BITMAP_TYPE_ICO)
            self.SetIcon(ico)
    
            # Here we create a panel and a notebook on the panel
            p = wx.Panel(self)
            nb = wx.Notebook(p)
    
            # create the page windows as children of the notebook
            page1 = PageOne(nb)
    
            # add the pages to the notebook with the label to show on the tab
            nb.AddPage(page1, "Inyec/Conec")
    
            # finally, put the notebook in a sizer for the panel to manage
            # the layout
            sizer = wx.BoxSizer()
            sizer.Add(nb, 1, wx.EXPAND)
            p.SetSizer(sizer)
    
    if __name__ == "__main__":
        app = wx.App()
        MainFrame().Show()
        app.MainLoop()
    

    enter image description here