Python編程之gui程序?qū)崿F(xiàn)簡單文件瀏覽器代碼
更新時間:2017年12月08日 09:23:25 作者:過去不再從來
這篇文章主要介紹了Python編程之gui程序?qū)崿F(xiàn)簡單文件瀏覽器代碼,具有一定借鑒價值,需要的朋友可以了解下。
本文主要分享了關(guān)于在python中實現(xià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ù),說明版本信息'''
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é)果:

代碼實現(xiàn)功能較簡單,感興趣的朋友參考下吧!
以上就是本文關(guān)于Python編程之gui程序?qū)崿F(xiàn)簡單文件瀏覽器代碼的全部內(nèi)容,希望對大家有所幫助。感興趣的朋友可以繼續(xù)參閱本站:
Python編程實現(xiàn)使用線性回歸預(yù)測數(shù)據(jù)
如有不足之處,歡迎留言指出。感謝朋友們對本站的支持!
相關(guān)文章
詳解Python中映射類型的內(nèi)建函數(shù)和工廠函數(shù)
這篇文章主要介紹了詳解Python中映射類型的內(nèi)建函數(shù)和工廠函數(shù),目前Python的內(nèi)建映射類型只有字典一種,需要的朋友可以參考下2015-08-08
Keras: model實現(xiàn)固定部分layer,訓(xùn)練部分layer操作
這篇文章主要介紹了Keras: model實現(xiàn)固定部分layer,訓(xùn)練部分layer操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-06-06

