Chinaunix首页 | 论坛 | 博客
  • 博客访问: 49594
  • 博文数量: 16
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 112
  • 用 户 组: 普通用户
  • 注册时间: 2013-10-09 19:10
文章分类

全部博文(16)

文章存档

2015年(2)

2014年(3)

2013年(11)

我的朋友

分类: Python/Ruby

2014-02-28 23:02:46

纯新手,写完就发现自己技能太差,急需充电,正好新工作也要开始了,Python暂时放下了,待我再次归来吧。。。。
'MyEverything V1.1---^^'
import os,threading
from tkinter import *
from tkinter.filedialog import *
from tkinter.messagebox import showerror
from time import sleep
from FindTool import *


ERR_STRING = ['未建立索引---dirs']


def threadSearch(file_name,keyword,obj):
    global mutex
    fobj = open(file_name, 'r')          
    for eachLine in fobj.readlines():
        if keyword in eachLine:
           mutex.acquire()
           obj.dirs.insert(END,eachLine)
           obj.top.update()
           mutex.release()
    fobj.close()
class DirList(object):
    def __init__(self,initdir = None):
        self.top = Tk()
        self.top.title('MyEverything')
        self.label = Label(self.top,text="Welcome to use MyEverything V1.1")
        self.label.pack()        
        self.cwd = StringVar(self.top)
        self.dirlabel = Label(self.top,fg='blue',font=('Helvetica',12,'bold'))
        self.dirlabel.pack()
        
        self.dirfm = Frame(self.top)
        self.dirfm.pack()
        
        self.dirsb = Scrollbar(self.dirfm)
        self.dirsb.pack(side = RIGHT,fill = Y)
        
        self.dirsb2 = Scrollbar(self.dirfm,orient = HORIZONTAL)
        self.dirsb2.pack(side = BOTTOM,fill = BOTH)
                
        self.dirs =Listbox(self.dirfm,height =15,width= 50,yscrollcommand=self.dirsb.set,xscrollcommand=self.dirsb2.set)
        self.dirs.bind('',self.setDirs)        
        self.dirsb.config(command = self.dirs.yview)
        self.dirsb2.config(command = self.dirs.xview)
        self.dirs.pack(side = LEFT,fill = BOTH)     


        self.dirn = Entry(self.top,width = 50,textvariable =self.cwd)
        self.dirn.bind('',self.fileSearch)
        self.dirn.pack()
        
        self.bfm= Frame(self.top)
        self.clr =Button(self.bfm,text ='Clear',command = self.clrDir,activeforeground ='white',activebackground ='blue',)
        self.find =Button(self.bfm,text ='Search',command =self.fileSearch, activeforeground ='white',activebackground ='green')
        self.setdir =Button(self.bfm,text ='dirs',command =self.setDirs, activeforeground ='white',activebackground ='yellow')
        self.quit =Button(self.bfm,text= 'quit',command=self.setQuit,activeforeground ='white',activebackground ='red')
        self.clr.pack(side = LEFT)
        self.find.pack(side = LEFT)
        self.setdir.pack(side = LEFT)
        self.quit.pack(side = LEFT)
        self.bfm.pack()   
        if initdir:
            self.err = ERR_STRING[0]
            self.cwd.set(os.curdir)
            self.setDirs
    def setQuit(self,ev = None):
        global g_killsig
        g_killsig = True
        self.top.destroy()
        
    def clrDir(self, ev = None):
        self.cwd.set('')
        self.dirs.delete(0,END)
        self.top.update()
        
    def setDirs(self):
        global g_fdirs,g_flag
        g_fdirs = askdirectory(initialdir = 'd:')
        if g_fdirs:
            g_flag = True
            self.dirlabel.config(text = "加载中ing")
        
    def fileSearch(self, ev = None):
        self.dirs.delete(0,END)
        self.top.update()    
        '从保存的索引文件中,查找文件'
        file_list = FindFile()
        if file_list == 0:
            self.dirlabel.config(text = self.err)
            return
        keyword = self.cwd.get()
        for file_name in file_list:
            threading.Thread(target = threadSearch(file_name,keyword,self)).start()
        self.top.update()        
       
def test():
    global g_flag,g_fdirs,g_killsig
    
    g_flag = False
    while(True):
        sleep(1)
        if g_flag == True:
            SaveIndex(g_fdirs)
            d.dirlabel.config(text = '建立索引完成,可以使用')
            g_flag = False
        if g_killsig == True:
            break
                   
def main():
    global d
    d=DirList(os.curdir)
    mainloop()
    
if __name__ == '__main__':
    global mutex,g_killsig
    
    g_killsig = False
    mutex = threading.Lock()
    threading.Thread(target = test).start()
    main()
阅读(2376) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~