【Python】简单的筛选格式 python工具

自己写的简单的筛选格式 python工具,第一个版本只可以扫描文件,第二个版本扫描出来之后双击就能运行程序了。

  1. [代码]这个版本只能扫描文件
import wx
import os
import time
 
class Filter(wx.Frame):
    def __init__(self, parent, id):
        wx.Frame.__init__(self, parent, id, 'Filter', size=(310,400),  style=wx.DEFAULT_FRAME_STYLE^(wx.MAXIMIZE_BOX))
        self.panel = wx.Panel(self)
 
        #Icon
        self.icon = wx.Icon('icon.ico',wx.BITMAP_TYPE_ICO)
        self.SetIcon(self.icon)
         
        #dirButton        
        self.dirButton = wx.Button(self.panel, -1, 'Choose Your Directory', pos=(20, 20))
        self.dirButton.Bind(wx.EVT_BUTTON, self.onDirButton)
        self.tips = wx.StaticText(self.panel, -1, 'Your Directory:', pos=(20,55))
 
        #outPut
        self.outPut = wx.TextCtrl(self.panel, -1, pos=(20, 100), size=(250, 200),style=wx.TE_MULTILINE|wx.HSCROLL)
 
        #List
        self.List = ['jpg', 'png', 'avi', 'mp4', 'rmvb', 'rar', 'zip', 'torrent']
        self.formatList = wx.Choice(self.panel, -1, pos=(200,20), choices=self.List)
        self.formatList.Bind(wx.EVT_CHOICE, self.onFormatList)
 
        #Start and Quit
        self.startButton = wx.Button(self.panel, -1, 'Start', pos=(90,310))
        self.startButton.Bind(wx.EVT_BUTTON, self.onStartButton)
        self.QuitButton = wx.Button(self.panel, -1, 'Quit', pos=(180,310))
        self.QuitButton.Bind(wx.EVT_BUTTON, self.onQuitButton)
          
    def onDirButton(self, event):
        self.dirDialog = wx.DirDialog(None)
        if self.dirDialog.ShowModal() == wx.ID_OK:
            self.path = self.dirDialog.GetPath()
        else:
            self.path = ''
            self.dirDialog.Destroy()
        self.dirDialog.Destroy()
        self.directory = wx.StaticText(self.panel, -1, self.path, pos=(20,75))
       
    def onFormatList(self, event):
        choice=self.formatList.GetSelection() 
        self.yourFormat = self.List[choice]
     
    def onStartButton(self, event):
        try:
            startTime = time.clock()
            self.outPut.Clear()
            for root,dirs,files in os.walk(self.path):
                for file in files:
                    if os.path.splitext(file)[1][1:] == self.yourFormat:
                        self.outPut.AppendText(file +'\n')    
            if self.outPut.GetValue() == '':
                self.outPut.SetValue('No %s File! \n' % self.yourFormat)
            endTime = time.clock()
            self.outPut.AppendText('Finished! Used %.3f s' % (endTime - startTime))
        except AttributeError:
            retCode = wx.MessageBox('Choose Your Directory & Format!', 'FormatError',
                                    wx.OK | wx.ICON_ERROR)
              
    def onQuitButton(self, event):
        self.Destroy()
 
app = wx.PySimpleApp()
frame = Filter(parent=None, id=-1)
frame.Show()
app.MainLoop()

  1. [代码]修改版,筛选了之后双击可以打开文件

import wx
import os, sys
import time, win32api
 
reload(sys)
sys.setdefaultencoding('utf-8')
 
class Filter(wx.Frame):
    def __init__(self, parent, id):
        wx.Frame.__init__(self, parent, id, 'Filter', size=(310,400),
                          style=wx.DEFAULT_FRAME_STYLE^(wx.MAXIMIZE_BOX | wx.RESIZE_BORDER))
        self.panel = wx.Panel(self)
 
        #Icon
        self.icon = wx.Icon('icon.ico',wx.BITMAP_TYPE_ICO)
        self.SetIcon(self.icon)
         
        #dirButton        
        self.dirButton = wx.Button(self.panel, -1, 'Choose Your Directory', pos=(20, 20))
        self.dirButton.Bind(wx.EVT_BUTTON, self.onDirButton)
        self.tips = wx.StaticText(self.panel, -1, 'Your Directory:', pos=(20,55))
 
        #outPut
        self.outPut = wx.ListCtrl(self.panel, -1, pos=(20, 100),size=(250, 200),
                                  style=wx.LC_REPORT | wx.LC_VRULES | wx.LC_HRULES)
 
        self.outPut.Bind(wx.EVT_LIST_ITEM_ACTIVATED, self.DConLCItem)
        self.outPut.InsertColumn(0,'File Name', wx.LIST_FORMAT_RIGHT, 100)
        self.outPut.InsertColumn(1,'Directory', wx.LIST_FORMAT_LEFT, 100)
 
        #List
        self.List = ['jpg', 'png', 'avi', 'wmv', 'mp4', 'rmvb', 'rar', 'zip', 'exe', 'torrent']
        self.formatList = wx.Choice(self.panel, -1, pos=(200,20), choices=self.List)
        self.formatList.Bind(wx.EVT_CHOICE, self.onFormatList)
 
        #Start and Quit
        self.startButton = wx.Button(self.panel, -1, 'Start', pos=(110,325))
        self.startButton.Bind(wx.EVT_BUTTON, self.onStartButton)
        self.QuitButton = wx.Button(self.panel, -1, 'Quit', pos=(195,325))
        self.QuitButton.Bind(wx.EVT_BUTTON, self.onQuitButton)
 
    def DConLCItem(self, event):
        #Double Click on ListCtrl Item
        fileIndex = self.outPut.GetFocusedItem()
        selectedDir = self.outPut.GetItem(fileIndex, 1).GetText()        
        selectedFile = self.outPut.GetItemText(fileIndex)
         
        win32api.ShellExecute(0, 'open', selectedFile, '', selectedDir, 1)
     
         
    def onDirButton(self, event):
        self.dirDialog = wx.DirDialog(None)
        if self.dirDialog.ShowModal() == wx.ID_OK:
            self.path = self.dirDialog.GetPath()
        else:
            self.path = ''
            self.dirDialog.Destroy()
        self.dirDialog.Destroy()
        self.directory = wx.StaticText(self.panel, -1, self.path, pos=(20,75))
       
    def onFormatList(self, event):
        choice=self.formatList.GetSelection() 
        self.yourFormat = self.List[choice]
     
    def onStartButton(self, event):
        try:
            startTime = time.clock()
            self.outPut.DeleteAllItems()
            index = 0
            for root, dirs, files in os.walk(self.path):
                for file in files:
                    if os.path.splitext(file)[1][1:] == self.yourFormat:
                        self.outPut.InsertStringItem(index, file)
                        self.outPut.SetStringItem(index, 1, unicode(root))
                        index += 1
            if self.outPut.GetItemCount() == 0:
                errorResult = wx.MessageBox('No %s Files!' % self.yourFormat, 'No File',
                                            wx.OK | wx.ICON_INFORMATION)
            endTime = time.clock()
            usedTime = wx.StaticText(self.panel, -1, 'Finished! Find %d file(s) ! Used %.3f s' %
                                     (self.outPut.GetItemCount(), endTime - startTime), pos=(20,305))
             
        except AttributeError:
            retCode = wx.MessageBox('Choose Your Directory & Format!', 'FormatError',
                                    wx.OK | wx.ICON_ERROR)
              
    def onQuitButton(self, event):
        self.Destroy()
 
app = wx.PySimpleApp()
frame = Filter(parent=None, id=-1)
frame.Show()
app.MainLoop()

推荐阅读:

零基础入门Python的最详细的源码教程

2019年Python爬虫学习路线图完整版

Python为何能坐稳AI人工智能的头牌语言

Python崛起,TIOBE编程语言排行榜再创新高!

上一篇:javaweb(十九)——JSP标签


下一篇:Panel