I am new to python and gui programming.I am writing a program that will list all the directories in root(/) and all the sub-directories in these directories.I am using treeview to show these directories and sub-directories.To speed up my program i am using multi-threading,but i am facing a problem that the tree is displayed only after all the threads are executed.I want tree to be displayed as any thread is executed and other nodes append to tree dynamically as other threads are executed.Thanks in advance.
Here is my code
import os
import threading
import gtk
class FileBrowser:
def __init__(self):
self.window = gtk.Window()
self.window.show_all()
self.window.connect("destroy", gtk.main_quit)
box = gtk.VBox()
box.show()
self.scrolled_w = gtk.ScrolledWindow()
self.scrolled_w.show()
self.window.add(box)
button = gtk.Button("start")
button.show()
button.connect("clicked", self.start_scanning)
button.set_size_request(30, 50)
box.pack_start(button, False, False, 4)
self.model=gtk.TreeStore(str)
self.treeview = gtk.TreeView(self.model)
self.treeview.show()
col = gtk.TreeViewColumn("FileName")
cell = gtk.CellRendererText()
self.treeview.append_column(col)
col.pack_start(cell, 0)
col.set_attributes(cell, text=0)
box.pack_start(self.scrolled_w, 10)
self.scrolled_w.add(self.treeview)
self.window.set_size_request(600, 300)
def start_scanning(self, w):
self.model.clear()
main_dir = "/"
list_dir = os.listdir(main_dir)
no_of_threads = len(list_dir)
threads = []
for i in range(no_of_threads):
t = threading.Thread(target=self.thread_scanning,
args=(main_dir,list_dir[i],))
threads.append(t)
t.start()
for t in threads:
t.join()
def thread_scanning(self, main_d, list_d):
path = main_d+""+list_d
if os.path.isdir(path):
list_subd = os.listdir(path)
par = self.model.append(None, [list_d])
for sub in list_subd:
self.model.append(par, [sub])
def main(self):
gtk.main()
if __name__=="__main__":
fb = FileBrowser()
fb.main()
So there were two basic issues with your code.
As @windfinder pointed out, you need to run
gobject.threads_init()for threading to work properly with your gtk-application if you want the threads to update your gui. Actually it can cause many weird problems if you don’t rungobject.threads_init()The callback of the button doesn’t finnish until all threads have finnished since you ask the threads to join before you return control to the main-loop. You can fix this by adding a timeout via
gobject.I did a couple of changes to your code (I also asked the threads to sleep so I could see that the gui was updated while the threads are still running).