【问题标题】:How to handle file inputs as variables in Python/wxPython?如何在 Python/wxPython 中将文件输入作为变量处理?
【发布时间】:2014-05-07 15:52:40
【问题描述】:

我有一个正在尝试移植到 GUI 的工作脚本。我是编程新手,所以很多代码可能都是hack-ish。我愿意接受有关一般做法和方法的建议!以下是我想要移植的基于文本的工作版本:

def InitUI(self):
    self.pdf = None
    sizer = wx.BoxSizer(wx.VERTICAL)
    btnSizer = wx.BoxSizer(wx.HORIZONTAL)
    self.pdf = PDFWindow(self, style=wx.SUNKEN_BORDER)
    panel = wx.Panel(self)

    sizer.Add(self.pdf, proportion=1, flag=wx.EXPAND)

    pdfPicker = wx.FilePickerCtrl(self, wx.ID_ANY,message='Please select the PDF to resize.', wildcard='*.pdf', size=(500,20))
    btnSizer.Add(pdfPicker, proportion=1, flag=wx.EXPAND|wx.ALL, border=5)
    btnSizer.AddStretchSpacer(3)

    self.label = wx.StaticText(self, label='Enter Scale (decimal percent):')
    self.field = wx.TextCtrl(self, value="0.5", size=(50,20))

    btnSizer.Add(self.label, 0, wx.ALL, 8)
    btnSizer.Add(self.field, 0, wx.ALL, 8)

    # .... more GUI code (buttons, etc.)

def resize_file_main(resize, fileout, self, e=None)
    file1 = ask_file_name('resize1', 'input', '', inputDir)
    fileout = ask_file_name('resize1', 'output')
    input1 = str(file1) + '.pdf'
    dir1 = os.path.join(inputDir, input1)
    backup1 = os.path.join(backupDir, str(file1) + '.pdf')

    resize_file(input1)

    try:
        shutil.move(dir1, backup1)
        print input1, "has been successfully moved to the backup folder.\n"
    except:
        print_error('\nThe PDF you entered is opened elsewhere.  The file was not backed up.')
        print "Please move your scanned PDF from /input to /backup or run the backup utility.\n\n      Press enter to continue  ....  "
        print raw_input('')
    continue

def resize_file(filename, filename2 = None):
    output = PdfFileWriter()

    fIn1 = file(os.path.join(inputDir,filename), 'rb')

    inp1 = PdfFileReader(fIn1)
    p1 = inp1.getPage(0)
    p1.scale(.5,.5)
    output.addPage(p1)

    if filename2 is not None:
        fIn2 = file(os.path.join(inputDir,filename2), 'rb')
        inp2 = PdfFileReader(fIn2)
        p2 = inp2.getPage(0)
        p2.scale(.50,.50)
        output.addPage(p2)

    outputStream = file(os.path.join(validateDir,str(fileout) + '.pdf'),"wb")
    output.write(outputStream)
    outputStream.close()
    fIn1.close()

这是我的问题。我想摆脱所有基于文本的用户交互。如何获取基于对象的输入并将它们转换为可传递的变量?我已经能够实现 wx.TextCtrl 来输入用户输入和 wx.FilePickerCtrl 来选择 PDF 作为输入。现在我该怎么做:

  • 将这些作为变量传递给我的 resize_file 函数?
  • 设置输出位置?
  • 设置备份位置+输入PDF名称?
  • 将 wx.TextCtrl 值(用于缩放)传递给我的 resize_file 函数?

这也可能是我的问题:

  1. 我有一个 InitUI 函数来完成所有 wxPython 的工作
  2. InitUi 函数中的按钮调用 resize_file_main
  3. resize_file_main 只处理输入/输出和移动最终 周围的文件。它还调用 resize_file。
  4. resize_file 是一个在其他几个领域中重复使用的函数 的脚本。它接受各种输入/输出,实际上 调整 PDF 的大小。

这是一个糟糕的流程吗?我不确定如何组合 resize_file_main 和 resize_file 因为调用 resize_file 的不同区域的输入/输出是不同的。

感谢您的帮助!我知道这很复杂!

编辑:谢谢!我相信我有足够的信息继续前进。感谢您的帮助。

【问题讨论】:

    标签: python python-2.7 wxpython wxwidgets


    【解决方案1】:

    回答这个具体问题:

    如何获取基于对象的输入并将其转换为可通过的 变量?

    您访问控件并调用适当的方法:

    self.resize_txt.GetValue()
    self.file_input.GetPath()
    self.outfile_input.GetPath()
    

    要将它们传递给您的调整大小函数,只需将这些值传递给您的 resize_file 函数。

    resize_file(self.file_input.GetPath(),
                self.outfile_input.GetPath())
    

    TBH 你在这里问的问题太多了。你应该从一个开始,随着你的进步创造其他的。

    什么鬼,我有时间在午饭前消磨时间!我给你一些指点。

    我们来看看这个函数:

    def resize_file(filename, filename2 = None)
    

    看着这个函数,我真的不知道这个函数的输入是什么。它说文件名和一个可选的文件名2。其中哪些正在调整大小?我怎么知道每个参数的作用?你没有记录这个函数,这让我深入研究你的代码,试图确定它的作用。

    所以,我深入研究了您的代码...看来这个特定的函数会调整大小并可能附加 pdf。请注意在您的代码中,您如何执行相同的代码两次?

    fIn1 = file(os.path.join(inputDir,filename), 'rb')
    
    inp1 = PdfFileReader(fIn1)
    p1 = inp1.getPage(0)
    p1.scale(.5,.5)
    output.addPage(p1)
    
    if filename2 is not None:
        fIn2 = file(os.path.join(inputDir,filename2), 'rb')
        inp2 = PdfFileReader(fIn2)
        p2 = inp2.getPage(0)
        p2.scale(.50,.50)
        output.addPage(p2)
    

    不要这样做。使用DRY 原则。你应该有一个循环,因为算法基本相同。 (无法说明循环atm,午餐前时间用完了,也许当我回来时:P)

    您甚至可以让您的函数获取无限量的 PDF 文件。检查这个sn-p:

    def resize_file(*args):
        output = PdfFileWriter()
    
        for filename in args:
            fIn1 = file(os.path.join(inputDir, filename), 'rb')
            inp1 = PdfFileReader(fIn1)
            p1 = inp1.getPage(0)
            p1.scale(.5,.5)
            output.addPage(p1)
    
        outputStream = file(os.path.join(validateDir,str(fileout) + '.pdf'),"wb")
        output.write(outputStream)
        outputStream.close()
    

    好吧,我撒谎了,我试图在午饭前把它挤进去。上面的代码可能无法开箱即用,但它应该为您指明大方向。您应该添加错误捕获以检查何时没有传递参数(除其他外)。

    希望对你有帮助!

    【讨论】:

      【解决方案2】:
          #initGUIStuff
          ....
          self.resize_btn.Bind(wx.EVT_BUTTON,self.OnResize)
      
       def OnResize(self,evt):
          resize(self.resize_txt.GetValue(),self.file_input.GetPath(),self.outfile_input.GetPath())
      

      也许?

      说实话不知道你要问什么......

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-08-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-10-02
        相关资源
        最近更新 更多