Search code examples
pythonpython-2.7wxpython

Change FilePickerCtrl box width (wxPython)


I am new to wxpython. I am trying to write a little app that allows me to pick a file. I wonder how I can change the width of the file path box.

My code is below:

import wx

class MainWindow(wx.Frame):
    def __init__(self, parent, title):
        wx.Frame.__init__(self, parent, title = title, size=(500, 400))
        self.Center()
        self.panel = wx.Panel(self)

        self.label1 = wx.StaticText(self.panel)
        self.fileCtrl = wx.FilePickerCtrl(self.panel, size=(100, 50))

        row1 = wx.StaticBoxSizer(wx.StaticBox(self.panel, 1, 'Please select the input file:'), orient=wx.HORIZONTAL)
        row1.Add(self.label1,0,wx.TOP | wx.RIGHT,70)
        row1.Add(self.fileCtrl)

        wrapper = wx.FlexGridSizer(1,1,20,20)
        wrapper.AddGrowableCol(0)
        wrapper.Add(row1,50,wx.TOP | wx.LEFT | wx.RIGHT | wx.ALIGN_CENTER,20)
        self.panel.SetSizerAndFit(wrapper)
        self.Centre()
        #self.Fit()

        self.Show()

app = wx.App(False)
win = MainWindow(None, "File selector")
app.MainLoop()

enter image description here


Solution

  • In short, I don't think that you can.
    From the look of your example I'm guessing that you are running on MSW, my example is from Linux but it shouldn't matter in this case.
    Your code looks a bit off, so I have cleaned it up a bit. try it and see if it helps. You could always use wx.FileDialog rather than wx.FilePicker

    import wx
    
    class MainWindow(wx.Frame):
        def __init__(self, parent, title):
            wx.Frame.__init__(self, parent, title = title, size=(500, 400))
            self.panel = wx.Panel(self)
            self.label1 = wx.StaticText(self.panel,wx.ID_ANY,"Select the Input File")
            self.fileCtrl = wx.FilePickerCtrl(self.panel, message="Select the input file name")
            self.selectedfile = wx.TextCtrl(self.panel,wx.ID_ANY,"None",size=(490,25))
            self.fileCtrl.Bind(wx.EVT_FILEPICKER_CHANGED, self.Selected)
            row1 = wx.BoxSizer(wx.VERTICAL)
            row1.Add(self.label1,0,wx.TOP | wx.RIGHT,5)
            row1.Add(self.fileCtrl)
            row1.Add(self.selectedfile)
            self.panel.SetSizer(row1)
            self.Show()
        def Selected(self, event):
            self.selectedfile.SetValue(self.fileCtrl.GetPath())
    app = wx.App(False)
    win = MainWindow(None, "File selector")
    app.MainLoop()
    

    Edit: I'm not sure if anything has changed with this widget in the years since I first answered this question but following would be how I'd code it today.

    import wx
    
    class MainWindow(wx.Frame):
        def __init__(self, parent, title):
            wx.Frame.__init__(self, parent, title = title, size=(500, 200))
            self.panel = wx.Panel(self)
            row1 = wx.StaticBoxSizer(wx.VERTICAL, self.panel, 'Please select the input file:')
            self.fileCtrl = wx.FilePickerCtrl(self.panel, message="Select file",style=wx.FLP_USE_TEXTCTRL,size=(390,25))
            row1.Add(self.fileCtrl,0,wx.ALL,10)
            self.panel.SetSizer(row1)
            self.Show()
    
    app = wx.App(False)
    win = MainWindow(None, "File selector")
    app.MainLoop()
    

    enter image description here