wxpython中文教程之文件猎手FILE HUNTER

发布时间:2020-02-03编辑:脚本学堂
有关wxpython中实现文件猎手功能的例子,一个文件管理器的骨架. 它山寨的是 Krusader 的样子,wxpython中文教程学习,需要的朋友参考下。

python/ target=_blank class=infotextkey>python脚本将会生成界面而不会实现其功能. 目的是展示一些这些有名的 GUI 界面在 wxPython 中是怎么完成的。

绝大多数的手册、教程以及电子书都是展示部件的基本用法的。

而当我(作者本人)还是一名初学者时,我总是想知道这些东西是怎么做出来的. 同时我认为大多数新手都会经历这个过程。

文件猎手File Hunter

这是一个文件管理器的骨架. 它山寨的是 Krusader 的样子,Krusader 是 Unix 系统上最好的文件管理器了.

专题教程:wxpython中文教程

完整代码:
 

复制代码 代码示例:

#!/usr/bin/python
#coding=utf-8

import wx
import os
import time

ID_BUTTON = 100
ID_EXIT = 200
ID_SPLITTER = 300

class MyListCtrl(wx.ListCtrl):
    def __init__(self, parent, id):
        wx.ListCtrl.__init__(self, parent, id, style=wx.LC_REPORT)
       
        files = os.listdir('.')
        images = ['images/empty.png', 'images/folder.png',
                  'images/source_py.png', 'images/image.png',
                  'images/pdf.png', 'images/up16.png']
       
        self.InsertColumn(0, '文件名')
        self.InsertColumn(1, '类型')
        self.InsertColumn(2, '大小', wx.LIST_FORMAT_RIGHT)
        self.InsertColumn(3, '修改日期')

        self.SetColumnWidth(0, 220)
        self.SetColumnWidth(0, 70)
        self.SetColumnWidth(0, 100)
        self.SetColumnWidth(0, 420)
       
        self.il = wx.ImageList(16, 16)
        for i in images:
            self.il.Add(wx.Bitmap(i))
        self.SetImageList(self.il, wx.IMAGE_LIST_SMALL)
       
        j = 1
        self.InsertStringItem(0, '..')
        self.SetItemImage(0, 5)
       
        for f in files:
            (name, ext) = os.path.splitext(f)
            ex = ext[1:]
            size = os.path.getsize(f)
            sec = os.path.getmtime(f)
            self.InsertStringItem(j, name)
            self.SetStringItem(j, 1, ex)
            self.SetStringItem(j, 2, str(size) + ' 字节')
            self.SetStringItem(j, 3, time.strftime('%Y-%m-%d %H:%M',
                                                   time.localtime(sec)))
           
            if os.path.isdir(f):
                self.SetItemImage(j, 1)
            elif ex == 'py':
                self.SetItemImage(j, 2)
            elif ex == 'jpg':
                self.SetItemImage(j, 3)
            elif ex == 'pdf':
                self.SetItemImage(j, 4)
            else:
                self.SetItemImage(j, 0)
               
            if (j % 2) == 0:
                self.SetItemBackgroundColour(j, '#e6f1f5')
            j += 1

class FileHunter(wx.Frame):
    def __init__(self, parent, id, title):
        wx.Frame.__init__(self, parent, id, title)
       
        self.splitter = wx.SplitterWindow(self, ID_SPLITTER,
                                          style=wx.SP_BORDER)
        self.splitter.SetMinimumPaneSize(50)
       
        p1 = MyListCtrl(self.splitter, -1)
        p2 = MyListCtrl(self.splitter, -1)
        self.splitter.SplitVertically(p1, p2)
       
        self.Bind(wx.EVT_SIZE, self.OnSize)
        self.Bind(wx.EVT_SPLITTER_DCLICK, self.OnDoubleClick,
                  id=ID_SPLITTER)
       
        filemenu = wx.Menu()
        filemenu.Append(ID_EXIT, "退出(&x)", "关闭程序")
        editmenu = wx.Menu()
        netmenu = wx.Menu()
        showmenu = wx.Menu()
        configmenu = wx.Menu()
        helpmenu = wx.Menu()
       
        menubar = wx.MenuBar()
        menubar.Append(filemenu, "文件(&F)")
        menubar.Append(editmenu, "编辑(&E)")
        menubar.Append(netmenu, "网络(&N)")
        menubar.Append(showmenu, "显示(&S)")
        menubar.Append(configmenu, "配置(&C)")
        menubar.Append(helpmenu, "帮助(&H)")
        self.SetMenuBar(menubar)
        self.Bind(wx.EVT_MENU, self.OnExit, id=ID_EXIT)

        toolbar = self.CreateToolBar(wx.TB_HORIZONTAL | wx.NO_BORDER
                                     | wx.TB_FLAT | wx.TB_TEXT)
        toolbar.AddSimpleTool(10, wx.Bitmap('images/previous.png'),
                              "前一")
        toolbar.AddSimpleTool(20, wx.Bitmap('images/up.png'),
                              "上一级目录")
        toolbar.AddSimpleTool(20, wx.Bitmap('images/home.png'), "主目录")
        toolbar.AddSimpleTool(20, wx.Bitmap('images/refresh.png'),
                              "刷新")
        toolbar.AddSeparator()
        toolbar.AddSimpleTool(20, wx.Bitmap('images/write.png'),
                              "编辑器")
        toolbar.AddSimpleTool(20, wx.Bitmap('images/terminal.png'),
                              "终端")
        toolbar.AddSeparator()
        toolbar.AddSimpleTool(20, wx.Bitmap('images/help.png'), "帮助")
        toolbar.Realize()
       
        self.sizer2 = wx.BoxSizer(wx.HORIZONTAL)
       
        button1 = wx.Button(self, ID_BUTTON + 1, "F3 查看")
        button2 = wx.Button(self, ID_BUTTON + 2, "F4 编辑")
        button3 = wx.Button(self, ID_BUTTON + 3, "F5 复制")
        button4 = wx.Button(self, ID_BUTTON + 4, "F6 移动")
        button5 = wx.Button(self, ID_BUTTON + 5, "F7 新建目录")
        button6 = wx.Button(self, ID_BUTTON + 6, "F8 删除")
        button7 = wx.Button(self, ID_BUTTON + 7, "F9 重命名")
        button8 = wx.Button(self, ID_EXIT, "F10 退出")
       
        self.sizer2.Add(button1, 1, wx.EXPAND)
        self.sizer2.Add(button2, 2, wx.EXPAND)
        self.sizer2.Add(button3, 3, wx.EXPAND)
        self.sizer2.Add(button4, 4, wx.EXPAND)
        self.sizer2.Add(button5, 5, wx.EXPAND)
        self.sizer2.Add(button6, 6, wx.EXPAND)
        self.sizer2.Add(button7, 7, wx.EXPAND)
        self.sizer2.Add(button8, 8, wx.EXPAND)

        self.Bind(wx.EVT_BUTTON, self.OnExit, id=ID_EXIT)
       
        self.sizer = wx.BoxSizer(wx.VERTICAL)
        self.sizer.Add(self.splitter, 1, wx.EXPAND)
        self.sizer.Add(self.sizer2, 0, wx.EXPAND)
        self.SetSizer(self.sizer)
       
        size = wx.DisplaySize()
        self.SetSize(size)
       
        self.statusbar = self.CreateStatusBar()
        self.statusbar.SetStatusText(os.getcwd())
        self.Center()
        self.Show(True)
       
    def OnExit(self, e):
        self.Close()
       
    def OnSize(self, event):
        size = self.GetSize()
        self.splitter.SetSashPosition(size.x / 2)
        self.statusbar.SetStatusText(os.getcwd())
        event.Skip()
       
    def OnDoubleClick(self, event):
        size = self.GetSize()
        self.splitter.SetSashPosition(size.x / 2)
   
app = wx.App(0)
filehunter = FileHunter(None, -1, '文件猎手')
app.MainLoop()

如在分割窗(splitter)部件上双击,或缩放窗口,都将让“文件猎手”程序分成两个同样大小的部分。

如图:

wxpython中文教程之文件猎手

文件猎手