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 linecache.getline()读取文件中特定一行的脚本
Sep 06 Python
Python中不同进制互相转换(二进制、八进制、十进制和十六进制)
Apr 05 Python
python 信息同时输出到控制台与文件的实例讲解
May 11 Python
python+unittest+requests实现接口自动化的方法
Nov 29 Python
详解Python网络框架Django和Scrapy安装指南
Apr 01 Python
Django数据库类库MySQLdb使用详解
Apr 28 Python
PyCharm+Qt Designer+PyUIC安装配置教程详解
Jun 13 Python
python设计微型小说网站(基于Django+Bootstrap框架)
Jul 08 Python
Django如何简单快速实现PUT、DELETE方法
Jul 24 Python
深入了解Django中间件及其方法
Jul 26 Python
给ubuntu18安装python3.7的详细教程
Jun 08 Python
Pytest中skip和skipif的具体使用方法
Jun 30 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实现文件与16进制相互转换的方法示例
2017/02/16 PHP
javascript据option的value值快速设定初始的selected选项
2007/08/13 Javascript
用JavaScript显示随机图像或引用
2009/04/21 Javascript
JS常见问题整理(持续更新)
2013/08/06 Javascript
获取3个数组不重复的值的具体实现
2013/12/30 Javascript
js调用iframe实现打印页面内容的方法
2014/03/04 Javascript
JavaScript DOM节点添加示例
2014/07/16 Javascript
JS验证字符串功能
2017/02/22 Javascript
ES6新数据结构Map功能与用法示例
2017/03/31 Javascript
Vue 兄弟组件通信的方法(不使用Vuex)
2017/10/26 Javascript
React全家桶环境搭建过程详解
2018/05/18 Javascript
angular1.x ui-route传参的三种写法小结
2018/08/31 Javascript
深入浅析vue-cli@3.0 使用及配置说明
2019/05/08 Javascript
selenium 反爬虫之跳过淘宝滑块验证功能的实现代码
2020/08/27 Javascript
python3.3教程之模拟百度登陆代码分享
2014/01/16 Python
总结用Pdb库调试Python的方式及常用的命令
2016/08/18 Python
神经网络(BP)算法Python实现及应用
2018/04/16 Python
Python基于Floyd算法求解最短路径距离问题实例详解
2018/05/16 Python
Python实现读取机器硬件信息的方法示例
2018/06/09 Python
python中多个装饰器的执行顺序详解
2018/10/08 Python
使用python判断你是青少年还是老年人
2018/11/29 Python
python 画三维图像 曲面图和散点图的示例
2018/12/29 Python
Python给图像添加噪声具体操作
2019/03/03 Python
Python multiprocess pool模块报错pickling error问题解决方法分析
2019/03/20 Python
Python玩转加密的技巧【推荐】
2019/05/13 Python
40个你可能不知道的Python技巧附代码
2020/01/29 Python
python实现飞行棋游戏
2020/02/05 Python
Rockport乐步美国官网:风靡美国的白宫鞋
2016/11/24 全球购物
韩国美国时尚服装和美容在线全球市场:KOODING
2018/11/07 全球购物
工商管理本科毕业生求职信范文
2013/10/05 职场文书
大学毕业生自荐书怎么写?
2014/01/06 职场文书
计算机专业毕业生求职信
2014/04/30 职场文书
中学生检讨书范文
2014/11/03 职场文书
农村婚庆主持词
2015/06/29 职场文书
投诉信格式范文
2015/07/02 职场文书
ORACLE中dbms_output.put_line输出问题的解决过程
2022/06/28 Oracle