Python编程之gui程序实现简单文件浏览器代码


Posted in Python onDecember 08, 2017

本文主要分享了关于在python中实现一个简单的文件浏览器的代码示例,代码及展示如下。

#!/usr/bin/env python
# -*- coding: UTF-8 -*- 
import os
from time import sleep
from Tkinter import * 

class DirList(object):
  def __init__(self, initdir=None):
    '''构造函数,说明版本信息'''
    self.top = Tk()
    self.label = Label(self.top, 
      text = 'My directory Lister v1.1')
    self.label.pack()
    self.cwd = StringVar(self.top)
    self.dir1 = Label(self.top, 
      fg='blue', font=('Helvetica', 22, 'bold'))
    self.dir1.pack()
    self.dirfm = Frame(self.top)
    self.dirsb = Scrollbar(self.dirfm)
    self.dirsb.pack(side=RIGHT, fill=Y)
    self.dirs = Listbox(self.dirfm, height=15,
      width=50, yscrollcommand=self.dirsb.set)
    self.dirs.bind('<Double-1>', self.setDirAndGo)
    self.dirsb.config(command=self.dirs.yview)
    self.dirs.pack(side=LEFT, fill=BOTH)
    self.dirfm.pack()
    self.dirn = Entry(self.top, width=50,
      textvariable=self.cwd)
    self.dirn.bind('<Return>', self.doLS)
    self.dirn.pack()
    self.bfm = Frame(self.top)
    self.clr = Button(self.bfm, text='Clear',
      command = self.clrDir,
      activeforeground = 'white',
      activebackground = 'blue')
    self.ls = Button(self.bfm, 
      text = 'List Directory',
      command = self.doLS,
      activeforeground = 'white',
      activebackground = 'green')
    self.quit = Button(self.bfm, text='Quit',
      command=self.top.quit,
      activeforeground='white',
      activebackground='red')
    self.clr.pack(side=LEFT)
    self.ls.pack(side=LEFT)
    self.quit.pack(side=LEFT)
    self.bfm.pack()
    if initdir:
      self.cwd.set(os.curdir)
      self.doLS()
  def clrDir(self, ev=None):
    self.cwd.set('')
  def setDirAndGo(self, ev=None):
    self.last = self.cwd.get()
    self.dirs.config(selectbackground='red')
    check = self.dirs.get(self.dirs.curselection())
    if not check:
      check = os.curdir
    self.cwd.set(check)
    self.doLS()
  def doLS(self, ev=None):
    error = ''
    tdir = self.cwd.get()
    if not tdir: tdir = os.curdir

    if not os.path.exists(tdir):
      error = tdir + ': no such file'
    elif not os.path.isdir(tdir):
      error = tdir + ': not a directory'
    if error:
      self.cwd.set(error)
      self.top.update()
      sleep(2)
      if not (hasattr(self, 'last') \
        and self.last):
        self.last = os.curdir
        self.cwd.set(self.last)
        self.dirs.config(\
          selectbackground='LightSkyBlue')
        self.top.update()
        return
    self.cwd.set(\
      'FETCHING DIRECTORY CONTENTS...')
    self.top.update()
    dirlist = os.listdir(tdir)
    dirlist.sort()
    os.chdir(tdir)
    self.dir1.config(text=os.getcwd())
    self.dirs.delete(0, END)
    self.dirs.insert(END, os.curdir)
    self.dirs.insert(END, os.pardir)
    for eachFile in dirlist:
      self.dirs.insert(END, eachFile)
    self.cwd.set(os.curdir)
    self.dirs.config(\
      selectbackground='LightSkyBlue')
def main():
  d = DirList(os.curdir)
  mainloop()
if __name__ == '__main__':
  main()

结果:

Python编程之gui程序实现简单文件浏览器代码

代码实现功能较简单,感兴趣的朋友参考下吧!

以上就是本文关于Python编程之gui程序实现简单文件浏览器代码的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站:

如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!

Python 相关文章推荐
python原始套接字编程示例分享
Feb 21 Python
Python下singleton模式的实现方法
Jul 16 Python
详解Python pygame安装过程笔记
Jun 05 Python
在python中利用GDAL对tif文件进行读写的方法
Nov 29 Python
Python3实现爬取简书首页文章标题和文章链接的方法【测试可用】
Dec 11 Python
解决Python中pandas读取*.csv文件出现编码问题
Jul 12 Python
Python使用grequests(gevent+requests)并发发送请求过程解析
Sep 25 Python
妙用itchat! python实现久坐提醒功能
Nov 25 Python
Python进程Multiprocessing模块原理解析
Feb 28 Python
Python matplotlib图例放在外侧保存时显示不完整问题解决
Jul 28 Python
pandas提升计算效率的一些方法汇总
May 30 Python
Python中requests库的用法详解
Jun 05 Python
Python中的pygal安装和绘制直方图代码分享
Dec 08 #Python
python的unittest测试类代码实例
Dec 07 #Python
Python numpy 常用函数总结
Dec 07 #Python
分享6个隐藏的python功能
Dec 07 #Python
Python中pygal绘制雷达图代码分享
Dec 07 #Python
Python学习之用pygal画世界地图实例
Dec 07 #Python
用Pygal绘制直方图代码示例
Dec 07 #Python
You might like
php escape URL编码
2008/12/10 PHP
PHP读取txt文件的内容并赋值给数组的代码
2011/11/03 PHP
php获取用户IPv4或IPv6地址的代码
2012/11/15 PHP
关于PHP的相似度计算函数:levenshtein的使用介绍
2013/04/15 PHP
php curl 上传文件代码实例
2015/04/27 PHP
Docker搭建自己的PHP开发环境
2018/02/24 PHP
用javascript getComputedStyle获取和设置style的原理
2008/10/10 Javascript
jQuery+jqmodal弹出窗口实现代码分明
2010/06/14 Javascript
Js动态添加复选框Checkbox的实例方法
2013/04/08 Javascript
为jquery的ajaxfileupload增加附加参数的方法
2014/03/04 Javascript
基于jquery实现的图片在各种分辨率下未知的容器内上下左右居中
2014/05/11 Javascript
使用jquery解析XML示例代码
2014/09/05 Javascript
在Javascript中处理数组之toSource()方法的使用
2015/06/09 Javascript
jQuery实现Meizu魅族官方网站的导航菜单效果
2015/09/14 Javascript
每天一篇javascript学习小结(Date对象)
2015/11/13 Javascript
JQuery日历插件My97DatePicker日期范围限制
2016/01/20 Javascript
关于JS中的apply,call,bind的深入解析
2016/04/05 Javascript
Bootstrap基本插件学习笔记之按钮(21)
2016/12/08 Javascript
详谈Node.js之操作文件系统
2017/08/29 Javascript
简单实现vue验证码60秒倒计时功能
2017/10/11 Javascript
jquery实现图片无缝滚动 蒙版遮蔽效果
2020/01/11 jQuery
CKEditor扩展插件:自动排版功能autoformat插件实现方法详解
2020/02/06 Javascript
python实现定时发送qq消息
2019/01/18 Python
详解Python基础random模块随机数的生成
2019/03/23 Python
从列表或字典创建Pandas的DataFrame对象的方法
2019/07/06 Python
把vgg-face.mat权重迁移到pytorch模型示例
2019/12/27 Python
基于pytorch 预训练的词向量用法详解
2020/01/06 Python
Python3标准库之dbm UNIX键-值数据库问题
2020/03/24 Python
Python3爬虫中关于Ajax分析方法的总结
2020/07/10 Python
python如何调用百度识图api
2020/09/29 Python
详解通过HTML5 Canvas实现图片的平移及旋转变化的方法
2016/03/22 HTML / CSS
2014年党风建设工作总结
2014/11/19 职场文书
小班上学期个人总结
2015/02/12 职场文书
未婚证明格式
2015/06/15 职场文书
鲁滨逊漂流记读书笔记
2015/06/26 职场文书
学前班教学反思
2016/02/24 职场文书