# -*- coding:utf-8 -*-
#author:
#time: 2020/8/24
import tkinter as tk
from tkinter import filedialog
from tkinter import ttk
import os
import cv2 as cv
from PIL import Image
#Iterate over the folders
def iter_files(rootDir, extend_name):
file_list = []
#Iterate over the roots
for root,dirs,files in os.walk(rootDir):
for file in files:
root = os.path.normpath(root) #格式化路径 斜杠问题
file_name = os.path.join(root,file) # 连接路径
flag = file_name.endswith(extend_name)
if flag:
file_list.append(file_name)
return file_list
def select_floder():
path = filedialog.askdirectory()
var.set(path)
def save_floder():
path1 = filedialog.askdirectory()
var1.set(path1)
window = tk.Tk()
window.title('my')
window.geometry('600x400')
var = tk.StringVar()
var1 = tk.StringVar()
# create path entry
e_path = tk.Entry(window, textvariable = var, width = 50)
e_path.place(x = 50, y = 10)
e_save_path = tk.Entry(window, textvariable = var1, width = 50)
e_save_path.place(x = 50, y = 50)
path_label = tk.Label(window, text = 'path:',bg = 'blue',width = 5)
path_label.place(x = 10, y = 10)
save_path_label = tk.Label(window, text = 'spath:',bg = 'red',width = 5)
save_path_label.place(x = 10, y = 50)
#create a combobox
val = tk.StringVar()
cmb = ttk.Combobox(window, textvariable = val, state = 'readonly' )
cmb['values'] = ('png','jpg','bmp','svg')
cmb.current(0)
cmb.place(x = 400, y = 10)
#combobox is vacant, oddness
b_select = tk.Button(window, text = 'select folder', command = select_floder,width = 20)
b_select.place(x = 300, y = 10)
b_save = tk.Button(window, text = 'save folder', command = save_floder,width = 20)
b_save.place(x = 300, y = 50)
t = tk.Text(window, height = 5)
t.place(x = 10, y = 150)
def process():
var2 = e_path.get()
ex_name = cmb.get()
val.set(ex_name)
file_list = iter_files(var2, ex_name)
for name in file_list:
t.insert('end',name)
t.insert(tk.INSERT, '\n') #增加换行
b_process = tk.Button(window, text = 'process', command = process, width = 20)
b_process.place(x = 50, y = 100)
window.mainloop()
每天学一点python——GUI遍历文件夹
最新推荐文章于 2024-06-15 17:36:30 发布