Sunday, May 5, 2024
2
rated 0 times [  2] [ 0]  / answers: 1 / hits: 3117  / 2 Years ago, wed, september 21, 2022, 2:42:36

I have a tree which I populate with a file list from my computer on the click of a button. While the program is populating the tree the whole GUI hangs.



Is there a way to populate the tree in another thread so the tree gets populated as the files are found and not when everything has already been added? Or any other ideas?



This is what it looks like:



Screenshot of App



I am populating the tree with this code:



def traversePaths(self, path, parent):
files = os.listdir(path)
files.sort()
for myfile in files:
if myfile[0] != ".":
if os.path.isdir(path + os.sep + myfile):
current = self.filelist.append(parent,[self.dirIcon,self.dirIcon,self.dirOpenIcon,myfile,"DD",True,True,3])
self.traversePaths(path + os.sep + myfile, current)
else:
current = self.filelist.append(parent,[self.fileIcon,self.dirIcon,self.dirOpenIcon,myfile,"AA",True,True,3])


which is executed on a button click:



def on_refreshbutton_clicked(self, button):
self.traversePaths(self.path.get_filename(), None)


I don't think threading.Thread works because of the gtk thread for the gui and cannot find gi.repository.Gtk api's on the subject



Any ideas?


More From » application-development

 Answers
6

When asking programming questions, it is always best to provide a minimal working example and not just code snippets.



Without a working example to play with, I have the following suggestions:




  1. Use a thread to walk the directory tree and put your results in a second separate treemodel (not connected to a treeview). This means you have two treemodels, one connected (which gets rendered to the screen) and one not connected (which therefore does not get rendered)

  2. Add a timeout of a few seconds, which does calls a function that detaches and deletes the first treemodel, copies the second treemodel to a new one, which now serves as the "thread" treemodel and attaches the second tree model.



The reason why you see the GUI hanging is that the file walker takes very long and every time you add a file, a lot of overhead in Gtk is called. By adding a full treemodel, this overhead is only called one time. And by using a thread for the file walking, your GUI stays responsive.



As for the threading part in Gtk, you might want to look here: https://stackoverflow.com/questions/8120860/python-doing-some-work-on-background-with-gtk-gui



A couple of notes to your code:




  • Python has a build-in file walker, which might be faster but is certainly shorter than your code: os.walk

  • If you want to use your code instead, remember that Python has a build-in recursion limit. Depending on the size of your file system, you might want to replace the recursion with a trampoline like construct


[#35993] Thursday, September 22, 2022, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
scusaper

Total Points: 335
Total Questions: 111
Total Answers: 119

Location: Belize
Member since Mon, Jun 20, 2022
2 Years ago
;