不多逼逼,上代碼
# -*-coding: utf-8-*- import os from time import sleep from tkinter import * class DirList(object): def __init__(self, initdir=None): # 第一個標簽:self.label,就是Directory Lister v1.1 self.top = Tk() self.label = Label(self.top, text='Directory Lister v1.1') self.label.pack() # 第二個標簽:self.dirl,就是當前文件目錄路徑 self.cwd = StringVar(self.top) # cwd是Tk()變量,用來跟蹤當前所在目錄的名字,以字符串形式?現在並沒有將值傳入 self.dirl = Label(self.top, fg='blue', font=('Helvetica', 12, 'bold')) self.dirl.pack() # 定義整個GUI程序核心,即主體部分,用框架(包含列表框和滾動條)這一組件形式表現 self.dirfm = Frame(self.top) # 框架組件,純容器,包含其他組件 self.dirsb = Scrollbar(self.dirfm) # 滾動條,在這里是對列表框提供滾動功能 self.dirsb.pack(side=RIGHT, fill=Y) # 將列表框放置在右側,並且填滿豎直方向 self.dirs = Listbox(self.dirfm, height=15, width=50, yscrollcommand=self.dirsb.set) # 列表框,參數依次是父組件、高度、寬度以及豎直方向滾動命令,其中豎直方向滾動命令就設置為滾動條 self.dirs.bind('<Double-1>', self.setDirAndGo) # 綁定回調函數setDirAndGo,但是'<Double-1>'是指鼠標雙擊列表框中的任意一項內容時,調用回調函數setDirAndGo() self.dirsb.config(command=self.dirs.yview) # 表示滾動條對列表框進行豎直方向的滾動 self.dirs.pack(side=LEFT, fill=BOTH) # 列表框放置在左側,並填滿框架的剩余空間(BOTH) self.dirfm.pack() # 定義輸入框,收集鍵盤輸入 self.dirn = Entry(self.top, width=50, textvariable=self.cwd) # textvariable參數是指輸入的內容,在本例中是輸入文件目錄,默認值是當前文件目錄 self.dirn.bind('<Return>', self.doLS) # 綁定回調函數doLS,但是'<Return>'是指用戶在輸入框輸完文本后,按下回車鍵,就會調用函數doLS() self.dirn.pack() # 定義按鈕框架,包含三個按鈕 self.bfm = Frame(self.top) self.clr = Button(self.bfm, text='Clear', command=self.clrDir, activeforeground='white', activebackground='blue') # "clear"按鈕,回調函數是清楚所有文件clrDir() self.ls = Button(self.bfm, text='List Directory', command=self.doLS, activeforeground='white', activebackground='green') # "go"按鈕,回調函數是doLS() self.quit = Button(self.bfm, text='Quit', command=self.top.quit, activeforeground='white', activebackground='red') # 退出按鈕 self.clr.pack(side=LEFT) self.ls.pack(side=LEFT) self.quit.pack(side=LEFT) self.bfm.pack() # 初始化GUI程序,從當前目錄開始,不理解。 if initdir: self.cwd.set(os.curdir) self.doLS() # clr按鈕的回調函數,清空Tk字符串變量cwd def clrDir(self, ev=None): self.cwd.set('') # 列表框回調函數,設置了要達到的目錄,以及調用doLS()函數 def setDirAndGo(self, ev=None): check = self.dirs.get( self.dirs.curselection()) # 列表框的get()方法是得到列表中的所有值(未傳入參數),在傳入參數(行號)的情況下是獲得所選中的選項;curselection()是返回選中的元素的行號 if not check: check = os.curdir self.cwd.set(check) # 將cwd跟蹤至列表框中某項目錄 self.doLS() # 整個GUI程序的關鍵,負責安全檢查,若無問題,則調用os.listdir()取得新文件集合,並替換列表框列表 def doLS(self, ev=None): # 安全檢查 error = '' #error歸零 tdir = self.cwd.get() # 以字符串形式返回cwd追蹤目錄 if not tdir: tdir = os.curdir # 若為空,則tdir設為當前目錄 if not os.path.exists(tdir): # 文件不存在 error = tdir + ': no such file' elif not os.path.isdir(tdir): # 文件路徑不存在 error = tdir + ': not a directory' # 若有錯誤,則最終目錄設置為當前目錄 if error: self.cwd.set(error) # 將cwd設為error self.top.update() # 刷新頁面 sleep(2) if not (hasattr(self, 'last') and self.last): self.last = os.curdir self.cwd.set(self.last) # 重新設置cwd為當前目錄 self.dirs.config(selectbackground='LightSkyBlue') self.top.update() #刷新頁面 return self.cwd.set('FETCHING DIRECTORY CONTENTS...') self.top.update() dirlist = os.listdir(tdir) # 列出文件目錄tdir下所有文件 dirlist.sort() # 排序 os.chdir(tdir) # 將當前工作目錄設置為tdir self.dirl.config(text=os.getcwd()) # 配置,將第二個標簽內容定為當前工作目錄 self.dirs.delete(0, END) # 刪除舊目錄下列表框的內容 self.dirs.insert(END, os.curdir) # 在新目錄列表框的最后加入當前目錄 self.dirs.insert(END, os.pardir) # 在新目錄列表框的最后加入當前目錄的上一級目錄 for eachFile in dirlist: # 在新目錄的列表框中,加入新目錄下的所有文件 self.dirs.insert(END, eachFile) self.cwd.set(os.curdir) self.dirs.config(selectbackground='LightSkyBlue') def main(): d = DirList(os.curdir) mainloop() if __name__ == "__main__": main()
圖形界面: