RSS Forums RSS

Append Results to ListBox

Please support our Python advertiser: Programming Forums
Reply
Posts: 427
Reputation: Agni has a spectacular aura about Agni has a spectacular aura about Agni has a spectacular aura about 
Solved Threads: 65
Sponsor
Agni's Avatar
Agni Agni is offline Offline
Posting Pro in Training

Append Results to ListBox

  #1  
Jun 28th, 2008
Hi,

I have 2 modules, grepforapp.py and myApp.py. Basically i'm trying to make a 'search' utility. myApp makes the UI with one textbox for taking input, start button and a list box which displays the file names where the pattern was found.
grepforApp walks through the dir tree and searches the pattern in each file. In my previous code i used to read and store everything and then would display the results in one shot. Then i changed the code to call the grepforapp.search in a loop and try to display the results as they come for each dir. but this is not happening. If you see the code i'm doing a ListBox.Append in a loop but still the final result is shown in one shot and not in steps. Can anyone see what's wrong with the code?

myApp module
  1. import wx
  2. import grepforapp
  3. import os
  4. import pdb
  5. import time
  6.  
  7. #pdb.set_trace()
  8.  
  9. class myApp(wx.Frame):
  10. def __init__(self,parent,id,title):
  11. wx.Frame.__init__(self,parent,id,title)
  12. self.tc1 = None
  13. self.Show()
  14.  
  15. def addButton(self,ID,butStr,coord):
  16. wx.Button(self,ID,butStr,coord)
  17. self.Bind(wx.EVT_BUTTON,self.addStaticBox,id=ID)
  18.  
  19. def addStaticBox(self,event):
  20. superDict = {}
  21. tc2 = None
  22. tc2 = wx.ListBox(self, 26, (5,80), (350, 150),style=wx.LB_SINGLE)
  23. tc2.SetForegroundColour('#RRGGBB')
  24. pattern = self.tc1.GetValue()
  25. superDict = {}
  26. if pattern != '':
  27. for roots,dirs,files in os.walk(os.getcwd()):
  28. myFileList = []
  29. fileList = []
  30. superDict.clear()
  31. myFileList = self.getFileList(files)
  32. superDict = grepforapp.searchPattern(pattern,1,myFileList,roots)
  33. # for key in superDict.keys():
  34. # print key
  35. fileList = superDict.keys()
  36. tc2.AppendItems(fileList)
  37. time.sleep(10)
  38.  
  39. def getFileList(self,files):
  40. # print roots,dirs,files
  41. i = 0
  42. i = len(files)
  43. searchAll = 1
  44. # print i
  45. myFileList = []
  46. for j in range(i):
  47. filename,fileext = os.path.splitext(files[j])
  48. # print fileext
  49. if(searchAll):
  50. myFileList.append(files[j])
  51. else:
  52. if fileext == '.cpp' or fileext == '.h' or fileext == '.hpp' or fileext == '.py':
  53. myFileList.append(files[j])
  54. return myFileList
  55. # print myFileList
  56.  
  57. def addTextCtrl(self):
  58. self.tc1 = wx.TextCtrl(self,pos=(5,5),size=(200,30))
  59.  
  60. def main():
  61. App = wx.App()
  62. app = myApp(None,-1,'Search')
  63. #app.addMenu()
  64. app.addTextCtrl()
  65. app.addButton(1,'Stop',(100,40))
  66. app.addButton(2,'Search',(5,40))
  67. #app.addStaticBox()
  68. App.MainLoop()
  69.  
  70. if __name__ == "__main__":
  71. main()

pgrepforapp code
  1. import os,re,sys
  2. import glob,getopt
  3. import pdb
  4.  
  5. #pdb.set_trace()
  6.  
  7. def searchPattern(pattern,searchAll,myFileList,roots):
  8. if pattern == '':
  9. usage()
  10. valueList = []
  11. superDict = {}
  12. superDict.clear()
  13. print '---------------Results---------------\n'
  14. for fil in myFileList:
  15. # print fil
  16. try:
  17. valueList = []
  18. matchFound = 0
  19. fullpath = roots + '/' + fil
  20. # print fullpath
  21. fileObj = open(fullpath)
  22. lines = fileObj.readlines()
  23. count = 0
  24. for count,line in enumerate(lines):
  25. if (re.search(pattern.lower(),line.lower())):
  26. matchFound = 1
  27. # print fullpath
  28. matchline = 'line#'+ str(count+1),line
  29. # #print matchline
  30. valueList.append(matchline)
  31. if matchFound:#
  32. superDict[fullpath] = valueList
  33. # print superDict.keys()
  34. except IOError:
  35. pass
  36. except KeyboardInterrupt:
  37. print 'so u got bored.. me too.. ok bye..'
  38. sys.exit(2)
  39. # print superDict.keys()
  40. return superDict
  41.  
  42. def usage():
  43. print 'invalid option'
  44. print 'usage: pgrep [-a] -p pattern'
  45. print '-a: search in all files'
  46. sys.exit()
  47.  
thanks
-chandra
AddThis Social Bookmark Button
Reply With Quote  
Posts: 764
Reputation: woooee has a spectacular aura about woooee has a spectacular aura about woooee has a spectacular aura about 
Solved Threads: 142
woooee woooee is offline Offline
Master Poster

Re: Append Results to ListBox

  #2  
Jun 28th, 2008
Take a look at the control variables section at the following link. A control variable can be linked to several widgets and will update the widget automatically. http://infohost.nmt.edu/tcc/help/pubs/tkinter/
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.



Other Threads in the Python Forum
Views: 593 | Replies: 1 | Currently Viewing: 1 (0 members and 1 guests)

 

Thread Tools Display Modes
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 3:24 pm.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC