Python基于tkinter模块实现的改名小工具示例

 更新时间:2017年07月27日 09:54:08   作者:一花一世界,一叶一乾坤  
这篇文章主要介绍了Python基于tkinter模块实现的改名小工具,结合实例形式分析了tkinter模块操作文件后缀名的相关实现技巧,需要的朋友可以参考下

本文实例讲述了Python基于tkinter模块实现的改名小工具。分享给大家供大家参考,具体如下:

#!/usr/bin/env python
#coding=utf-8
# 
# 版权所有 2014 yao_yu
# 本代码以MIT许可协议发布
# 文件名批量加.xls后缀
# 2014-04-21 创建
# 
import os
import tkinter as tk
from tkinter  import ttk
version = '2014-04-21'
app_title = '文件名批量加后缀 Ver:' + version
listdir = os.listdir
isdir = os.path.isdir
isfile = os.path.isfile
path_join = os.path.join
#---------------------------- Object Visit  ----------------------------#
def visit_directory_files(root, visitor):
  for i in listdir(root):
    i = path_join(root, i)
    if isdir(i):
      if visit_directory_files(i, visitor):
        return True
    elif isfile(i):
      if visitor(i):
        return True
#----------------------------  Visitor  ----------------------------#
class ListVisitor(object):
  def __init__(self, *visitors, terminate = True):
    if (visitors 
         and isinstance(visitors, (list, tuple)) 
         and isinstance(visitors[0], (list, tuple))):
      visitors = visitors[0]
    self._visitors = list(visitors)
    self._terminate = terminate
  def __call__(self, *args, **kwdargs):
    for visitor in self._visitors:
      if visitor(*args, **kwdargs):
        return self._terminate
  def append(self, visitor):
    assert(visitor)
    self._visitors.append(visitor)
def get_screen_size(window):
  return window.winfo_screenwidth(),window.winfo_screenheight()
def get_window_size(window):
  return window.winfo_reqwidth(),window.winfo_reqheight()
def center_window(root, width, height):
  screenwidth = root.winfo_screenwidth()
  screenheight = root.winfo_screenheight()
  size = '%dx%d+%d+%d' % (width, height, (screenwidth - width)/2, (screenheight - height)/2)
  root.geometry(size)
class Application(object):
  def __init__(self, master):
    self.master = ttk.Frame(master)
    self.master.pack(side = tk.TOP, expand = tk.YES, fill = tk.BOTH)
    self.create_widgets()
  def create_widgets(self):
    master = self.master
    master.columnconfigure(1, weight=1)
    master.rowconfigure(2, weight=1)
    self.targetdir = tk.StringVar()
    self.targetdir.set('/Volumes/Data/Document/Test')
    padx = 5
    pady = 10
    ttk.Label(master, text="操作目录").grid(row = 0, column = 0, stick = tk.E, padx = padx, pady = pady)
    ttk.Entry(master, textvariable = self.targetdir).grid(row = 0, column = 1, stick = tk.EW, padx = padx)
    commands = ttk.Frame(master)
    commands.grid(row = 1, column = 0, columnspan = 2)
    ttk.Button(commands, text="开始", command = self.onStart).pack(side = tk.LEFT)
    ttk.Button(commands, text="退出", command = master.quit).pack(side = tk.LEFT)
    self.status = tk.StringVar()
    self.status.set('就绪')
    master.rowconfigure(3, minsize = 160)
    ttk.Label(master, textvariable = self.status, wraplength=600).grid(row = 3, column = 0, columnspan = 2, padx = padx, stick = tk.NSEW)
    self.progress = ttk.Progressbar(master, maximum=100, orient=tk.HORIZONTAL, mode='determinate')
    self.progress.grid(row = 4, column = 0, columnspan = 2, padx = padx, stick = tk.NSEW)
  def onStart(self):
    targetdir = self.targetdir.get().strip()
    basename = os.path.basename(targetdir)
    if os.path.isdir(targetdir):
      listvisitor = ListVisitor(ProgressVisitor(self.progress),
                    self.StatusVisitor(),
                    FileLogVisitor(basename),
                    #FileRenameVisitor(basename),
                    )
      visit_directory_files(targetdir, listvisitor)
    else:
      self.status.set('目标目录不存在')
  def StatusVisitor(self):
    print_status = self.status.set
    def __call__(file):
      __call__.n += 1
      print_status('%s,%s' % (__call__.n, file))
    __call__.n = 0
    return __call__
splitext = os.path.splitext
file_rename = os.rename
knownexts = dict.fromkeys(['.jpg', '.log', '.pdf', '.tif', '.xls', '.zip', '.rar'])
class FileRenameVisitor(object):
  def __init__(self, file):
    self.__fp = open('%s_%s_rename.txt' % (os.path.splitext(__file__)[0], file), 'w')
  def __call__(self, file):
    ext = splitext(file)[1].lower()
    if ext not in knownexts:
      file_rename(file, file + '.xls')
      self.__fp.write('%s\n' % file)
  def __del__(self):
    self.__fp.close()
class FileLogVisitor(object):
  def __init__(self, file):
    self.__fp = open('%s_%s_all.txt' % (os.path.splitext(__file__)[0], file), 'w')
  def __call__(self, file):
    self.__fp.write('%s\n' % file)
  def __del__(self):
    self.__fp.close()
class ProgressVisitor(object):
  COUNT = 202
  def __init__(self, progress, count=COUNT):
    self.progress = progress
    if count and isinstance(count, int) and count > 0:
      self.count = count
    else:
      self.count = self.COUNT
    self.n = 1
  def __call__(self, *args, **kwdargs):
    self.n += 1
    if self.n == self.count:
      self.progress.step()
      self.progress.update()
      self.n = 1
  def __del__(self):
    self.progress['value'] = 0
if __name__ == '__main__':
  root = tk.Tk()
  root.title(app_title)
  app = Application(root)
  center_window(root, 600, 240)
  tk.mainloop()

更多关于Python相关内容感兴趣的读者可查看本站专题:《Python编码操作技巧总结》、《Python数据结构与算法教程》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》及《Python入门与进阶经典教程

希望本文所述对大家Python程序设计有所帮助。

相关文章

  • python聚类算法解决方案(rest接口/mpp数据库/json数据/下载图片及数据)

    python聚类算法解决方案(rest接口/mpp数据库/json数据/下载图片及数据)

    这篇文章主要介绍了python聚类算法解决方案(rest接口/mpp数据库/json数据/下载图片及数据),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-08-08
  • Flask框架通过Flask_login实现用户登录功能示例

    Flask框架通过Flask_login实现用户登录功能示例

    这篇文章主要介绍了Flask框架通过Flask_login实现用户登录功能,结合实例形式较为详细的分析了flask框架使用Flask_login实现用户登陆功能的具体操作步骤、相关实现技巧与操作注意事项,需要的朋友可以参考下
    2018-07-07
  • Windows平台Python编程必会模块之pywin32介绍

    Windows平台Python编程必会模块之pywin32介绍

    在Windows平台上,从原来使用C/C++编写原生EXE程序,到使用Python编写一些常用脚本程序,成熟的模块的使用使得编程效率大大提高了
    2019-10-10
  • Python 数据库操作 SQLAlchemy的示例代码

    Python 数据库操作 SQLAlchemy的示例代码

    这篇文章主要介绍了Python 数据库操作 SQLAlchemy的示例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2019-02-02
  • python实现21点小游戏

    python实现21点小游戏

    这篇文章主要为大家详细介绍了python实现21点小游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-04-04
  • 使用Python实现管理系统附源码

    使用Python实现管理系统附源码

    这篇文章主要为大家介绍了Python实现管理系统,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,希望能够给你带来帮助
    2022-01-01
  • Python实现仿真双径效应的方法

    Python实现仿真双径效应的方法

    双径模型是一种很好的近似,能够准确地反映信号的传播特性。这篇文章主要介绍了Python实现仿真双径效应的方法,感兴趣的小伙伴们可以参考一下
    2021-05-05
  • python mysql 字段与关键字冲突的解决方式

    python mysql 字段与关键字冲突的解决方式

    这篇文章主要介绍了python mysql 字段与关键字冲突的解决方式,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-03-03
  • python 如何利用argparse解析命令行参数

    python 如何利用argparse解析命令行参数

    这篇文章主要介绍了python 利用argparse解析命令行参数的步骤,帮助大家更好的理解和学习python,感兴趣的朋友可以了解下
    2020-09-09
  • Python利用 matplotlib 绘制直方图

    Python利用 matplotlib 绘制直方图

    这篇文章主要介绍了Python利用 matplotlib 模块绘制直方图相关属性和方法,具有一定的参考价值,需要的朋友可以参考一下
    2021-12-12

最新评论