Search code examples
pythonwxpythonfilebrowse

How to disable/remove the input box in wx.lib.filebrowsebutton.FileBrowseButton?


Looks like there's no built-in option to disable/remove the InputBox/TextCtrl part in wx.lib.filebrowsebutton.FileBrowseButton, well I did come up with a workaround which is simply set labelText to blank and then size it down to fit only the button itself, this way visually you can tell no difference from a normal button, but I don't think it's nice enough to go with.

So is there a way to fully disable/remove the InputBox part? Or maybe a way to bind normal button with file browser function?


Solution

  • If you don't need a textctrl, then you don't really need wx.lib.FileBrowseButton. You can just have a normal wx.Button that launches a wx.FileDialog instance. In fact, that's all that wx.lib.FileBrowsBbutton does. Here's the relevant source code, the whole thing can be viewed here: https://github.com/wxWidgets/wxPython/blob/master/wx/lib/filebrowsebutton.py

    def OnBrowse (self, event = None):
            """ Going to browse for file... """
            current = self.GetValue()
            directory = os.path.split(current)
            if os.path.isdir( current):
                directory = current
                current = ''
            elif directory and os.path.isdir( directory[0] ):
                current = directory[1]
                directory = directory [0]
            else:
                directory = self.startDirectory
                current = ''
            dlg = wx.FileDialog(self, self.dialogTitle, directory, current,
                                self.fileMask, self.fileMode)
    
            if dlg.ShowModal() == wx.ID_OK:
                self.SetValue(dlg.GetPath())
            dlg.Destroy()