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 555721
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T11:50:39+00:00 2026-05-13T11:50:39+00:00

Have been wondering about this for days now: I have a basic wxpython program

  • 0

Have been wondering about this for days now:

I have a basic wxpython program like this:

from MyModule import *

class Form(wx.Panel):
  def __init__(self, parent, id):
    self.gauge = wx.Gauge(...)
    ...
  def ButtonClick(self, event):
    proc = LongProcess()
    while (LongProcess):
      self.gauge.SetValue(LongProcess.status)
      wx.Yield()

which imports the MyModule.py:

from threading import *

class LongProcess(self):
  def __init__(self):
    Thread.__init__(self)
    self.start()
  def run(self):
    for i in range(100):
      Do_something()
      self.status = i  

This updates the gauge according to the value LongProcess.status, as expected. But the while-loop doesn’t seem appropriate as the whole program uses 100% cpu load because it continuously checks the status (not surprising, tho). Is there any way to send the status back to the “mother program” without doing that millions of times per second?

  • 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-05-13T11:50:39+00:00Added an answer on May 13, 2026 at 11:50 am

    You can instantiate custom events from the non-GUI thread and wx.PostEvent them back to the GUI-thread. This is a thread-safe action. My use cases typically work like this:

    • Start worker thread – Custom event ‘Starting Action’
    • Start processing
    • Post events back updating progress ‘Line 435 of 15000 is parsed’
    • etc.

    Then I bind the custom event to update a dialog or textctrl/log or whatever. It’s surprisingly easy to do. If you’d like I can post some sample code of a little test case I wrote a while back when I was figuring this stuff out.

    –Edit:

    Okay here’s some code, first the threading example:

    #!usr/bin/env python
    
    import wx
    import threading
    import Queue
    import random
    import time
    
    TextEventType = wx.NewEventType()
    EVT_THREAD_TEXT_EVENT = wx.PyEventBinder(TextEventType, 1)
    
    global_queue = Queue.Queue()
    
    def threadStart(numthrds, queue, window):
        for i in range(numthrds):
            i = TextThread(queue, window)
    
    class TextThread(threading.Thread):
        def __init__(self, queue, output_window):
            threading.Thread.__init__(self)
            self.inqueue = queue
            self.output_window = output_window
            self.start()
    
    
        def run(self):
            word = self.inqueue.get()
            self.setName(word.upper())
            wait = random.randrange(1, 10)
            time.sleep(wait)
            msg = 'Thread: ' + self.getName() + '--wait= ' + str(wait) + ' ' + word
            evt = NewTextEvent(TextEventType, -1)
            evt.setText(msg)
            wx.PostEvent(self.output_window, evt) #post EVT_THREAD_TEXT_EVENT
            #self.inqueue.task_done() #may not need this if non-blocking
    
    
    
    class NewTextEvent(wx.PyCommandEvent):
        def __init__(self, evtType, id):
            wx.PyCommandEvent.__init__(self, evtType, id)
    
            self.msg = ''
    
        def setText(self, text):
            self.msg = text
    
        def getText(self):
            return self.msg
    
    class TextFrame(wx.Frame):
        def __init__(self, parent, id, *args, **kwargs):
            wx.Frame.__init__(self, parent, id, *args, **kwargs)
            self.queue = Queue.Queue()
            framesizer = wx.BoxSizer(wx.VERTICAL)
            self.panel = ThreadPanel(self, wx.ID_ANY)
            framesizer.Add(self.panel, 0, wx.EXPAND)
            self.SetSizerAndFit(framesizer)
    
            self.Bind(EVT_THREAD_TEXT_EVENT, self.OnThreadText)
    
        def OnThreadText(self, evt):
            msg = evt.getText()
            self.panel.out_tc.AppendText(msg + '\n')
    
    class ThreadPanel(wx.Panel):
        def __init__(self, parent, id, *args, **kwargs):
            wx.Panel.__init__(self, parent, *args, **kwargs)
            vsizer = wx.BoxSizer(wx.VERTICAL)
            self.wordtc = wx.TextCtrl(self, id=wx.ID_ANY, value='', size=(350, -1))
            self.inst_text = wx.StaticText(self, wx.ID_ANY,
                label='Enter a list of space-separated words')
            self.out_tc = wx.TextCtrl(self, id=wx.ID_ANY, size=(350, 300), 
                value='', style=wx.TE_MULTILINE)
            self.start_button = wx.Button(self, wx.ID_ANY, label='Start Threads')
    
            vsizer.Add(self.inst_text, 0, wx.ALIGN_LEFT)
            vsizer.Add(self.wordtc, 0, wx.EXPAND)
            vsizer.Add(self.start_button)
            vsizer.Add((100,100))
            vsizer.Add(self.out_tc, 0, wx.EXPAND)
            self.SetSizer(vsizer)
            self.Bind(wx.EVT_BUTTON, self.OnStartButton, self.start_button)
    
        def OnStartButton(self, evt):
            self.out_tc.Clear()
            text = self.wordtc.GetValue()
            self.wordtc.Clear()
            if not text.count(','):
                text = text.split(' ')
            num_thrds = len(text)
            for word in text:
                word = word.strip()
                self.GetParent().queue.put(word)
            threadStart(num_thrds, self.GetParent().queue, self.GetParent())
    
    
    
    
    if __name__ == "__main__":
        app = wx.App()
        frame = TextFrame(None, wx.ID_ANY, 'Thread test')
        frame.Show()    
        app.MainLoop()
    

    And a second, more simple example with custom events:

    #!usr/bin/env python
    
    import wx
    import random
    
    colorEventType = wx.NewEventType()
    EVT_COLOR_EVENT = wx.PyEventBinder(colorEventType, 1)
    
    class ButtonPanel(wx.Panel):
        def __init__(self, parent, *args, **kwargs):
            wx.Panel.__init__(self, parent, *args, **kwargs)
    
            vsizer = wx.BoxSizer(wx.VERTICAL)
            self.rstbutt = wx.Button(self, wx.ID_ANY, label='Restore')
            self.rstbutt.Disable()
            self.Bind(wx.EVT_BUTTON, self.OnButt, self.rstbutt)
            vsizer.Add(self.rstbutt, 0, wx.ALIGN_CENTER)
            vsizer.Add((500,150), 0)
            self.SetSizer(vsizer)
    
        def OnButt(self, evt):
            self.SetBackgroundColour(wx.NullColor)
            self.GetParent().Refresh()
            self.rstbutt.Disable()
    
    class ColorEvent(wx.PyCommandEvent):
        def __init__(self, evtType, id):
            wx.PyCommandEvent.__init__(self, evtType, id)
            self.color = None
    
        def SetMyColor(self, color):
            self.color = color
    
        def GetMyColor(self):
            return self.color
    
    class MainFrame(wx.Frame):
        def __init__(self, parent, *args, **kwargs):
            wx.Frame.__init__(self, parent, *args, **kwargs)
            framesizer = wx.BoxSizer(wx.VERTICAL)
            self.panel = ButtonPanel(self, wx.ID_ANY)
            framesizer.Add(self.panel, 1, wx.EXPAND)
    
            menubar = wx.MenuBar()
            filemenu = wx.Menu()
            menuquit = filemenu.Append(wx.ID_ANY, '&Quit')
            menubar.Append(filemenu, 'File')
            colormenu = wx.Menu()
            switch = colormenu.Append(wx.ID_ANY, '&Switch Color')
            menubar.Append(colormenu, '&Color')
            self.SetMenuBar(menubar)
    
            self.Bind(wx.EVT_MENU, self.OnQuit, menuquit)
            self.Bind(wx.EVT_MENU, self.OnColor, switch)
            self.Bind(EVT_COLOR_EVENT, self.ColorSwitch)
            self.SetSizerAndFit(framesizer)
    
        def OnQuit(self, evt):
            self.Close()
    
        def OnColor(self, evt):
            colevt = ColorEvent(colorEventType, -1) 
            colors = ['red', 'green', 'blue', 'white', 'black', 'pink', 
                (106, 90, 205), #slate blue
                (64, 224, 208), #turquoise
                ]
            choice = random.choice(colors)
            colevt.SetMyColor(choice)
            self.GetEventHandler().ProcessEvent(colevt)
            #evt.Skip()
    
        def ColorSwitch(self, evt):
            color = evt.GetMyColor()
            #print(color)
            self.panel.SetBackgroundColour(color)
            self.Refresh()
            self.panel.rstbutt.Enable()
    
    
    
    if __name__ == "__main__":
        app = wx.App()
        frame = MainFrame(None, wx.ID_ANY, title="Change Panel Color Custom Event")
        frame.Show(True)
    
        app.MainLoop()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 415k
  • Answers 415k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer This is an excellent blog with advanced concepts and tutorials:… May 15, 2026 at 9:00 am
  • Editorial Team
    Editorial Team added an answer This is entirely normal, assemblies that contain IL code always… May 15, 2026 at 9:00 am
  • Editorial Team
    Editorial Team added an answer right I took another look at the code, and I… May 15, 2026 at 9:00 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.