python之tkinter使用-文件系统遍历
# tkinter:文件系统遍历
import tkinter as tk, os
from time import sleep class DirList(object):
def __init__(self, initdir=os.curdir):
self.top = tk.Tk()
self.top.title('查找') self.top.geometry('400x500') # 设置窗口大小
self.label = tk.Label(self.top, text='文件列表', font=('宋体 -16 bold'))
self.label.pack() # StringVar:字符串变量,特点是跟踪变量变化,及时显示在界面上
self.cwd = tk.StringVar(self.top)
self.cwd.set(initdir) # 当前目录显示标签
self.dirl = tk.Label(self.top, fg='blue', font=('Helvetica -14 bold'))
self.dirl.pack() # 新建框体容器,存放文件列表和滚动条
self.dirfm = tk.Frame(self.top)
# 滚动条
self.dirsb = tk.Scrollbar(self.dirfm)
self.dirsb.pack(side=tk.RIGHT, fill=tk.Y)
# 列表框
self.dirs = tk.Listbox(self.dirfm, height=23, width=60,
font=('Helvetica -14'),
yscrollcommand=self.dirsb.set)
# 绑定setDirAndGo函数
self.dirs.bind('<Double-1>', self.setDirAndGo)
# 滑动框与列表关联
self.dirsb.config(command=self.dirs.yview)
self.dirs.pack(side=tk.LEFT, fill=tk.X)
self.dirfm.pack(fill=tk.BOTH) # 输入框
self.dirn = tk.Entry(self.top, width=60, font=('Helvetica -14'),
textvariable=self.cwd)
# 监听回车事件,绑定doLS函数,函数必须要有event参数
self.dirn.bind('<Return>', self.doLS)
self.dirn.pack(fill=tk.BOTH) # 功能按钮框架包括三个按钮:清除、查询和退出。
self.bfm = tk.Frame(self.top)
self.clr = tk.Button(self.bfm, text='清除', width=6, command=self.clrDir,
activeforeground='white', activebackground='blue')
self.ls = tk.Button(self.bfm, text='查询', width=6, command=self.doLS,
activeforeground='white', activebackground='green')
self.quit = tk.Button(self.bfm, text='退出', width=6, command=self.top.quit,
activeforeground='white', activebackground='red')
self.clr.pack(side=tk.LEFT, fill=tk.BOTH)
self.ls.pack(side=tk.LEFT, fill=tk.BOTH)
self.quit.pack(side=tk.LEFT, fill=tk.BOTH)
self.bfm.pack() def clrDir(self, ev=None):
'''清空文件路径输入框'''
self.cwd.set('') def setDirAndGo(self, ev=None):
'''设置文件路径并查询'''
self.last = self.cwd.get()
# 选中项背景默认为红色,后续修改为蓝色
self.dirs.config(selectbackground='red')
# 获取文件列表中选择项,没有选则输入框设置为当前目录路径
try:
# 获取目录列表中选中的文件名
check = self.dirs.get(self.dirs.curselection())
except:
print("没有文件或文件错误!")
return if not check:
check = os.curdir
self.cwd.set(check)
self.doLS() def doLS(self, event=''):
'''
查询文件路径
:param event:输入框回车事件触发参数
:return:无
'''
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 + ' is not directory!' if error:
'''出现错误则提示在输入框内,2秒后还原'''
self.dirn.config(fg='red') # 输入框提示文字变为红色
self.cwd.set(error)
self.top.update()
sleep(2)
if not (hasattr(self, 'last') and self.last):
'''使用hasattr函数判断对象是否含有last属性或方法'''
self.last = os.curdir
self.cwd.set(self.last)
self.dirs.config(selectbackground='LightSkyBlue')
self.dirn.config(fg='black') # 输入框文字颜色还原
self.top.update()
return self.cwd.set('FETCHING DIRECTORY CONTENTS...')
self.top.update() '''目录列表框操作'''
self.dirs.delete(0, tk.END) # 清空目录列表
# self.dirs.insert(tk.END, os.curdir) # 添加当前目录"."
self.dirs.insert(tk.END, os.pardir) # 添加上级目录".." '''获取指定目录下的文件,在列表控件展示'''
dirlist = os.listdir(tdir)
dirlist.sort()
os.chdir(tdir) # 改变目录到指定路径
for eachFlie in dirlist:
self.dirs.insert(tk.END, eachFlie)
self.cwd.set(os.getcwd()) # 在输入框中显示当前绝对路径
self.dirl.config(text=os.getcwd()) # 上方标签显示当前路径
self.dirs.config(selectbackground='LightSkyBlue') # 选中时背景色为蓝色
self.last = self.cwd.get() # 记录最后一次路径 def main():
DirList('D:\\')
tk.mainloop() if __name__ == '__main__':
main()
截图:
python之tkinter使用-文件系统遍历的更多相关文章
- python之Tkinter控件学习
转载自 http://www.cnblogs.com/kaituorensheng/p/3287652.html#_label0 阅读目录 1. 产品介绍 2. 设计规划 3. 相关知识 4. 源码 ...
- Python GUI - Tkinter tkMessageBox
Python GUI - Tkinter tkMessageBox: tkMessageBox模块用于显示在您的应用程序的消息框.此模块提供了一个功能,您可以用它来显示适当的消息 tkMess ...
- Python GUI - tkinter
目录: Tkinter 组件 标准属性 几何管理 代码实例: 1. Label & Button 2. Entry & Text 3.Listbox列表 4.Radiobutton单选 ...
- python界面Tkinter编程(tkMessageBox对话框使用)
python界面Tkinter编程(tkMessageBox对话框使用) 转载 https://blog.csdn.net/m_buddy/article/details/80105154 1 ...
- Python中,os.listdir遍历纯数字文件乱序如何解决
Python中,os.listdir遍历纯数字文件乱序如何解决 日常跑深度学习视觉相关代码时,常常需要对数据集进行处理.许多图像文件名是利用纯数字递增的方式命名.通常所用的排序函数sort(),是按照 ...
- python使用数组作为索引遍历数组
python使用数组作为索引遍历数组 觉得有用的话,欢迎一起讨论相互学习~Follow Me python使用数组作为索引遍历数组 import numpy as np a=np.arange(0,5 ...
- python使用tkinter做界面之颜色
python使用tkinter做界面之颜色 from tkinter import *colors = '''#FFB6C1 LightPink 浅粉红#FFC0CB Pink 粉红#DC ...
- python中字典的循环遍历的两种方式
开发中经常会用到对于字典.列表等数据的循环遍历,但是python中对于字典的遍历对于很多初学者来讲非常陌生,今天就来讲一下python中字典的循环遍历的两种方式. 注意: python2和python ...
- python使用tkinter无法给顶层窗体的输入框设定默认值
这几天某同学遇到了一个棘手的问题,困扰了很久.今天终于解决了,我来记录一下坑. 情景:python 使用tkinter为第二层窗体(顶层窗体)中的一个输入框设定默认值时,总是无法设置,而且对输入框获取 ...
随机推荐
- 【转】利用python的KMeans和PCA包实现聚类算法
转自:https://www.cnblogs.com/yjd_hycf_space/p/7094005.html 题目: 通过给出的驾驶员行为数据(trip.csv),对驾驶员不同时段的驾驶类型进行聚 ...
- 阿里图标库使用IconFont
1.注册账号登陆 2.创建项目 3.搜索想使用的图标,添加入库,或者上传自己的图标入库 4.在图标库中,将添加的图标加入项目 5.将项目图标下载至本地 6.下载文件为 包括三种格式,使用方法不同 Un ...
- ASP.NET quartz 定时任务
1.下载 2.使用例子 Demo 概述:Quartz 是开源的定时任务工具类,默认每隔10秒执行一次任务,相当于C#的Timer,不断的循环执行(Start 方法),也可以随时停止(ShutDown方 ...
- BMP280 driver对接单片机I2C或者SPI总线接口
1:登录github网站搜BMP280,找到 BoschSensortec/BMP280_driver 2:gitclone或者download zip都可以,把驱动下载到本地,记得fork哦! 3: ...
- jmeter(二十)阶梯式加压测试
性能测试中,有时需要模拟一种实际生产中经常出现的情况,即:从某个值开始不断增加压力,直至达到某个值,然后持续运行一段时间. 在jmeter中,有这样一个插件,可以帮我们实现这个功能,这个插件就是:St ...
- redis make jemalloc
zmalloc.h:50:31: error: jemalloc/jemalloc.h: No such file or directoryzmalloc.h:55:2: error: #error ...
- Android 解决布局无法对齐的情况
是这样的,在为app制作titlebar或者使用RadioGroup设置布局的的weight属性后,会出现有些机型的手机布局无法居中的问题. 在遇到这类问题时,大部分的原因就是因为没有设置控件的属性: ...
- 在TerraExplorer中如何批量根据shape多边形对象创建TerrainModify对象?
其实,在Skyline中TerrainModify对象就是一个特殊类型Polygon对象,他们的Geometry是可以直接交换使用的: <!DOCTYPE html PUBLIC "- ...
- Vue-条件渲染v-if与v-show
一.共同点 根据数据值来判断是否显示DOM元素 二.区别 代码: <!DOCTYPE html> <html lang="en"> <head> ...
- mysql利用binlog进行数据恢复
目录 mysql利用binlog进行数据恢复 binlog基本配置和格式 binlog基本配置 查看binlog状态 binlog的三种格式 转换成sql mysql自带的mysqlbinlog 利用 ...