import os
from time import sleep
from tkinter import *
from tkinter.messagebox import showinfo class DirList(object):
def __init__(self, initdir=None):
self.top = Tk()
self.label = Label(master=self.top, text='Directory Lister V1.0')
self.label.pack() self.cwd = StringVar(master=self.top) self.dirl = Label(self.top, fg='blue', font=('Helvetica', 14, 'bold'))
self.dirl.pack() self.dirfm = Frame(master=self.top)
self.dirsb = Scrollbar(master=self.dirfm)
self.dirsb.pack(side=RIGHT,fill=Y)    # fill=Y,垂直填充空间排列 self.dirs = Listbox(master=self.dirfm, height=15, width=50, yscrollcommand=self.dirsb.set)
self.dirs.bind('<Double-1>', func=self.setDirAndGo)   # <Double-1>,双击显示路径列表
self.dirsb.config(command=self.dirs.yview)
self.dirs.pack(side=LEFT, fill=BOTH)
self.dirfm.pack() self.dirn = Entry(master=self.top, width=50, textvariable=self.cwd)
self.dirn.bind('<Return>', func=self.doLS)
self.dirn.pack() self.bfm = Frame(master=self.top)
self.cleer = Button(master=self.bfm, text='清除', command=self.clrDir, activeforeground='white',
activebackground='blue')
self.ls = Button(master=self.bfm, text='显示列表', command=self.doLS, activeforeground='white',
activebackground='green')
self.quit = Button(master=self.bfm, text='退出', command=self.top.quit, activeforeground='white',
activebackground='red')
self.cleer.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 setDirAndGo(self, ev=None):
self.last = self.cwd.get()
self.dirs.config(selectbackground='red')
chek = self.dirs.get(self.dirs.curselection())
if not chek:
chek = os.curdir
self.cwd.set(chek)
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 + ':未找到文件,请检查路径!'
elif not os.path.isdir(tdir):
error = tdir + ':不是一个路径!' if error:
# self.cwd.set(error)
showinfo(title='提示',message=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 if not os.path.isdir(tdir):
self.cwd.set('')
else:
self.cwd.set('获取目录内容中...')
self.top.update()
dirlist = os.listdir(tdir)
dirlist.sort()
os.chdir(tdir) self.dirl.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 clrDir(self, ev=None):
self.cwd.set('') if __name__ == '__main__':
dir = DirList(os.curdir)
mainloop()

效果如下:

  至此,转载请注明出处。

Python GUI编程实例的更多相关文章

  1. Python GUI 编程

    Python GUI编程(Tkinter) Python 提供了多个图形开发界面的库,几个常用 Python GUI 库如下: Tkinter: Tkinter 模块(Tk 接口)是 Python 的 ...

  2. Python GUI编程(Tkinter) windows界面开发

    Python实现GUI简单的来说可以调用Tkinter库,这样一般的需求都可以实现,显示简单的windows窗口代码如下: python_gui.py 1 #!C:\Python27\python.e ...

  3. Python GUI编程各种实现的对比

    Python GUI编程各种实现的对比 从 Python 语言的诞生之日起,就有许多优秀的 GUI 工具集整合到 Python 当中,这些优秀的 GUI 工具集,使得 Python 也可以在图形界面编 ...

  4. Python gui编程pyQt5安装步骤t

    Python gui编程pyQt5安装步骤         pip install PyQt5 Pip3 install PyQt5               https://riverbankco ...

  5. Python gui编程pyQt5安装步骤

    Python gui编程pyQt5安装步骤 =============================== -m PyQt5.uic.pyuic  $FileName$ -o $FileNameWit ...

  6. python GUI编程tkinter示例之目录树遍历工具

    摘录 python核心编程 本节我们将展示一个中级的tkinter应用实例,这个应用是一个目录树遍历工具:它会从当前目录开始,提供一个文件列表,双击列表中任意的其他目录,就会使得工具切换到新目录中,用 ...

  7. Python GUI编程--Tkinter

    今天看到了GUI编程,书上推荐用wxPython,去官网上看了看,发现Windows的最高支持到2.7,我用的是3.4版本,咋办,用自带的库--Tkinter呗,它是Python的默认GUI库,几乎是 ...

  8. Python并发编程实例教程

    有关Python中的并发编程实例,主要是对Threading模块的应用,文中自定义了一个Threading类库. 一.简介 我们将一个正在运行的程序称为进程.每个进程都有它自己的系统状态,包含内存状态 ...

  9. python udp编程实例

    与python tcp编程控制见 http://blog.csdn.net/aspnet_lyc/article/details/39854569 c++ udp/tcp 编程见 http://blo ...

随机推荐

  1. vue 项目实战 (vue全家桶之--- vuex)

    老规矩先安装 npm install vuex --save 在看下面内容之前 你应该大概的看了一边vuex官方的文档对vuex有个大概对了解 首先 vuex 是什么? vuex 是属于vue中的什么 ...

  2. sql server 备份与恢复系列四 大容量模式下的备份与还原

    一. 概述 在sql server 备份与恢复系列的第一篇里,有讲到大容量模式下备份与还原的相关知识.这篇重点来演示在大容量模式下常用的备份与还原模式“完整备份+差异备份+日志备份”. 在大容量恢复模 ...

  3. tcp关闭状态详解

    tcp关闭连接不区分客户端和服务端,哪一端口可以主动发起关闭连接请求.所以为了描述方便,描述中的“主动方”表示主动发起关闭连接一方,“被动方”表示被动关闭连接一方. 1. tcp关闭连接状态转换 上图 ...

  4. es6入门1-- let与var的区别详解

    一.前言 说到做到,现在暂时放了放JS模式的读书笔记,打算好好看看ES6,毕竟出了这么久了,还是靠JS吃饭的,都不好好学JS新特性,确实说不过去,我本来是想当读书笔记去记录ES6,但是这个确实是属于边 ...

  5. JavaScript之函数(上)

    在编程语言中,无论是面向过程的C,兼备面过程和对象的c++,还是面向对象的编程语言,如java,.net,php等,函数均扮演着重要的角色.当然,在面向对象编程语言JavaScript中(严格来说,J ...

  6. 五分钟彻底学会iptables防火墙--技术流ken

    iptables简介 IPTABLES 是与最新的 3.5 版本 Linux内核集成的 IP 信息包过滤系统.如果 Linux 系统连接到因特网或 LAN.服务器或连接 LAN 和因特网的代理服务器, ...

  7. [转]分别使用Node.js Express 和 Koa 做简单的登录页

    本文转自:https://blog.csdn.net/weixin_38498554/article/details/79204240 刚刚学了Koa2,由于学的不是很深,并没有感受到网上所说的Koa ...

  8. [转]Build An Image Manager With NativeScript, Node.js, And The Minio Object Storage Cloud

    本文转自:https://www.thepolyglotdeveloper.com/2017/04/build-image-manager-nativescript-node-js-minio-obj ...

  9. .Net C# 使用Redis

    Redis是一个开源的使用ANSI C语言编写.支持网络.可基于内存亦可持久化的日志型.Key-Value数据库,并提供多种语言的API.从2010年3月15日起,Redis的开发工作由VMware主 ...

  10. 深入理解Redis内存模型

    前言 Redis是目前最火爆的内存数据库之一,通过在内存中读写数据,大大提高了读写速度,可以说Redis是实现网站高并发不可或缺的一部分. 我们使用Redis时,会接触Redis的5种对象类型(字符串 ...