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的高级Git库 Gittle
Sep 22 Python
用Python代码来解图片迷宫的方法整理
Apr 02 Python
Python中每次处理一个字符的5种方法
May 21 Python
Python实现高斯函数的三维显示方法
Dec 29 Python
如何在django里上传csv文件并进行入库处理的方法
Jan 02 Python
对Xpath 获取子标签下所有文本的方法详解
Jan 02 Python
Django框架模板语言实例小结【变量,标签,过滤器,继承,html转义】
May 23 Python
python 使用matplotlib 实现从文件中读取x,y坐标的可视化方法
Jul 04 Python
VSCode基础使用与VSCode调试python程序入门的图文教程
Mar 30 Python
解决Jupyter因卸载重装导致的问题修复
Apr 10 Python
python空元组在all中返回结果详解
Dec 15 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中用PDO查询Mysql来避免SQL注入风险的方法
2013/04/25 PHP
PHP获取文件的MD5值并判断是否被修改的例子
2014/06/19 PHP
使用PHP编写发红包程序
2015/07/22 PHP
thinkPHP模型初始化实例分析
2015/12/03 PHP
PHP实现的猴王算法(猴子选大王)示例
2018/04/30 PHP
JS 分号引起的一段调试问题
2009/06/18 Javascript
JavaScript小技巧 2.5 则
2010/09/12 Javascript
jquery 根据name名获取元素的value值
2015/02/27 Javascript
Bootstrap基本组件学习笔记之input输入框组(9)
2016/12/07 Javascript
NodeJS使用七牛云存储上传文件的方法
2017/07/24 NodeJs
Angular2 组件间通过@Input @Output通讯示例
2017/08/24 Javascript
JavaScript实现修改伪类样式
2017/11/27 Javascript
微信小程序首页的分类功能和搜索功能的实现思路及代码详解
2018/09/11 Javascript
vue:el-input输入时限制输入的类型操作
2020/08/05 Javascript
vue3.0生命周期的示例代码
2020/09/24 Javascript
Python中暂存上传图片的方法
2015/02/18 Python
python图像处理之镜像实现方法
2015/05/30 Python
python ansible服务及剧本编写
2017/12/29 Python
python编写暴力破解zip文档程序的实例讲解
2018/04/24 Python
Python基于dom操作xml数据的方法示例
2018/05/12 Python
python与C、C++混编的四种方式(小结)
2019/07/15 Python
python自动化测试之DDT数据驱动的实现代码
2019/07/23 Python
浅谈python图片处理Image和skimage的区别
2019/08/04 Python
Python实现自动签到脚本的示例代码
2020/08/19 Python
Pycharm学生免费专业版安装教程的方法步骤
2020/09/24 Python
详解Python遍历列表时删除元素的正确做法
2021/01/07 Python
使用phonegap查找联系人的实现方法
2017/03/31 HTML / CSS
美国CVS药店官网:CVS Pharmacy
2018/07/26 全球购物
高中生学习总结的自我评价范文
2013/10/13 职场文书
先进个人事迹材料
2014/01/25 职场文书
运动会开幕式邀请函
2014/02/03 职场文书
小学教师读书活动总结
2014/07/08 职场文书
小学大队长竞选稿
2015/11/20 职场文书
2016年优秀共青团员事迹材料
2016/02/25 职场文书
vue3语法糖内的defineProps及defineEmits
2022/04/14 Vue.js
基于Android10渲染Surface的创建过程
2022/08/14 Java/Android