Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8919875
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T06:08:00+00:00 2026-06-15T06:08:00+00:00

I am new to python and gui programming.I am writing a program that will

  • 0

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()
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T06:08:02+00:00Added an answer on June 15, 2026 at 6:08 am

    So there were two basic issues with your code.

    1. 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 run gobject.threads_init()

    2. 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).

    #!/usr/bin/env python
    
    import gobject
    import time  # This is just used for slowing down the threads
    import os
    import threading
    import gtk
    
    
    class FileBrowser:
    
        def __init__(self):
            self.window = gtk.Window()
            self.window.show_all()
            self.window.connect("destroy", self._destroy)
    
            box = gtk.VBox()
            box.show()
    
            self.scrolled_w = gtk.ScrolledWindow()
            self.scrolled_w.show()
            self.window.add(box)
    
            self.status = gtk.Label("...")  # I added a status label to see when scan is done
            self.status.show()
            box.pack_start(self.status, False, False, 2)
    
            self.button = gtk.Button("start")
            self.button.show()
            self.button.connect("clicked",self.start_scanning)
            self.button.set_size_request(30,50)
            box.pack_start(self.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)
    
            self.threads = list()  # I made this be part of the full application
            """ That allows us to wait for all threads on close-down, don't know how
            necessary it is."""
    
        def start_scanning(self,w):
    
            self.button.set_sensitive(False)  # To disallow stacking scan-stats
            self.status.set_text("Scanning...")  # Let user know it is started
            self.model.clear()
            main_dir= os.path.expanduser("~")  # I just wanted my home-dir instead
            list_dir = os.listdir(main_dir)
            no_of_threads = len(list_dir)
            for i in range(no_of_threads):
                t = threading.Thread(target=self._thread_scanning,args=(main_dir,list_dir[i],))
                self.threads.append(t)
                t.start()
    
            gobject.timeout_add(200, self._callback)  # This will cause the main app to
            #check every 200 ms if the threads are done.
    
        def _callback(self):
    
            if threading.active_count() == 1:  # If only one left, scanning is done
                self.status.set_text("Done!")
                self.button.set_sensitive(True)  # Allow button being pressed again
                return False  # False make callback stop
    
            print threading.active_count()
            return True
    
        def _thread_scanning(self,main_d,list_d):
            path = os.sep.join((main_d, list_d))  # Made use of os's sep instead...
            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])
    
            time.sleep(3)  # Useless other than to delay finish of thread.
    
        def main(self):
    
            gtk.main()
    
    
        def _destroy(self, *args, **kwargs):
    
            #Own destroy that waits for all threads...
            for t in self.threads:
                t.join()
    
            gtk.main_quit(*args, **kwargs)
    
    if __name__=="__main__":
        gobject.threads_init()
        fb=FileBrowser()
        fb.main()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to Python and am starting to teach myself GUI programming (hopefully) using
I have written a python program with a PyQt GUI that, using the imaplib,
I am fairly new to Python programming, and completely new to cross-platform GUI building
I'm new with python programming and GUI. I search on internet about GUI programming
I am new in Python and using PyQt4 for developing the Gui. I want
I am very new to object orientated javascript, with experience writing gui's in python
I'm teaching an introductory class to programming and GUI development using Python, and have
I'm new to GUI programming with python and gtk, so this is a bit
I'm new to Python and I'm trying to create a simple GUI using Tkinter.
I am new to Python. I am writing an application using wxPython and I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.