python - wxPython FileDialog 防止某些文件被拾取
问题描述
我正在设计某种pdf
转换器。它可以将图像和其他 pdf 文件合并为一个pdf
文件。我正在wxPython
用作 GUI 框架。
有两个按钮:一是设置输入文件,二是设置输出文件。这是我的两个事件处理程序。
def pick_files(self, event):
with wx.FileDialog(self, "Pick files", wildcard=self.load_options,
style=wx.FD_OPEN | wx.FD_FILE_MUST_EXIST | wx.FD_MULTIPLE) as fileDialog:
if fileDialog.ShowModal() != wx.ID_CANCEL:
self.files_list = fileDialog.GetPaths()
self.convert_and_merge_button.Enable()
def convert_and_merge(self, event):
with wx.FileDialog(self, "Convert and merge", wildcard=self.save_options,
style=wx.FD_SAVE | wx.FD_OVERWRITE_PROMPT) as fileDialog:
if fileDialog.ShowModal() != wx.ID_CANCEL:
# pass parameters to Converter class
self.converter.convert(self.files_list, fileDialog.GetPath())
问题是,如果我选择一个输入文件作为输出文件,程序就会崩溃。如何防止某些文件被选中FileDialog
?
我想知道是否有可能有某种MessageBox
,比如“这个文件被设置为输入文件。你不能覆盖它。” 并回到FileDialog
.
解决方案
只需在合并例程之前根据输入文件列表检查为输出选择的文件名。
import wx
class MyFrame(wx.Frame):
def __init__(self, *args, **kwargs):
wx.Frame.__init__(self, *args, **kwargs)
panel = wx.Panel(self)
sizer = wx.BoxSizer(wx.HORIZONTAL)
self.select_button = wx.Button(panel, label="Select files")
self.convert_and_merge_button = wx.Button(panel, label="Merge files")
sizer.Add(self.select_button, 0, 0, 0)
sizer.Add(self.convert_and_merge_button, 0, 0, 0)
self.select_button.Bind(wx.EVT_BUTTON, self.pick_files)
self.convert_and_merge_button.Bind(wx.EVT_BUTTON, self.convert_and_merge)
self.load_options = "Pdf and Image Files |*.pdf;*.gif;*.bmp;*.tif;*.png;"
self.save_options = "Pdf Files |*.pdf;"
self.convert_and_merge_button.Enable(False)
panel.SetSizer(sizer)
def pick_files(self, event):
with wx.FileDialog(self, "Pick files", wildcard=self.load_options,
style=wx.FD_OPEN | wx.FD_FILE_MUST_EXIST | wx.FD_MULTIPLE) as fileDialog:
if fileDialog.ShowModal() != wx.ID_CANCEL:
self.files_list = fileDialog.GetPaths()
self.convert_and_merge_button.Enable()
def convert_and_merge(self, event):
with wx.FileDialog(self, "Convert and merge", wildcard=self.save_options,
style=wx.FD_SAVE | wx.FD_OVERWRITE_PROMPT) as fileDialog:
if fileDialog.ShowModal() != wx.ID_CANCEL:
# pass parameters to Converter class
merge_file = fileDialog.GetPath()
#Test that the output file is not in the input list
if merge_file in self.files_list:
wx.MessageBox('The chosen output file is in the input files\n Choose another file', 'Error', wx.OK | wx.ICON_INFORMATION)
return
self.converter(self.files_list, merge_file)
def converter(self, files_list, merge_file):
print ("Merging:\n"+str(files_list)+"\n into\n"+str(merge_file))
class MyApp(wx.App):
def OnInit(self):
frame = MyFrame(None, -1, 'A test dialog')
frame.Show()
return True
if __name__ == "__main__":
app = MyApp()
app.MainLoop()
推荐阅读
- java - 执行插入时获取 SQL 语法异常
- shell - 将书签文件夹名称转换为 Firefox 书签 JSON 导出中的标签
- oracle - 换行未通过 SSIS 上的 Attunity 驱动程序传输到 Oracle
- android - 为什么我不能为 LiveData 赋值?
- php - 如何在 Laravel 中将 JWT 令牌发送到 jQuery
- python - 如何合并两行并将所选列的值划分为新列
- javascript - Axios 请求在 iOS 上显示响应但不适用于 android
- wordpress - 如何 301 重定向特定页面以删除部分 URL?
- html - 如何使用
- jquery - 出于安全考虑,是否有类似于 End of Life/Supported Versions 的 jQuery 版本的支持终止