tkinter的简单模型

# -*- coding: utf-8 -*-
import os
from tkinter import *
from tkinter import filedialog
import tkinter.messagebox


def main():
    # 选择文件夹
    def selectExcelfile():
        sfname = filedialog.askdirectory(title='选择文件夹')
        text1.insert(INSERT, sfname)  # 存入
    #  关闭程序
    def closeThisWindow():
        root.destroy()
	# 处理函数
    def doProcess():
        tkinter.messagebox.showinfo('提示', '处理文件的示例程序。')
        path_ = text1.get()  # 取出文件路径
        # print(path_)
        try:
            os.chdir(path_)   # 切换路径
            # print(os.getcwd()) 获取当前文件路径
        except:
            path_ = text1.get().replace('/', '\\')
            os.chdir(path_)
        print(os.listdir(path_))
        filename_list = os.listdir(path_)  # 扫描目标路径的文件,将文件名存入列表
        for f in filename_list:
            if '.xls' in f:
                continue
            used_name = f
            f_list = f.split('.')[0].split('~')
            new_name = f_list[0] + '.mp4'
            # new_name = f.split('.')[0] + ' ' + str(int(f_list[0]) + 85) + '讲' + '.mp4'
            print()
            try:
                os.rename(used_name, new_name)
                print("文件%s重命名成功,新的文件名为%s" % (used_name, new_name))
            except Exception as e:
                print(e)
            else:
                continue
                
	# 清空输入框
    def clraeThisWindow():
        text1.delete(0, END)

    # 初始化
    root = Tk()
    # 设置窗体标题
    root.title('Python GUI Learning')
    # 设置窗口大小和位置
    root.geometry('500x300+570+200')

    label1 = Label(root, text='请选择文件:')
    text1 = Entry(root, bg='white', width=45)
    button1 = Button(root, text='浏览', width=8, command=selectExcelfile)
    button2 = Button(root, text='处理', width=8, command=doProcess)
    button3 = Button(root, text='退出', width=8, command=closeThisWindow)
    button4 = Button(root, text='清空', width=8, command=clraeThisWindow)

    label1.pack()
    text1.pack()
    button1.pack()
    button2.pack()
    button3.pack()

    label1.place(x=30, y=30)
    text1.place(x=100, y=30)
    button1.place(x=390, y=26)
    button2.place(x=150, y=80)
    button3.place(x=250, y=80)
    button4.place(x=350, y=80)

    root.mainloop()


if __name__ == "__main__":
    main()

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值