Search code examples
checkboxwxpythoncustomdialog

Creating a custom dialog that allows me to select all files at once or a single file individually


I'm attempting to create a custom dialog in wxPython that mimics the wx.MultiChoiceDialog, yet only differs in allowing a user to select all files with the selection of a single check box. This seemed to be a straight forward process, but I've not had success with the textCntrl, checkboxes, or populating the files. Any and all help and direction is appreciated. Thanks!

Below, is one of my many attempts:

import wx

class Extract_file(wx.Dialog):
    def __init__(self, parent, title):
        wx.Dialog.__init__(self, parent, title=title, size=(345, 337))
        self.control = wx.TextCtrl(self, style=wx.TE_MULTILINE | wx.TE_RICH2)
        wx.StaticText(self, -1, 'Files in c:\Extracted', (20,20))

        panel = wx.Panel(self)


        sizer = wx.BoxSizer(wx.HORIZONTAL)

        chbox = wx.CheckBox(panel, -1, 'CheckBox')
        sizer.Add(chbox, 0, wx.ALL, 10)





        compute_btn = wx.Button(self, 1, 'Okay', (167, 272))
        compute_btn.SetFocus()
        clear_btn = wx.Button(self, 2, 'Cancel', (247, 272))

        wx.EVT_BUTTON(self, 1, self.OnOkay)
        wx.EVT_BUTTON(self, 2, self.OnQuit)


        self.Centre()
        self.ShowModal()
        self.Destroy()


    def OnOkay(self, event):
        #do something

    def OnQuit(self, event):
        self.Close(True)    

    if __name__ == '__main__':
        app = wx.App(False)
        dlog = Extract_file(None, 'File Extraction')
        app.MainLoop()

Solution

  • You don't need wx.TextCtrl to make this dialog. MultiChoiceDialog makes use of the control named wx.CheckBoxList. You can get many examples of its use on the net. Here is one more code snippet:

    import wx
    
    class MyMCD(wx.Dialog):
        def __init__(self, parent, message, caption, choices=[]):
            wx.Dialog.__init__(self, parent, -1)
            self.SetTitle(caption)
            sizer = wx.BoxSizer(wx.VERTICAL)
            self.message = wx.StaticText(self, -1, message)
            self.clb = wx.CheckListBox(self, -1, choices = choices)
            self.chbox = wx.CheckBox(self, -1, 'Select all')
            self.btns = self.CreateSeparatedButtonSizer(wx.OK | wx.CANCEL)
            self.Bind(wx.EVT_CHECKBOX, self.EvtChBox, self.chbox)
    
            sizer.Add(self.message, 0, wx.ALL | wx.EXPAND, 5)
            sizer.Add(self.clb, 1, wx.ALL | wx.EXPAND, 5)
            sizer.Add(self.chbox, 0, wx.ALL | wx.EXPAND, 5)
            sizer.Add(self.btns, 0, wx.ALL | wx.EXPAND, 5)
            self.SetSizer(sizer)
    #        self.Fit()
    
        def GetSelections(self):
            return self.clb.GetChecked()
    
        def EvtChBox(self, event):
            state = self.chbox.IsChecked()
            for i in range(self.clb.GetCount()):
                self.clb.Check(i, state)
    
    
    if __name__ == '__main__':
        l = ['AND', 'OR', 'XOR', 'NOT']
        app = wx.PySimpleApp()
        dlg = MyMCD(None, 'Choose as many as you wish', 'MCD Title', choices = l)
        if dlg.ShowModal() == wx.ID_OK:
            result = dlg.GetSelections()
            wx.MessageBox(str(result) + ' were chosen')
        dlg.Destroy()