Python中关于tkinter模块开发改名小工具的示例分享

这篇文章主要介绍了python基于tkinter模块实现的改名小工具,结合实例形式分析了tkinter模块操作文件后缀名的相关实现技巧,需要的朋友可以参考下

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

#!/usr/bin/env python#coding=utf-8# # 版权所有 2014 yao_yu# 本代码以MIT许可协议发布# 文件名批量加.xls后缀# 2014-04-21 创建# import osimport tkinter as tkfrom tkinter  import ttkversion = '2014-04-21'app_title = '文件名批量加后缀 Ver:' + versionlistdir = os.listdirisdir = os.path.isdirisfile = os.path.isfilepath_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.splitextfile_rename = os.renameknownexts = 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' % 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' % 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'] = 0if __name__ == '__main__':  root = tk.Tk()  root.title(app_title)  app = Application(root)  center_window(root, 600, 240)  tk.mainloop()

登录后复制

以上就是Python中关于tkinter模块开发改名小工具的示例分享的详细内容,更多请关注【创想鸟】其它相关文章!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至253000106@qq.com举报,一经查实,本站将立刻删除。

发布者:PHP中文网,转转请注明出处:https://www.chuangxiangniao.com/p/2267622.html

(0)
上一篇 2025年2月27日 10:11:23
下一篇 2025年2月19日 10:33:20

AD推荐 黄金广告位招租... 更多推荐

相关推荐

  • python中关于django对数据库Mysql的增删改查操作详解

    下面小编就为大家带来一篇python django 增删改查操作 数据库mysql。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧 下面介绍一下django增删改查操作: 1、view.py # -*- cod…

    编程技术 2025年2月27日
    200
  • python中可以使用两种方法实现更新数据库脚本的示例

    最近项目的两次版本迭代中,根据业务需求的变化,需要对数据库进行更新,两次分别使用了不同的方式进行更新。 第一种:使用python的MySQLdb模块利用原生的sql语句进行更新 import MySQLdb#主机名HOST = ‘127.0…

    编程技术 2025年2月27日
    200
  • Python开发之多个定时任务在单线程下执行的实例分析

    单线程多定时任务  1、初始版本: 思路:定时器,说白了就是延时执行指定的程序,目前自己重构python里面的定时器不太现实,能力达不到,所以延时操作时还得用到系统定时器,不过我们可以改一下规则;把所有要进行定时操作的程序添加到特定列表中,…

    2025年2月27日 编程技术
    200
  • 关于python方法无法在线程中使用的解决办法

      在python中,定义一个方法,直接调用可以,但是创建一个线程来调用就可能导致失败。这种现象多出现在使用com对象进行系统操作时,而且是以线程的形式调用。   异常提示如下: syntax error。WMI returned a sy…

    编程技术 2025年2月27日
    200
  • python中关于装饰器的学习

    定义:本质上就是个函数,(装饰器其他函数)就是为了给其他函数添加附加功能 原则:1.不能修改被装饰的函数的源代码            2.不能修改被装饰的函数的调用方式 import timedef timer(hello):    de…

    编程技术 2025年2月27日
    200
  • 如何在网上自学Python?

    现在的网络技术十分发达,足不出户就可以学习到很多自己感兴趣的知识。当然,我们学习python也一样可以在网上学习。下面我就来介绍一下有关我自己本身在网上自学python的经历。 我的学习网站是PHP中文网.虽然叫PHP中文网,但是网站上的知…

    2025年2月27日
    200
  • Python入门学习的流程分享

    所谓师傅领进门修行看个人,自己的勤奋非常重要,但是前提是要有个好的老师领进正确的门,不能带跑偏了。学习python也一样,入门很重要,下面我就分享下学习python的入门经验分享。 1.开发环境的搭建 http://www.php.cn/p…

    2025年2月27日
    200
  • Python基本语法大全

    1、python的对汉字的支持 #!/usr/bin/python#coding: UTF-8 print “你好,世界”; 2、Python标识符 在python里,标识符有字母、数字、下划线组成。 在python…

    编程技术 2025年2月27日
    200
  • Python中基本且又常用的算法

    这篇文章主要学习python常用算法,python常用排序算法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下 本节内容 算法定义时间复杂度空间复杂度常用算法实例 1.算法定义 算法(Algorithm)是指解题方案的准确而完整的描述,是…

    2025年2月27日 编程技术
    200
  • Python中的函数详解

    Python中的函数,无论是命名函数,还是匿名函数,都是语句和表达式的集合。在Python中,函数是第一个类对象,这意味着函数的用法并没有限制。Python函数的使用方式就像Python中其他值一样,例如字符串和数字等。Python函数拥有…

    编程技术 2025年2月27日
    200

发表回复

登录后才能评论