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 16 Python
python中常用检测字符串相关函数汇总
Apr 15 Python
Python实现列表转换成字典数据结构的方法
Mar 11 Python
Python元字符的用法实例解析
Jan 17 Python
Python简单计算文件MD5值的方法示例
Apr 11 Python
pandas数值计算与排序方法
Apr 12 Python
python与caffe改变通道顺序的方法
Aug 04 Python
Python图像处理之图像的缩放、旋转与翻转实现方法示例
Jan 04 Python
selenium+python自动化测试之环境搭建
Jan 23 Python
python保存字典和读取字典的实例代码
Jul 07 Python
详解Python3中的 input() 函数
Mar 18 Python
Python计算信息熵实例
Jun 18 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
Zend的MVC机制使用分析(一)
2013/05/02 PHP
destoon实现会员商铺中指定会员或会员组投放广告的方法
2014/08/21 PHP
PHP类的反射用法实例
2014/11/03 PHP
PHP图像处理类库MagickWand用法实例分析
2015/05/21 PHP
PHP引用返回用法示例
2016/05/28 PHP
thinkPHP中钩子的两种配置调用方法详解
2016/11/11 PHP
Yii2语言国际化自动配置详解
2018/08/22 PHP
JS控制文本框textarea输入字数限制的方法
2013/06/17 Javascript
JS实现的新浪微博大厅文字内容滚动效果代码
2015/11/05 Javascript
jQuery模拟物体自由落体运动(附演示与demo源码下载)
2016/01/21 Javascript
JS获取当前脚本文件的绝对路径
2016/03/02 Javascript
JavaScript将DOM事件处理程序封装为event.js 出现的低级错误问题
2016/08/03 Javascript
node.js平台下的mysql数据库配置及连接
2017/03/31 Javascript
JavaScript实现简单图片轮播效果
2017/08/21 Javascript
vue动态绑定class选中当前列表变色的方法示例
2018/12/19 Javascript
jQuery zTree插件使用简单教程
2019/08/16 jQuery
在vue中使用echars实现上浮与下钻效果
2019/11/08 Javascript
jquery实现烟花效果(面向对象)
2020/03/10 jQuery
Nodejs文件上传、监听上传进度的代码
2020/03/27 NodeJs
JavaScript常用工具函数汇总(浏览器环境)
2020/09/17 Javascript
[14:20]刀塔大凶女神互压各路奇葩屌丝
2014/05/16 DOTA
用python实现批量重命名文件的代码
2012/05/25 Python
Python使用scrapy采集数据过程中放回下载过大页面的方法
2015/04/08 Python
Python实现截屏的函数
2015/07/25 Python
Python基于dom操作xml数据的方法示例
2018/05/12 Python
python 读取目录下csv文件并绘制曲线v111的方法
2018/07/06 Python
python  创建一个保留重复值的列表的补码
2018/10/15 Python
Python subprocess库的使用详解
2018/10/26 Python
numpy库与pandas库axis=0,axis= 1轴的用法详解
2019/05/27 Python
预订旅游活动、景点和旅游:GetYourGuide
2019/09/29 全球购物
腾讯技术类校园招聘笔试试题
2014/05/06 面试题
民主评议教师党员自我评价
2015/03/04 职场文书
小学英语听课心得体会
2016/01/14 职场文书
2016教师六五普法学习心得体会
2016/01/21 职场文书
深入浅析python3 依赖倒置原则(示例代码)
2021/07/09 Python
《模拟人生4》推出新补丁 “婚礼奇缘”DLC终于得到修复
2022/04/03 其他游戏