Tkinter示例:文件系統(tǒng)遍歷GUI

1 示例簡(jiǎn)介

目錄樹遍歷工具。

它會(huì)從當(dāng)前目錄開始,提供一個(gè)文件列表。

雙擊列表中任意其他目錄,就會(huì)使得工具切換到新目錄中,用新目錄中的文件列表代替舊文件列表。

2 實(shí)現(xiàn)方法

獲取目錄;顯示目錄;跳轉(zhuǎn)目錄;獲取目錄;顯示目錄。

2.1 獲取目錄——核心函數(shù):os.listdir(tdir)

這個(gè)示例的核心是要將某個(gè)路徑下的文件顯示出來,那么首先就要想辦法獲取這些文件,這里的關(guān)鍵函數(shù)就是:os.listdir(tdir),他可以獲得某個(gè)路徑tdir下的所有文件。然后,再考慮怎么在圖形界面進(jìn)行顯示。


2.2 顯示目錄——核心函數(shù):Listbox.insert()

Paste_Image.png

self.dirs.insert(END,os.curdir)中,END是要插入的元素的位置,可以理解為數(shù)組的末尾。

其中,dirs是Listbox類型,98-99兩行在dirs中添加了當(dāng)前目錄‘.’和上級(jí)目錄‘..’。

我是怎么查到dirs的size()和get()方法的呢?

在源文件Tkinter.py中搜索關(guān)鍵詞“Listbox”可以查看他的所有方法:


Paste_Image.png

運(yùn)行到100行,給dirs中添加了,當(dāng)前目錄‘.’和上級(jí)目錄‘..’:

Paste_Image.png

繼續(xù)執(zhí)行,會(huì)發(fā)現(xiàn)當(dāng)前目錄下的文件列表被添加進(jìn)去了:

Paste_Image.png

2.3 跳轉(zhuǎn)目錄——核心函數(shù):

當(dāng)雙擊某一個(gè)文件時(shí),能進(jìn)入那個(gè)文件,發(fā)揮作用的函數(shù)是setDirAndGo

self.dirs.curselection()可以獲取當(dāng)前選取的文件名。

3 源代碼

#!/usr/bin/env python
# coding=utf-8

import pdb
import os
from time import sleep
from Tkinter import *

class DirList(object):
    def __init__(self, initdir=None):
        #pdb.set_trace()
        self.top = Tk()
        self.label = Label(self.top, text='Directory Lister v1.1')
        self.label.pack()

        self.cwd = StringVar(self.top)

        self.dirl = Label(self.top,fg='blue',font=('Helvetica',12,'bold'))
        self.dirl.pack()

        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)
        self.dirsb.config(command=self.dirs.yview)
        self.dirs.pack(side=LEFT,fill=BOTH)
        self.dirfm.pack()

        self.dirn = Entry(self.top,width=50,textvariable=self.cwd)
        self.dirn.bind('<Return>',self.doLS)
        self.dirn.pack()

        self.bfm = Frame(self.top)
        self.clr = Button(self.bfm,text='Clear',
            comman=self.clrDir,
            activeforeground='white',
            activebackground='blue')
        self.ls = Button(self.bfm,
            text='List Directory',
            command=self.doLS,
            activeforeground='white',
            activebackground='green')
        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()

        if initdir:
            self.cwd.set(os.curdir)
            self.doLS()

    def clrDir(self, ev=None):
        self.cwd.set('')

    def setDirAndGo(self,ev=None):
        self.last=self.cwd.get()
        self.dirs.config(selectbackground='red')
        check = self.dirs.get(self.dirs.curselection())
        if not check:
            check=os.curdir
        self.cwd.set(check)
        self.doLS()

    def doLS(self,ev=None):
        error=''
        tdir = self.cwd.get()
        if not tdir: tdir=os.curdir

        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)
            self.top.update()
            sleep(2)
            if not (hasattr(self,'last')\
               and self.last):
               self.last = os.curdir
            self.cwd.set(self.last)
            self.dirs.config(selectbackground='LightSkyBlue')
            self.top.update()
            return
        self.cwd.set('FETCHING DIRECTORY CONTENTS...')
        self.top.update()
        dirlist = os.listdir(tdir)
        dirlist.sort()
        os.chdir(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__':
    pdb.set_trace()
    main()

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • linux資料總章2.1 1.0寫的不好抱歉 但是2.0已經(jīng)改了很多 但是錯(cuò)誤還是無法避免 以后資料會(huì)慢慢更新 大...
    數(shù)據(jù)革命閱讀 13,203評(píng)論 2 33
  • 《裕語言》速成開發(fā)手冊(cè)3.0 官方用戶交流:iApp開發(fā)交流(1) 239547050iApp開發(fā)交流(2) 10...
    葉染柒丶閱讀 28,733評(píng)論 5 20
  • 《ijs》速成開發(fā)手冊(cè)3.0 官方用戶交流:iApp開發(fā)交流(1) 239547050iApp開發(fā)交流(2) 10...
    葉染柒丶閱讀 5,636評(píng)論 0 7
  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,536評(píng)論 19 139
  • 一顆相思梅 解盡相思苦 登高身自遠(yuǎn) 忘卻身心念 望山山不在 望水水無痕 顛行多路人 愿寬身與心
    白紙文字閱讀 369評(píng)論 0 0

友情鏈接更多精彩內(nèi)容