Python GUI编程实例
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编程实例的更多相关文章
- Python GUI 编程
Python GUI编程(Tkinter) Python 提供了多个图形开发界面的库,几个常用 Python GUI 库如下: Tkinter: Tkinter 模块(Tk 接口)是 Python 的 ...
- Python GUI编程(Tkinter) windows界面开发
Python实现GUI简单的来说可以调用Tkinter库,这样一般的需求都可以实现,显示简单的windows窗口代码如下: python_gui.py 1 #!C:\Python27\python.e ...
- Python GUI编程各种实现的对比
Python GUI编程各种实现的对比 从 Python 语言的诞生之日起,就有许多优秀的 GUI 工具集整合到 Python 当中,这些优秀的 GUI 工具集,使得 Python 也可以在图形界面编 ...
- Python gui编程pyQt5安装步骤t
Python gui编程pyQt5安装步骤 pip install PyQt5 Pip3 install PyQt5 https://riverbankco ...
- Python gui编程pyQt5安装步骤
Python gui编程pyQt5安装步骤 =============================== -m PyQt5.uic.pyuic $FileName$ -o $FileNameWit ...
- python GUI编程tkinter示例之目录树遍历工具
摘录 python核心编程 本节我们将展示一个中级的tkinter应用实例,这个应用是一个目录树遍历工具:它会从当前目录开始,提供一个文件列表,双击列表中任意的其他目录,就会使得工具切换到新目录中,用 ...
- Python GUI编程--Tkinter
今天看到了GUI编程,书上推荐用wxPython,去官网上看了看,发现Windows的最高支持到2.7,我用的是3.4版本,咋办,用自带的库--Tkinter呗,它是Python的默认GUI库,几乎是 ...
- Python并发编程实例教程
有关Python中的并发编程实例,主要是对Threading模块的应用,文中自定义了一个Threading类库. 一.简介 我们将一个正在运行的程序称为进程.每个进程都有它自己的系统状态,包含内存状态 ...
- python udp编程实例
与python tcp编程控制见 http://blog.csdn.net/aspnet_lyc/article/details/39854569 c++ udp/tcp 编程见 http://blo ...
随机推荐
- Linux编程 10 (shell外部命令与内建命令,alias ,type命令)
一. 内部命令 Linux命令有内部命令(内建命令)和外部命令之分,内部命令和外部命令功能基本相同,但也有些细微差别.内部命令不需要使用子进程来执行,它们已经和shell编译成一体,作为shell工 ...
- MySQL数据库事务详解
微信公众号[程序员江湖] 作者黄小斜,斜杠青年,某985硕士,阿里 Java 研发工程师,于 2018 年秋招拿到 BAT 头条.网易.滴滴等 8 个大厂 offer,目前致力于分享这几年的学习经验. ...
- 【原创】驱动枚举之EnumServicesStatusEx
BOOL WINAPI EnumServicesStatusEx( _In_ SC_HANDLE hSCManager, _In_ SC_ENUM_TYPE InfoLevel, _In_ DWORD ...
- PHP实现螺旋矩阵(螺旋数组)
今天碰到一个比较有意思的问题, 就是把A到Y这25个字母以下面的形式输出出来 A B C D E P Q R S F O X Y T G N W V U H M L K J I 问题很有意思,就是转圈 ...
- Go语言学习笔记(三) [控制结构、内建函数]
日期:2014年7月21日 一.控制结构 1.Go中,只有几个控制结构,它没有do或者while循环,有for,灵活的switch语句和if,在switch中可以接受像for那样可选的初始化语 ...
- [总结]数论和组合计数类数学相关(定理&证明&板子)
0 写在前面 0.0 前言 由于我太菜了,导致一些东西一学就忘,特开此文来记录下最让我头痛的数学相关问题. 一些引用的文字都注释了原文链接,若侵犯了您的权益,敬请告知:若文章中出现错误,也烦请告知. ...
- MySQL中间件之ProxySQL(3):Admin管理接口
返回ProxySQL系列文章:http://www.cnblogs.com/f-ck-need-u/p/7586194.html 1.ProxySQL的Admin管理接口 当ProxySQL启动后,将 ...
- Apollo 9 — adminService 主/灰度版本发布
目录 Controller 层 Service 层 publish 方法 发送 ReleaseMessage 消息 总结 1. Controller 层 主版本发布即点击主版本发布按钮: 具体接口位置 ...
- 【Java每日一题】20170209
20170208问题解析请点击今日问题下方的“[Java每日一题]20170209”查看(问题解析在公众号首发,公众号ID:weknow619) package Feb2017; public cla ...
- java中获取路径的方法
在class获取路径的方法,getResource有没有“\”的区别 System.out.println("" + this.getClass().getResource(&qu ...