python - How to thread a application? -
i'll referring python in question. ok, let's have script renames 1000 files.how multithread it? thought dividing files chunks makes code complex, , run 1000 thread, 1 each file, isn't going work.
here code.
import os import tkinter.messagebox import tkinter.ttk import tkinter.filedialog tkinter import tk, stringvar, text, end print(""" little reminder! file gets renamed can't undo,so don't screw around this. note : careful extension,you might lose files way. """) class rename: def __init__(self,path): self.p = path def rename(self): try: files = os.listdir(self.p) = 0 os.chdir(self.p) while <= len(files): ext = files[i].split(".")[-1] #you can change renaming variable,for example may add "file{}.{}".format(i,ext).. #also,don't play ext if don't know you're doing.. os.rename(files[i],"{}.{}".format(i,ext)) += 1 yield "now renaming {}".format(files[i]) except oserror e: print("error -> {}".format(e)) except indexerror: pass class gui: def __init__(self): self.root = tk() self.root.wm_title("filerenamer") self.root.config(background = "black") # label self.label = tkinter.ttk.label(self.root, text = "path: ") self.label.config(foreground = "white", background = "black", font = ("arial", 16)) self.label.grid(row = 0, column = 0) # file dialog self.path = stringvar() self.filepath = tkinter.ttk.button(self.root, text = "choose", command = self.askpath).grid(row = 0, column = 1) # textwidget self.text = text(self.root, height = 5, width = 50) self.text.config(foreground = "white", background = "black", font = ("arial", 12)) self.text.grid(row = 3, column = 0, columnspan = 2) # button self.button = tkinter.ttk.button(self.root, text = "rename", command = self.rename) self.button.grid(row = 2, column = 1, columnspan = 2) self.root.mainloop() def askpath(self): self.path = tkinter.filedialog.askdirectory() def rename(self): path = os.path.abspath(self.path) rem = rename(path) im in rem.rename(): self.text.insert(end, "{}\n".format(im)) # make sure add "\" path enter, otherwise you'll error. r = gui()
Comments
Post a Comment