# -*- 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()
tkinter的简单模型
最新推荐文章于 2021-08-07 14:20:55 发布