示例5-6代码保存

import os
from time import sleep
from tkinter import *


class DirList(object):  # 老版本的继承

    def __init__(self, initdir=None):  # initdir 不知道啥意思
        self.top = Tk()  # 建立顶层窗口
        self.label = Label(self.top, text='Directory Lister v1.1')  # 建立一个label 标题
        self.label.pack()  # 放入顶层窗口中

        self.cwd = StringVar(self.top)  # 还不知道是什么,只知道保存变量?追踪?

        self.dirl = Label(self.top, fg='blue', font=('Helvetica', '12', 'bold'))
        # 第二个label 存储的是当前路径
        self.dirl.pack()  # 第二个label打包

        self.dirfm = Frame(self.top)  # 在top中建立一个容器
        self.dirsb = Scrollbar(self.dirfm)  # 给Frame建立一个滚动条
        self.dirsb.pack(side=RIGHT, fill=Y)  # 打包滚动条,放在右边并且填满Y轴
        self.dirs = Listbox(self.dirfm, height=15, width=50, yscrollcommand=self.dirsb.set)
        #  建立一个Listbox,放在建立的Frame里面,高度为15,宽度50,垂直命令由滚动条给出
        self.dirs.bind('<Double-1>', self.setDirAndGo)  # 绑定回调函数 双击
        self.dirsb.config(command=self.dirs.yview())  # 绑定和Listbox的联系
        self.dirs.pack(side=LEFT, fill=BOTH)  # 打包listbox 靠左,填满剩余部分
        self.dirfm.pack()  # Frame打包

        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())  # 当前选择的赋值给check
        if not check:
            check = os.curdir  # 如果没有check,默认为当前路径
        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 + ': No such directory'  # 不存在当前路径

        if error:
            self.cwd.set(error)
            self.top.update()  # 刷新界面?
            sleep(2)
            if not (hasattr(self, 'last') and self.last):  # 判断self.last是否合法
                self.last = os.curdir
            self.cwd.set(self.last)
            self.dir.config(selectbackgroud='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__':
    main()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值