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

  • SEARCH
  • Home
  • 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 8847289
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T12:09:31+00:00 2026-06-14T12:09:31+00:00

I am trying to update a textctrl in wxpython while a long running thread

  • 0

I am trying to update a textctrl in wxpython while a long running thread is finding some sort of information. It works well until the user changes focus to another window, and then upon returning to the wxpython script, the GUI is unresponsive until the thread is finished.

Is there a way to return to this script and still see the textctrl updating with the animated cursor?

Here’s the code:

#!/usr/bin/env python
import wx
import thread
import Queue
from time import sleep

class MyFrame1 ( wx.Frame ):

        def __init__( self, parent ):
                wx.Frame.__init__ ( self, parent, id = wx.ID_ANY, title = wx.EmptyString, pos = wx.DefaultPosition, size = wx.Size( 500,300 ), style = wx.DEFAULT_FRAME_STYLE|wx.TAB_TRAVERSAL )

                self.SetSizeHintsSz( wx.DefaultSize, wx.DefaultSize )

                bSizer1 = wx.BoxSizer( wx.VERTICAL )

                self.m_panel1 = wx.Panel( self, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.TAB_TRAVERSAL )
                bSizer2 = wx.BoxSizer( wx.VERTICAL )

                self.m_button1 = wx.Button( self.m_panel1, wx.ID_ANY, u"MyButton", wx.DefaultPosition, wx.DefaultSize, 0 )
                bSizer2.Add( self.m_button1, 0, wx.ALL, 5 )

                self.m_panel1.SetSizer( bSizer2 )
                self.m_panel1.Layout()
                bSizer2.Fit( self.m_panel1 )
                bSizer1.Add( self.m_panel1, 1, wx.EXPAND |wx.ALL, 5 )

                self.m_panel2 = wx.Panel( self, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.TAB_TRAVERSAL )
                bSizer3 = wx.BoxSizer( wx.VERTICAL )

                self.m_textCtrl1 = wx.TextCtrl( self.m_panel2, wx.ID_ANY, wx.EmptyString, wx.DefaultPosition, wx.DefaultSize, wx.TE_MULTILINE )
                bSizer3.Add( self.m_textCtrl1, 1, wx.ALL|wx.EXPAND, 5 )

                self.m_panel2.SetSizer( bSizer3 )
                self.m_panel2.Layout()
                bSizer3.Fit( self.m_panel2 )
                bSizer1.Add( self.m_panel2, 1, wx.EXPAND |wx.ALL, 5 )

                self.SetSizer( bSizer1 )
                self.Layout()

                self.Centre( wx.BOTH )

                # Connect Events
                self.m_button1.Bind( wx.EVT_BUTTON, self.do_something )

        def __del__( self ):
                pass


        # Virtual event handlers, overide them in your derived class
        def do_something( self, event ):
                event.Skip()



class MyFrame( MyFrame1 ):
    def __init__( self, parent ):
        MyFrame1.__init__( self, parent )

    def do_something(self, event):
        self.Result = Queue.Queue()
        thread.start_new_thread(self.do_loop, ())
        self.m_textCtrl1.AppendText("searching for something... ")
        self.found= 0
        for character in self.cursor():
            self.m_textCtrl1.AppendText(character)
            self.Update()
            sleep(0.1)
            self.m_textCtrl1.Undo()
            if (self.found == 1):
                self.m_textCtrl1.AppendText('\n')
                break
        num = self.Result.get()
        print num

    def cursor(self):
        characters='.oOo'
        i = 0
        while 1:
            yield characters[i]
            i = (i + 1) % len(characters)

    def do_loop(self):
        x = 0
        while (x < 20007):
            print x
            x = x + 1
            if x == 20000:
                self.Result.put(x)
                self.found = 1
                break

class threadtest(wx.App):
    def OnInit(self):
        self.m_frame = MyFrame(None)
        self.m_frame.Show()
        self.SetTopWindow(self.m_frame)
        return True

app = threadtest(0)
app.MainLoop()
  • 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-14T12:09:32+00:00Added an answer on June 14, 2026 at 12:09 pm

    Here is a solution based on Anonymous Coward’s comment. I moved everything from the button event into a thread. Let me know if there is a better way to do this.

    #!/usr/bin/env python
    import wx
    import thread
    import Queue
    from time import sleep
    
    class MyFrame1 ( wx.Frame ):
    
            def __init__( self, parent ):
                    wx.Frame.__init__ ( self, parent, id = wx.ID_ANY, title = wx.EmptyString, pos = wx.DefaultPosition, size = wx.Size( 500,300 ), style = wx.DEFAULT_FRAME_STYLE|wx.TAB_TRAVERSAL )
    
                    self.SetSizeHintsSz( wx.DefaultSize, wx.DefaultSize )
    
                    bSizer1 = wx.BoxSizer( wx.VERTICAL )
    
                    self.m_panel1 = wx.Panel( self, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.TAB_TRAVERSAL )
                    bSizer2 = wx.BoxSizer( wx.VERTICAL )
    
                    self.m_button1 = wx.Button( self.m_panel1, wx.ID_ANY, u"MyButton", wx.DefaultPosition, wx.DefaultSize, 0 )
                    bSizer2.Add( self.m_button1, 0, wx.ALL, 5 )
    
                    self.m_panel1.SetSizer( bSizer2 )
                    self.m_panel1.Layout()
                    bSizer2.Fit( self.m_panel1 )
                    bSizer1.Add( self.m_panel1, 1, wx.EXPAND |wx.ALL, 5 )
    
                    self.m_panel2 = wx.Panel( self, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.TAB_TRAVERSAL )
                    bSizer3 = wx.BoxSizer( wx.VERTICAL )
    
                    self.m_textCtrl1 = wx.TextCtrl( self.m_panel2, wx.ID_ANY, wx.EmptyString, wx.DefaultPosition, wx.DefaultSize, wx.TE_MULTILINE )
                    bSizer3.Add( self.m_textCtrl1, 1, wx.ALL|wx.EXPAND, 5 )
    
                    self.m_panel2.SetSizer( bSizer3 )
                    self.m_panel2.Layout()
                    bSizer3.Fit( self.m_panel2 )
                    bSizer1.Add( self.m_panel2, 1, wx.EXPAND |wx.ALL, 5 )
    
                    self.SetSizer( bSizer1 )
                    self.Layout()
    
                    self.Centre( wx.BOTH )
    
                    # Connect Events
                    self.m_button1.Bind( wx.EVT_BUTTON, self.do_something )
    
            def __del__( self ):
                    pass
    
    
            # Virtual event handlers, overide them in your derived class
            def do_something( self, event ):
                    event.Skip()
    
    
    
    class MyFrame( MyFrame1 ):
        def __init__( self, parent ):
            MyFrame1.__init__( self, parent )
    
        def do_something(self, event):
            thread.start_new_thread(self.do_something_thread, ())
    
        def do_something_thread(self):
            self.Result = Queue.Queue()
            thread.start_new_thread(self.do_loop, ())
            self.m_textCtrl1.AppendText("searching for something... ")
            self.found= 0
            for character in self.cursor():
                self.m_textCtrl1.AppendText(character)
                self.Update()
                sleep(0.1)
                self.m_textCtrl1.Undo()
                if (self.found == 1):
                    self.m_textCtrl1.AppendText('\n')
                    break
            num = self.Result.get()
            print num
    
        def cursor(self):
            characters='.oOo'
            i = 0
            while 1:
                yield characters[i]
                i = (i + 1) % len(characters)
    
        def do_loop(self):
            x = 0
            while (x < 20007):
                print x
                x = x + 1
                if x == 20000:
                    self.Result.put(x)
                    self.found = 1
                    break
    
    class threadtest(wx.App):
        def OnInit(self):
            self.m_frame = MyFrame(None)
            self.m_frame.Show()
            self.SetTopWindow(self.m_frame)
            return True
    
    app = threadtest(0)
    app.MainLoop()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im trying to update the contents of an element after running some php code.
Im trying to update a progress bar while doing some data type checks on
I am trying update text views while this loop is processing. Here is my
Im trying to update some nested params from a form. I can see that
Trying to update some repositories on Debian Etch installation and getting the following errors
I am trying to update number of records with respect to their thread Id.
I am trying to update a mongoose model. Having some difficulites. This is how
Trying to update some old code from live() to use on() and something is
In trying to update my SDK to the latest, some things installed, but others*
I'm trying update some values for some hidden input boxes, but I'm not very

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.