wxPython having trouble with frame objects

Hello,
I am using wxglade to design a gui which i am using in another script.
Here are the codes

The main file:
import wx,gui,threading
class guithread(threading.Thread):
   def run(self):
       app = wx.PySimpleApp(0)
       wx.InitAllImageHandlers()
       self.frame_1 = gui.MyFrame(None, -1, "")
       app.SetTopWindow(self.frame_1)
       self.frame_1.Show()
       app.MainLoop()
gui1=guithread()
gui1.start()
class changer(threading.Thread):
   def run(self):
       gui1.frame_1.text_ctrl_1.Setvalue("hello")
chang=changer()
chang.start()

and The GUI file (gui.py, imported in the above)
import wx

# begin wxGlade: extracode
# end wxGlade

class MyFrame(wx.Frame):
   def __init__(self, *args, **kwds):
       # begin wxGlade: MyFrame.__init__
       kwds["style"] = wx.DEFAULT_FRAME_STYLE
       wx.Frame.__init__(self, *args, **kwds)
       self.text_ctrl_1 = wx.TextCtrl(self, -1, "")
       self.slider_1 = wx.Slider(self, -1, 0, 0, 10)
       self.Open = wx.Button(self, -1, "Open")
       self.button_4 = wx.Button(self, -1, "Pause/Resume")
       self.button_5 = wx.Button(self, -1, "Quit")

       self.__set_properties()
       self.__do_layout()

       self.Bind(wx.EVT_COMMAND_SCROLL, self.slider, self.slider_1)
       self.Bind(wx.EVT_BUTTON, self.open, self.Open)
       self.Bind(wx.EVT_BUTTON, self.pause, self.button_4)
       self.Bind(wx.EVT_BUTTON, self.quit, self.button_5)
       # end wxGlade

   def __set_properties(self):
       # begin wxGlade: MyFrame.__set_properties
       self.SetTitle("frame_1")
       self.SetSize((522, 457))
       # end wxGlade

   def __do_layout(self):
       # begin wxGlade: MyFrame.__do_layout
       sizer_1 = wx.BoxSizer(wx.VERTICAL)
       sizer_2 = wx.BoxSizer(wx.VERTICAL)
       sizer_3 = wx.BoxSizer(wx.HORIZONTAL)
       sizer_2.Add(self.text_ctrl_1, 7, wx.EXPAND, 0)
       sizer_2.Add(self.slider_1, 0, wx.EXPAND, 0)
       sizer_3.Add(self.Open, 0, wx.LEFT, 70)
       sizer_3.Add((52, 20), 0, 0, 0)
       sizer_3.Add(self.button_4, 0, wx.ALIGN_CENTER_HORIZONTAL, 0)
       sizer_3.Add((55, 23), 0, 0, 0)
       sizer_3.Add(self.button_5, 0, 0, 0)
       sizer_2.Add(sizer_3, 1, wx.EXPAND, 0)
       sizer_1.Add(sizer_2, 1, wx.EXPAND, 0)
       self.SetSizer(sizer_1)
       self.Layout()
       # end wxGlade

   def slider(self, event): # wxGlade: MyFrame.<event_handler>
       print "Event handler `slider' not implemented!"
       event.Skip()

   def open(self, event): # wxGlade: MyFrame.<event_handler>
       print "Event handler `open' not implemented!"
       event.Skip()

   def pause(self, event): # wxGlade: MyFrame.<event_handler>
       print "Event handler `pause' not implemented!"
       event.Skip()

   def quit(self, event): # wxGlade: MyFrame.<event_handler>
       print "Event handler `quit' not implemented!"
       event.Skip()

# end of class MyFrame

if __name__ == "__main__":
   app = wx.PySimpleApp(0)
   wx.InitAllImageHandlers()
   frame_1 = MyFrame(None, -1, "")
   app.SetTopWindow(frame_1)
   frame_1.Show()
   app.MainLoop()

The problem here is that when i run the main file, i am told that
'guithread' object has no attribute 'frame_1' whereas i seem to have
defined
self.frame_1=gui.MyFrame etc.

The idea here is to access a gui element running in a thread from a
separate thread. Please help
regards
Soumen

Soumen banerjee wrote:

Hello,
I am using wxglade to design a gui which i am using in another script.
Here are the codes

The main file:
import wx,gui,threading
class guithread(threading.Thread):
   def run(self):
       app = wx.PySimpleApp(0)
       wx.InitAllImageHandlers()
       self.frame_1 = gui.MyFrame(None, -1, "")
       app.SetTopWindow(self.frame_1)
       self.frame_1.Show()
       app.MainLoop()
gui1=guithread()
gui1.start()
class changer(threading.Thread):
   def run(self):
       gui1.frame_1.text_ctrl_1.Setvalue("hello")
chang=changer()
chang.start()
...

The problem here is that when i run the main file, i am told that
'guithread' object has no attribute 'frame_1' whereas i seem to have
defined
self.frame_1=gui.MyFrame etc.
  
Your problem is simply one of timing. The second thread happens to get
to its first statement before the first thread has set a value for
self.frame_1.

Murphy's Law applies in strength to multithreaded programs. If there is
a worst possible timing scenario, your program will get it.

The idea here is to access a gui element running in a thread from a
separate thread.

In general, this is a dangerous thing to do. Windows are associated
with threads (the window messages for a given window are ALWAYS routed
to the message queue for the thread that created the window), and you
can cause strange issues by accessing a window from a different thread.

···

--
Tim Roberts, timr@probo.com
Providenza & Boekelheide, Inc.

Tim Roberts wrote:

Soumen banerjee wrote:
  

Hello,
I am using wxglade to design a gui which i am using in another script.
Here are the codes

The main file:
import wx,gui,threading
class guithread(threading.Thread):
   def run(self):
       app = wx.PySimpleApp(0)
       wx.InitAllImageHandlers()
       self.frame_1 = gui.MyFrame(None, -1, "")
       app.SetTopWindow(self.frame_1)
       self.frame_1.Show()
       app.MainLoop()
gui1=guithread()
gui1.start()
class changer(threading.Thread):
   def run(self):
       gui1.frame_1.text_ctrl_1.Setvalue("hello")
chang=changer()
chang.start()
...

The problem here is that when i run the main file, i am told that
'guithread' object has no attribute 'frame_1' whereas i seem to have
defined
self.frame_1=gui.MyFrame etc.
  
Your problem is simply one of timing. The second thread happens to get
to its first statement before the first thread has set a value for
self.frame_1.

Murphy's Law applies in strength to multithreaded programs. If there is
a worst possible timing scenario, your program will get it.

The idea here is to access a gui element running in a thread from a
separate thread.
    
In general, this is a dangerous thing to do. Windows are associated
with threads (the window messages for a given window are ALWAYS routed
to the message queue for the thread that created the window), and you
can cause strange issues by accessing a window from a different thread.

Yeah...if you want to access the GUI from a separate thread, you need to use threadsafe methods. The wiki has a few examples here:

http://wiki.wxpython.org/LongRunningTasks

···

-------------------
Mike Driscoll

Blog: http://blog.pythonlibrary.org