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 相关文章推荐
sqlalchemy对象转dict的示例
Apr 22 Python
Django框架中方法的访问和查找
Jul 15 Python
遍历python字典几种方法总结(推荐)
Sep 11 Python
python中使用正则表达式的连接符示例代码
Oct 10 Python
Python实现拷贝/删除文件夹的方法详解
Aug 29 Python
深入浅析Python中list的复制及深拷贝与浅拷贝
Sep 03 Python
Pycharm+Scrapy安装并且初始化项目的方法
Jan 15 Python
python中import与from方法总结(推荐)
Mar 21 Python
Python socket模块实现的udp通信功能示例
Apr 10 Python
Python中如何引入第三方模块
May 27 Python
在keras里面实现计算f1-score的代码
Jun 15 Python
Python使用openpyxl模块处理Excel文件
Jun 05 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 SQL防注入代码集合
2008/04/25 PHP
php学习之 数组声明
2011/06/09 PHP
PHP数据库调用类调用实例(详细注释)
2012/07/12 PHP
一个严格的PHP Session会话超时时间设置方法
2014/06/10 PHP
ThinkPHP框架整合微信支付之刷卡模式图文详解
2019/04/10 PHP
PNGHandler-借助JS让PNG图在IE下实现透明(包括背景图)
2007/08/31 Javascript
JS支持带x身份证号码验证函数
2008/08/10 Javascript
JavaScript 字符串连接性能优化
2008/12/20 Javascript
当jQuery遭遇CoffeeScript的时候 使用分享
2011/09/17 Javascript
JSON.stringify转换JSON时日期时间不准确的解决方法
2014/08/08 Javascript
nodejs实现的简单web服务器功能示例
2018/03/15 NodeJs
Angular Renderer (渲染器)的具体使用
2018/05/03 Javascript
Javascript实现异步编程的过程
2018/06/18 Javascript
通过jQuery学习js类型判断的技巧
2019/05/27 jQuery
浅谈目前可以使用ES10的5个新特性
2019/06/25 Javascript
IE11下处理Promise及Vue的单项数据流问题
2019/07/24 Javascript
将Python代码嵌入C++程序进行编写的实例
2015/07/31 Python
简要讲解Python编程中线程的创建与锁的使用
2016/02/28 Python
浅谈python抛出异常、自定义异常, 传递异常
2016/06/20 Python
Python判断文件和字符串编码类型的实例
2017/12/21 Python
pyqt5实现绘制ui,列表窗口,滚动窗口显示图片的方法
2019/06/20 Python
Python使用matplotlib 模块scatter方法画散点图示例
2019/09/27 Python
使用pandas 将DataFrame转化成dict
2019/12/10 Python
详解CSS的border边框属性及其在CSS3中的新特性
2016/05/10 HTML / CSS
CSS3 :nth-child()伪类选择器实现奇偶行显示不同样式
2013/11/05 HTML / CSS
移动web模拟客户端实现多方框输入密码效果【附代码】
2016/03/25 HTML / CSS
Data URI scheme详解和使用实例及图片base64编码实现方法
2014/05/08 HTML / CSS
Daniel Wellington官方海外旗舰店:丹尼尔惠灵顿DW手表
2018/02/22 全球购物
美国领先的家庭智能音响系统品牌:Sonos
2018/07/20 全球购物
学习心得体会
2014/01/01 职场文书
小学防溺水制度
2014/01/29 职场文书
毕业生欢送会主持词
2014/03/31 职场文书
行政求职信
2014/07/04 职场文书
2014年学校总务处工作总结
2014/12/08 职场文书
带你彻底理解JavaScript中的原型对象
2021/04/14 Javascript
Python下opencv库的安装过程及问题汇总
2021/06/11 Python