更改 FilePickerCtrl 框宽度 (wxPython)

Change FilePickerCtrl box width (wxPython)

我是 wxpython 的新手。我正在尝试编写一个允许我选择文件的小应用程序。 我想知道如何更改文件路径框的宽度

我的代码如下:

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

总之,我认为你做不到。
从你的例子来看,我猜你是 运行 的 MSW,我的例子来自 Linux 但在这种情况下应该无关紧要。
你的代码看起来有点不对劲,所以我已经清理了一下。尝试一下,看看是否有帮助。 你总是可以使用 wx.FileDialog 而不是 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()

编辑: 自从我第一次回答这个问题以来,我不确定这些小部件是否有任何变化,但下面是我今天的编码方式。

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

您可以使用 VERTICAL & HORIZONTAL BoxSizer 并将小部件的比例保持为 1 而不是默认的 0

代码如下:

import wx
import os

wildcard = "All files (*.*)|*.*"


class MainWindow(wx.Frame):

    def __init__(self):
        wx.Frame.__init__(self, None, title='File Selector')
        self.currentDirectory = os.getcwd()

        self.panel = wx.Panel(self)
        vbox = wx.BoxSizer(wx.VERTICAL)

        ie_box = wx.StaticBox(self.panel, -1, 'Please select the input file')
        ie_sizer = wx.StaticBoxSizer(ie_box, wx.VERTICAL)

        fl_box = wx.BoxSizer(wx.HORIZONTAL)
        self.fl_ctrl = wx.FilePickerCtrl(self.panel, message="Choose a file")
        fl_box.Add(self.fl_ctrl, 1, wx.ALL | wx.CENTER | wx.EXPAND, 5)
        ie_sizer.Add(fl_box, 1, wx.ALL | wx.CENTER | wx.EXPAND, 10)
        self.fl_ctrl.Bind(wx.EVT_FILEPICKER_CHANGED, self.on_open_file)

        vbox.Add(ie_sizer, 0, wx.ALL | wx.CENTER | wx.EXPAND, 5)
        self.panel.SetSizer(vbox)
        self.Center()
        self.panel.Fit()
        self.Show()

    def on_open_file(self, event):
        self.fl_ctrl.GetPath()


if __name__ == '__main__':
    app = wx.App()
    frame = MainWindow()
    app.MainLoop()