Why don't threads execute concurrently with GUI?

Hi, all

from multiprocessing import Process
import wx

class MainWindow(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, title="sylphlker")
        self.panel = wx.Panel(self, wx.ID_ANY)            
        self.k1  = wx.TextCtrl(self.panel, -1, "F1", size=(45,25), style=wx.TE_READONLY)

def HotKey():
    hotkey = hotkey.k1.GetValue()
if __name__ == '__main__':
    app = wx.App()
    frame = MainWindow()
    p = Process(target = HotKey)

Why don’t threads execute concurrently with GUI?, An error is thrown when the GUI is closed:wx._core.PyNoAppError: The wx.App object must be created first!

  1. You are not using a thread. You are using a process. There is a big difference between both.
    A thread runs in the same memory space than the caller/creator.
    A process runs in a different memory space.

  2. app.MainLoop() exists when the main window is closed (app is exiting). So, p is created when main window is closed.

What are you trying to do ?

I want to execute the process immediately after the GUI is created

Tank you very much, a thread can quote attributes of the wx.frame(the main process), but the child process can not, because they run in different memory spaces. Do i get the truth? In other post, Marc_Tompkins teach me how to use the thread module to fix my problem. you both so kind, thank you again.

A child process can not access parent process. You have to use special inter-process communication objects.
A child thread can reach all objects of it’s parent process/thread. But be very careful. A working thread must not access the GUI API directly. You have to use the special function wx.CallAfter().

To run your working thread immediately after GUI launches, create the thread at the end of the init() method of your main window/frame.