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中的赋值、浅拷贝、深拷贝介绍
Mar 09 Python
Python内置函数—vars的具体使用方法
Dec 04 Python
Python之文字转图片方法
May 10 Python
完美解决python中ndarray 默认用科学计数法显示的问题
Jul 14 Python
使用Python实现从各个子文件夹中复制指定文件的方法
Oct 25 Python
Python Matplotlib库安装与基本作图示例
Jan 09 Python
Python基于scipy实现信号滤波功能
May 08 Python
将数据集制作成VOC数据集格式的实例
Feb 17 Python
python实现猜拳游戏
Mar 04 Python
Python基于百度AI实现OCR文字识别
Apr 02 Python
Python 数据的累加与统计的示例代码
Aug 03 Python
Python基础进阶之海量表情包多线程爬虫功能的实现
Dec 17 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
php字符串截取的简单方法
2013/07/04 PHP
Ubuntu12下编译安装PHP5.3开发环境
2015/03/27 PHP
原生php实现excel文件读写的方法分析
2018/04/25 PHP
Javascript闭包用法实例分析
2015/01/23 Javascript
js判断浏览器类型及设备(移动页面开发)
2015/07/30 Javascript
深入理解js数组的sort排序
2016/05/28 Javascript
用AngularJS来实现监察表单按钮的禁用效果
2016/11/02 Javascript
快速实现jQuery多级菜单效果
2017/02/01 Javascript
详解windows下vue-cli及webpack 构建网站(二)导入bootstrap样式
2017/06/17 Javascript
Vuejs+vue-router打包+Nginx配置的实例
2018/09/20 Javascript
js实现图片放大并跟随鼠标移动特效
2019/01/18 Javascript
php结合js实现多条件组合查询
2019/05/28 Javascript
React学习之受控组件与数据共享实例分析
2020/01/06 Javascript
vue-cli创建的项目中的gitHooks原理解析
2020/02/14 Javascript
使用Python获取CPU、内存和硬盘等windowns系统信息的2个例子
2014/04/15 Python
python获取外网ip地址的方法总结
2015/07/02 Python
Python实现字符串的逆序 C++字符串逆序算法
2020/05/28 Python
python实现下载pop3邮件保存到本地
2018/06/19 Python
opencv python 基于KNN的手写体识别的实例
2018/08/03 Python
在PyCharm中批量查找及替换的方法
2019/01/20 Python
Python列表(List)知识点总结
2019/02/18 Python
Python二叉树的镜像转换实现方法示例
2019/03/06 Python
django如何实现视图重定向
2019/07/24 Python
详解python pandas 分组统计的方法
2019/07/30 Python
python 画函数曲线示例
2019/12/04 Python
Django模型中字段属性choice使用说明
2020/03/30 Python
通过实例了解python__slots__使用方法
2020/09/14 Python
matplotlib交互式数据光标实现(mplcursors)
2021/01/13 Python
基于CSS3制作立体效果导航菜单
2016/01/12 HTML / CSS
中专药剂专业应届毕的自我评价
2013/12/27 职场文书
工作中个人的自我评价
2013/12/31 职场文书
喜之郎果冻广告词
2014/03/20 职场文书
全运会口号
2014/06/20 职场文书
婚前协议书范本两则
2014/10/16 职场文书
2014年民政工作总结
2014/11/26 职场文书
高中语文教材(文学文化常识大全一)
2019/08/13 职场文书