您的位置:首页 > 编程语言 > Python开发

python核心编程学习笔记-2016-09-03-02-图形化用户界面编程(三)

2016-09-03 09:00 671 查看
        19.3.6中级Tkinter范例

         这个程序比较复杂,其中有部分内容还没有完全理解。

#-*-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 = ''
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()


        针对self.cwd这个变量,本人理解是,它决定着第二个标签的文本内容,有点类似于指针,默认值是当前工作目录。但输入框和列表框都能影响它的值。

        首先是输入框,用户输入文本内容后,先检查用户输入,若无问题,则将self.cwd设为用户输入的字符串,随之列表框内容也发生相应的改变。其次是在用户点击列表框的某一项时,self.cwd也会变为相应的目录,等同于在输入框输入一个相同的字符串。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  python 编程