【发布时间】: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 函数?
这也可能是我的问题:
- 我有一个 InitUI 函数来完成所有 wxPython 的工作
- InitUi 函数中的按钮调用 resize_file_main
- resize_file_main 只处理输入/输出和移动最终 周围的文件。它还调用 resize_file。
- resize_file 是一个在其他几个领域中重复使用的函数 的脚本。它接受各种输入/输出,实际上 调整 PDF 的大小。
这是一个糟糕的流程吗?我不确定如何组合 resize_file_main 和 resize_file 因为调用 resize_file 的不同区域的输入/输出是不同的。
感谢您的帮助!我知道这很复杂!
编辑:谢谢!我相信我有足够的信息继续前进。感谢您的帮助。
【问题讨论】:
标签: python python-2.7 wxpython wxwidgets