Search code examples
pythonwxpython

generating buttons after a if statement in wxPython


I have wxPython application were user gives a file as an input, and then my program should generate 2 dropdown lists for each line from the file. Problem is that no buttons are generated after user gives a file as an input, this is done in menu and self.content gets it's items. I have tried to call myButtons method after user gives the file, but it didn't work either.

Here is my simplified code for buttons:

class size:
    def __init__(self, id, name):
        self.id = id
        self.name = name

class type:
    def __init__(self, id, name):
        self.id = id
        self.name = name

class GUI(wx.Frame):
    def __init__(self, parent, id, title):
        self.dirname = ''
        self.content = ''

        wx.Frame.__init__(self, parent, id, title, size=(700, 400))

        #UI stuff
        panelControl = wx.Panel(self, 1, style=wx.MAXIMIZE) 
        sizerControl = wx.GridBagSizer(hgap=4,vgap = 4)

        self.menu()
        #buttons
        if not self.content == '':
            self.myButtons()


    def myButtons(self): 
        listOfObjects = self.content #list if lists
        #content of the dropdown lists
        sizes = [size(0, 'very small'), size(1, 'small'), size(2, 'medium'), size(3, 'large'), size(4,'very large')]
        types = [type(0, 'UI'), type(1, 'text'), type(2, 'I/O'), type(3, 'calculation'), 
            type(4, 'set-up'), type(5,  'logic'), type(6, 'data')]

        for i in listOfObjects:
            lista = [] #save the dropdown list selections
            panel = wx.Panel(self, wx.ID_ANY)
            self.labelthing = wx.StaticText(panel, label="Object name: %s LOC: %s Method amount: %s Mean: %s"%(i[0], i[1], i[2], i[3]))
            self.sizeDrop = wx.ComboBox(panel,
                          size=wx.DefaultSize,
                          choices=lista)

            self.typeDrop = wx.ComboBox(panel,
                          size=wx.DefaultSize,
                          choices=lista)

            self.widgetMaker(self.sizeDrop, self.typeDrop)    
            self.Show()


    def widgetMaker(self, widget1, widget2):
        widget1.Bind(wx.EVT_COMBOBOX, self.onSelect)
        widget2.Bind(wx.EVT_COMBOBOX, self.onSelect)


    def onSelect(self, event):
        #wait for the both dropdown list values, then do stuff
        pass

    def menu(self):
        #do other stuff too
        self.content = [['foo1', 1 , 'bar1', 1],['foo2', 1 , 'bar2', 1]]

if __name__ == "__main__":
    app = wx.PySimpleApp()
    frame = GUI(None, -1, "") 
    frame.Show(1)
    app.MainLoop()

Solution

  • You have several issues. For one, you overwrite the widgets in your loop in each iteration. The panel doesn't get overwritten, but the comboboxes and the static text do. So the bindings to the first two comboboxes may not work the way you expect since they are bound to the 2nd instance of the widgets. You are also using wx.PySimpleApp which is deprecated. You should be using wx.App(False) instead.

    Also note that your code will not run as you never imported wx.

    I edited your code a bit to make it work, but I did not fix all the issues with it:

    import wx 
    
    class size:
        def __init__(self, id, name):
            self.id = id
            self.name = name
    
    class type:
        def __init__(self, id, name):
            self.id = id
            self.name = name
    
    class GUI(wx.Frame):
        def __init__(self, parent, id, title):
            self.dirname = ''
            self.content = ''
    
            wx.Frame.__init__(self, parent, id, title, size=(700, 400))
            self.panel = wx.Panel(self)
    
            #UI stuff
            #panelControl = wx.Panel(self, 1, style=wx.MAXIMIZE) 
            sizerControl = wx.BoxSizer(wx.VERTICAL)
    
            self.menu()
            #buttons
            if not self.content == '':
                self.myButtons(sizerControl)
    
            self.panel.SetSizer(sizerControl)
            self.panel.Layout()
    
            self.Show()
    
    
        def myButtons(self, sizerControl): 
            listOfObjects = self.content #list if lists
            #content of the dropdown lists
            sizes = [size(0, 'very small'), size(1, 'small'), size(2, 'medium'), size(3, 'large'), size(4,'very large')]
            types = [type(0, 'UI'), type(1, 'text'), type(2, 'I/O'), type(3, 'calculation'), 
                type(4, 'set-up'), type(5,  'logic'), type(6, 'data')]
    
            for i in listOfObjects:
                lista = [] #save the dropdown list selections
                panel = wx.Panel(self.panel, wx.ID_ANY)
                nested_sizer = wx.BoxSizer(wx.VERTICAL)
                self.labelthing = wx.StaticText(panel, label="Object name: %s LOC: %s Method amount: %s Mean: %s"%(i[0], i[1], i[2], i[3]))
                nested_sizer.Add(self.labelthing, 0, wx.ALL, 5)
    
                self.sizeDrop = wx.ComboBox(panel,
                              size=wx.DefaultSize,
                              choices=lista)
                nested_sizer.Add(self.sizeDrop, 0, wx.ALL, 5)
    
                self.typeDrop = wx.ComboBox(panel,
                              size=wx.DefaultSize,
                              choices=lista)
                nested_sizer.Add(self.typeDrop, 0, wx.ALL, 5)
                panel.SetSizer(nested_sizer)
    
    
                #self.widgetMaker(self.sizeDrop, self.typeDrop)
                sizerControl.Add(panel, 1, wx.EXPAND, 5)
    
    
        def widgetMaker(self, widget1, widget2):
            widget1.Bind(wx.EVT_COMBOBOX, self.onSelect)
            widget2.Bind(wx.EVT_COMBOBOX, self.onSelect)
    
    
        def onSelect(self, event):
            #wait for the both dropdown list values, then do stuff
            pass
    
        def menu(self):
            #do other stuff too
            self.content = [['foo1', 1 , 'bar1', 1],['foo2', 1 , 'bar2', 1]]
    
    if __name__ == "__main__":
        app = wx.App(False)
        frame = GUI(None, -1, "")
        app.MainLoop()
    

    You will note that I swapped out the GridBagSizer for a regular BoxSizer to make this simpler to demonstrate. I used the Widget Inspection Tool to help me figure out what was going on. You should check it out: