Python編程之gui程序?qū)崿F(xiàn)簡(jiǎn)單文件瀏覽器代碼
本文主要分享了關(guān)于在python中實(shí)現(xiàn)一個(gè)簡(jiǎn)單的文件瀏覽器的代碼示例,代碼及展示如下。
#!/usr/bin/env python # -*- coding: UTF-8 -*- import os from time import sleep from Tkinter import * class DirList(object): def __init__(self, initdir=None): '''構(gòu)造函數(shù),說(shuō)明版本信息''' self.top = Tk() self.label = Label(self.top, text = 'My directory Lister v1.1') self.label.pack() self.cwd = StringVar(self.top) self.dir1 = Label(self.top, fg='blue', font=('Helvetica', 22, 'bold')) self.dir1.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', command = 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.dir1.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()
結(jié)果:
代碼實(shí)現(xiàn)功能較簡(jiǎn)單,感興趣的朋友參考下吧!
以上就是本文關(guān)于Python編程之gui程序?qū)崿F(xiàn)簡(jiǎn)單文件瀏覽器代碼的全部?jī)?nèi)容,希望對(duì)大家有所幫助。感興趣的朋友可以繼續(xù)參閱本站:
Python編程實(shí)現(xiàn)使用線(xiàn)性回歸預(yù)測(cè)數(shù)據(jù)
如有不足之處,歡迎留言指出。感謝朋友們對(duì)本站的支持!
相關(guān)文章
python計(jì)算機(jī)視覺(jué)opencv卡號(hào)識(shí)別示例詳解
這篇文章主要為大家介紹了python計(jì)算機(jī)視覺(jué)opencv卡號(hào)識(shí)別的實(shí)現(xiàn)示例詳解,有需要的朋友可以借鑒參考下 希望能夠有所幫助,祝大家多多進(jìn)步2021-11-11詳解Python中映射類(lèi)型的內(nèi)建函數(shù)和工廠(chǎng)函數(shù)
這篇文章主要介紹了詳解Python中映射類(lèi)型的內(nèi)建函數(shù)和工廠(chǎng)函數(shù),目前Python的內(nèi)建映射類(lèi)型只有字典一種,需要的朋友可以參考下2015-08-08Selenium啟動(dòng)Chrome時(shí)配置選項(xiàng)詳解
這篇文章主要介紹了Selenium啟動(dòng)Chrome時(shí)配置選項(xiàng)詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03pytorch 模型的train模式與eval模式實(shí)例
今天小編就為大家分享一篇pytorch 模型的train模式與eval模式實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-02-02python簡(jiǎn)單實(shí)現(xiàn)獲取當(dāng)前時(shí)間
最近項(xiàng)目中經(jīng)常需要python去取當(dāng)前的時(shí)間,雖然不是很難,但是老是忘記,用一次丟一次,為了能夠更好的記住,我今天特意寫(xiě)下python 當(dāng)前時(shí)間這篇文章,如果你覺(jué)的對(duì)你有用的話(huà),可以收藏下。2016-08-08Keras: model實(shí)現(xiàn)固定部分layer,訓(xùn)練部分layer操作
這篇文章主要介紹了Keras: model實(shí)現(xiàn)固定部分layer,訓(xùn)練部分layer操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-06-06