Python GUI编程完整示例


Posted in Python onApril 04, 2019

本文实例讲述了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程序设计有所帮助。

Python 相关文章推荐
Python中声明只包含一个元素的元组数据方法
Aug 25 Python
Python中join和split用法实例
Apr 14 Python
python使用wmi模块获取windows下硬盘信息的方法
May 15 Python
python中管道用法入门实例
Jun 04 Python
简单掌握Python中glob模块查找文件路径的用法
Jul 05 Python
关于Python中浮点数精度处理的技巧总结
Aug 10 Python
Python设计模式之模板方法模式实例详解
Jan 17 Python
python 字典访问的三种方法小结
Dec 05 Python
Python输出指定字符串的方法
Feb 06 Python
opencv中图像叠加/图像融合/按位操作的实现
Apr 01 Python
Python virtualenv虚拟环境实现过程解析
Apr 18 Python
基于python获取本地时间并转换时间戳和日期格式
Oct 27 Python
Python使用sax模块解析XML文件示例
Apr 04 #Python
详解小白之KMP算法及python实现
Apr 04 #Python
Python魔法方法功能与用法简介
Apr 04 #Python
详解pandas.DataFrame中删除包涵特定字符串所在的行
Apr 04 #Python
pandas删除指定行详解
Apr 04 #Python
详解python之heapq模块及排序操作
Apr 04 #Python
python实现kmp算法的实例代码
Apr 03 #Python
You might like
PDO防注入原理分析以及注意事项
2015/02/25 PHP
Win7下手动安装apache2.2、php5.4笔记
2015/04/03 PHP
PHP中类的继承和用法实例分析
2016/05/24 PHP
示例详解Laravel重置密码代码重构
2016/08/10 PHP
laravel5.4生成验证码的实例讲解
2017/08/05 PHP
javascript json2 使用方法
2010/03/16 Javascript
JS判断数组中是否有重复值得三种实用方法
2013/08/16 Javascript
判断iframe里的页面是否加载完成
2014/06/06 Javascript
js实现鼠标感应图片展示的方法
2015/02/27 Javascript
谈谈我对JavaScript原型和闭包系列理解(随手笔记6)
2015/12/20 Javascript
AngularJS入门心得之directive和controller通信过程
2016/01/25 Javascript
用canvas 实现个图片三角化(LOW POLY)效果
2016/02/18 Javascript
JQuery+EasyUI轻松实现步骤条效果
2016/02/22 Javascript
node.js使用stream模块实现自定义流示例
2020/02/13 Javascript
[04:59]2018DOTA2亚洲邀请赛 4.7 Mineski夺冠时刻
2018/04/09 DOTA
[02:03]完美世界DOTA2联赛10月30日赛事集锦
2020/10/31 DOTA
python解析xml文件实例分析
2015/05/27 Python
Python使用add_subplot与subplot画子图操作示例
2018/06/01 Python
python通过Windows下远程控制Linux系统
2018/06/20 Python
对Python3中bytes和HexStr之间的转换详解
2018/12/04 Python
Python Opencv提取图片中某种颜色组成的图形的方法
2019/09/19 Python
python读取ini配置的类封装代码实例
2020/01/08 Python
使用keras根据层名称来初始化网络
2020/05/21 Python
python和opencv构建运动检测器的实现
2021/03/03 Python
汽车检测与维修应届毕业生求职信
2013/10/19 职场文书
银行工作检查书范文
2014/01/31 职场文书
元宵晚会主持词
2014/03/25 职场文书
《鹬蚌相争》教学反思
2014/04/22 职场文书
运动会演讲稿300字
2014/08/25 职场文书
教师节横幅标语
2014/10/08 职场文书
小学生读书笔记范文
2015/06/30 职场文书
三八妇女节新闻稿
2015/07/17 职场文书
赞助商致辞
2015/07/30 职场文书
国庆节主题班会
2015/08/15 职场文书
使用jpa之动态插入与修改(重写save)
2021/11/23 Java/Android
IIS服务器中设置HTTP重定向访问HTTPS
2022/04/29 Servers