首页 > 解决方案 > 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.

标签: pythonwxpython

解决方案


只需在合并例程之前根据输入文件列表检查为输出选择的文件名。

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

推荐阅读