创建一个自定义对话框,允许我一次选择所有文件或单独选择一个文件

时间:2012-02-20 13:28:52

标签: checkbox wxpython customdialog

我正在尝试在wxPython中创建一个模仿wx.MultiChoiceDialog的自定义对话框,但只允许用户选择所有选中单个复选框的文件。这似乎是一个直接的过程,但我没有成功使用textCntrl,复选框或填充文件。任何和所有的帮助和方向表示赞赏。谢谢!

下面是我多次尝试之一:

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()

1 个答案:

答案 0 :(得分:1)

您无需wx.TextCtrl即可创建此对话框。 MultiChoiceDialog使用名为wx.CheckBoxList的控件。您可以在网上获得许多其使用示例。这是另外一个代码段:

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()