【问题标题】:Change FilePickerCtrl box width (wxPython)更改 FilePickerCtrl 框的宽度(wxPython)
【发布时间】:2015-11-13 00:52:01
【问题描述】:

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

【问题讨论】:

    标签: python python-2.7 wxpython


    【解决方案1】:

    简而言之,我认为你不能。
    从你的例子看,我猜你是在 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()
    

    【讨论】:

      【解决方案2】:

      您可以使用 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()
      

      【讨论】:

        猜你喜欢
        • 2021-12-29
        • 1970-01-01
        • 2016-11-13
        • 2015-07-07
        • 2012-12-27
        • 1970-01-01
        • 2016-07-25
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多