No more forcing, on the code
#-*-coding:utf-8-*-ImportOS fromTimeImportSleep fromTkinterImport*classDirlist (object):def __init__(Self, initdir=None):#First tag: Self.label, which is directory Lister v1.1Self.top =Tk () Self.label= Label (Self.top, text='Directory Lister v1.1') Self.label.pack ()#second tag: Self.dirl, which is the current file directory pathSELF.CWD = Stringvar (self.top)#CWD is a TK () variable used to track the name of the current directory, in string form? The value is not passed in nowSelf.dirl = Label (Self.top, fg='Blue', Font= ('Helvetica', 12,'Bold') ) Self.dirl.pack ()#defines the entire GUI program core, the body part, represented as a component of the framework (including list boxes and scrollbars)SELF.DIRFM = Frame (self.top)#framework components, pure containers, including other componentsSELF.DIRSB = Scrollbar (SELF.DIRFM)#scroll bar, where the scroll function is provided for the list boxSelf.dirsb.pack (Side=right, fill=y)#Place the list box on the right and fill the vertical directionSelf.dirs = Listbox (Self.dirfm, height=15, width=50, Yscrollcommand=self.dirsb.set)#list box, where the parameter is the parent component, the height, the width, and the vertical scrolling command, where the vertical scrolling command is set to scroll barSelf.dirs.bind ('<Double-1>', Self.setdirandgo)#The binding callback function Setdirandgo, but ' <Double-1> ' refers to invoking the callback function Setdirandgo () when the mouse double-clicks any item in the list box.Self.dirsb.config (Command=self.dirs.yview)#indicates that the scroll bar scrolls vertically in the list boxSelf.dirs.pack (Side=left, Fill=both)#The list box is placed on the left and fills the remaining space of the frame (BOTH)Self.dirfm.pack ()#define input box, collect keyboard inputSelf.dirn = Entry (Self.top, width=50, TEXTVARIABLE=SELF.CWD)#The textvariable parameter refers to the input, in this case the input file directory, the default value is the current file directorySelf.dirn.bind ('<Return>', Self.dols)#The binding callback function DoLS, but ' <Return> ' means that the user presses the ENTER key after the input box has finished the text and then calls the function DoLS ()Self.dirn.pack ()#defines a button frame with three buttonsSELF.BFM =Frame (self.top) self.clr= Button (SELF.BFM, text='Clear', Command=self.clrdir, activeforeground=' White', Activebackground='Blue')#"Clear" button, callback function is clear all files Clrdir ()Self.ls = Button (SELF.BFM, text='List Directory', Command=self.dols, activeforeground=' White', Activebackground='Green')#"Go" button, callback function is DoLS ()Self.quit = Button (SELF.BFM, text='Quit', Command=self.top.quit, activeforeground=' White', Activebackground='Red')#Exit buttonSelf.clr.pack (side=Left ) self.ls.pack (Side=Left ) self.quit.pack (Side=Left ) self.bfm.pack ()#initializes the GUI program, starting from the current directory, and does not understand. ifInitdir:self.cwd.set (Os.curdir) self.dols ()#CLR button callback function, empty TK string variable CWD defClrdir (Self, ev=None): Self.cwd.set ("') #list box callback function, set the directory to be reached, and call the DoLS () function defSetdirandgo (Self, ev=None): Check=Self.dirs.get (Self.dirs.curselection ())#The Get () Method of the list box is to get all the values in the list (no parameters passed in), to get the selected option in case of passing in the parameter (line number); Curselection () is the line number that returns the selected element if notCheck:check=os.curdir self.cwd.set (check)#Trace CWD to a directory in the list boxSelf.dols ()#The key to the entire GUI program, which is responsible for security checks, if there is no problem, call Os.listdir () to get a new collection of files and replace the list box list defDoLS (Self, ev=None):#Security ChecksError ="' #Error ZeroingTdir = Self.cwd.get ()#return CWD Tracking directory as a string if notTdir:tdir = Os.curdir#if empty, the Tdir is set to the current directory if notOs.path.exists (Tdir):#file does not existError = Tdir +': No such file' elif notOs.path.isdir (Tdir):#file path does not existError = Tdir +': Not a directory' #If there is an error, the final directory is set to the current directory ifError:self.cwd.set (Error)#set CWD to errorSelf.top.update ()#Refresh PageSleep (2) if not(Hasattr (Self,' Last') andself.last): Self.last=os.curdir Self.cwd.set (self.last)#Reset CWD to the current directorySelf.dirs.config (selectbackground='Lightskyblue') self.top.update ()#Refresh Page returnSelf.cwd.set ('fetching DIRECTORY CONTENTS ...') self.top.update () dirlist= Os.listdir (Tdir)#list all files under file directory TdirDirlist.sort ()#SortOs.chdir (Tdir)#set the current working directory to TdirSelf.dirl.config (TEXT=OS.GETCWD ())#configuration To set the second label content as the current working directorySelf.dirs.delete (0, END)#Delete the contents of the following table boxes in the old directorySelf.dirs.insert (END, Os.curdir)#Add the current directory at the end of the new directory list boxSelf.dirs.insert (END, Os.pardir)#at the end of the new directory list box, add the top level directory of the current directory forEachfileinchDirlist:#in the list box of the new directory, add all the files in the new directorySelf.dirs.insert (END, Eachfile) self.cwd.set (os.curdir) self.dirs.config (selectbackground='Lightskyblue')defMain (): D=dirlist (Os.curdir) mainloop ()if __name__=="__main__": Main ()
Graphical interface:
Python Intermediate tkinter sample file Manager