TypeError: in method 'new_MessageDialog', expected argument 1 of type 'wxWindow

why cant I create wxwidget in any class except the main class... when
ever I try to use a wxwidget and in this case wx.MessageDialog i get
and error "TypeError: in method 'new_MessageDialog', expected argument
1 of type 'wxWindow "

class InsertData(wx.Frame):
    def __init__(self, parent, id, title):
        wx.Frame.__init__(self, parent, id, title,
size=(990,700),style=wx.DEFAULT_FRAME_STYLE)
self.nsync = wx.Button(panel, -1, '&Sync', size=(-1, 30),
pos=(700,250))
  self.nsync.Bind(wx.EVT_BUTTON, self.rock,self.nsync)
def rock(self,event):
  SynceThread(self,event).start()

class SynceThread(threading.Thread):
    def __init__(self, parent, event):
        self.parent = parent
        self.event = event
        threading.Thread.__init__(self)
    def run(self):
  f =open("ipaddress.txt","r")
  self.ip = f.readline() # reads the lines
  f.close()
  host=self.ip
        port = 51269
        size = 1000000
        addr = (host,port)
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        try:
            f = open('duplicate_table2.txt', "r")
            data = f.read()
            f.close()
            s.connect(addr)
            s.send(data)
            data = s.recv(size)
            s.close()
            #print "Got it"
            os.remove('duplicate_table2.txt')
      nsync = wx.MessageDialog(self, "Data Was Transferred
Successfully", caption = "It Worked " , style = wx.OK |
wx.ICON_EXCLAMATION)
      nsync.ShowModal()
      nsync == wx.OK
      nsync.Destroy()
            return True
        except socket.error:
      traceback.print_exc(file=open("errlog.txt","a"))
      print"error"
      exc_type, exc_value, exc_tb = sys.exc_info()
      nsync1 = wx.MessageDialog(self, "Data Was Not Transferred
Successfully", caption = "Did Not Trnsfer " , style = wx.OK |
wx.ICON_EXCLAMATION)
      nsync1.ShowModal()
      nsync1 == wx.OK
      nsync1.Destroy()

        except IOError:
      traceback.print_exc(file=open("errlog.txt","a"))
            exc_type, exc_value, exc_tb = sys.exc_info()
      nsync1 = wx.MessageDialog(self, "Data Was Not Transferred
Successfully", caption = "Did Not Trnsfer " , style = wx.OK |
wx.ICON_EXCLAMATION)
      nsync1.ShowModal()
      nsync1 == wx.OK
      nsync1.Destroy()
        return False

Exception in thread Thread-3:
Traceback (most recent call last):
  File "C:\Python26\lib\threading.py", line 522, in __bootstrap_inner
    self.run()
  File "c:\Python26\sign\Version2_Client_Side_OCT13_socket_thread.py",
line 6504, in run
    nsync1 = wx.MessageDialog(self, "Data Was Not Transferred
Successfully", caption = "Did Not Trnsfer " , style = wx.OK |
wx.ICON_EXCLAMATION)
  File "C:\Python26\Lib\site-packages\wx-2.8-msw-ansi\wx\_windows.py",
line 2922, in __init__

_windows_.MessageDialog_swiginit(self,_windows_.new_MessageDialog(*args,
**kwargs))
TypeError: in method 'new_MessageDialog', expected argument 1 of type
'wxWindow

You can create a widget in any class you want - but the parent of that widget can’t be just anything. In this case, you are trying to pass a threading.Thread instance as the parent of the wx.MessageDialog. If you don’t care whether that dialog has a parent or not, just pass None as the parent instead.

Also, FYI, I’m almost certain this is going to crash under some circumstances, since you are creating GUI objects in child thread.

-Nat

···

On Wed, Oct 12, 2011 at 4:35 PM, aikidoguy clayrichmond1@gmail.com wrote:

why cant I create wxwidget in any class except the main class… when

ever I try to use a wxwidget and in this case wx.MessageDialog i get

and error "TypeError: in method ‘new_MessageDialog’, expected argument

1 of type 'wxWindow "

Thanks Nat what would be a more stable way to do this ? I am a self taught Newbie… thanks for you guidance

···

On Wed, Oct 12, 2011 at 7:41 PM, Nat Echols nathaniel.echols@gmail.com wrote:

On Wed, Oct 12, 2011 at 4:35 PM, aikidoguy clayrichmond1@gmail.com wrote:

why cant I create wxwidget in any class except the main class… when

ever I try to use a wxwidget and in this case wx.MessageDialog i get

and error "TypeError: in method ‘new_MessageDialog’, expected argument

1 of type 'wxWindow "

You can create a widget in any class you want - but the parent of that widget can’t be just anything. In this case, you are trying to pass a threading.Thread instance as the parent of the wx.MessageDialog. If you don’t care whether that dialog has a parent or not, just pass None as the parent instead.

Also, FYI, I’m almost certain this is going to crash under some circumstances, since you are creating GUI objects in child thread.

-Nat

To unsubscribe, send email to wxPython-users+unsubscribe@googlegroups.com

or visit http://groups.google.com/group/wxPython-users?hl=en

Well, just on general principle I’d decouple the thread and the GUI presentation completely - this isn’t a Python thing, it’s just good programming practice. (That’s in addition to the likelihood of it crashing as currently written, which is also a pretty general feature of most programming/GUI environments.) It’s getting awfully close to 5pm here so I don’t have time to send modified code right now, but I habitually do the following:

  1. Create a custom event class (derived from wx.PyEvent), which embeds whatever data or message you want to convey to the user inside it.

  2. Bind to this event type in the main window - the event handler should take the data from the custom event, and display the appropriate MessageDialog.

  3. You already have a reference to the main window in your thread, so when you reach the end of run(), create an instance of your custom event, and use wx.PostEvent() to send it to the main window. (Using events this way from a child thread is allowed.)

I believe there is a more abstract way to do this called “pubsub”, but I haven’t learned to use it yet (mostly out of laziness and impatience, not for any technical reason). I’m sure others will chime in though.

-Nat

···

On Wed, Oct 12, 2011 at 4:47 PM, Clay Richmond clayrichmond1@gmail.com wrote:

Thanks Nat what would be a more stable way to do this ?

thanks for the insight

···

On Wed, Oct 12, 2011 at 7:57 PM, Nat Echols nathaniel.echols@gmail.com wrote:

On Wed, Oct 12, 2011 at 4:47 PM, Clay Richmond clayrichmond1@gmail.com wrote:

Thanks Nat what would be a more stable way to do this ?

Well, just on general principle I’d decouple the thread and the GUI presentation completely - this isn’t a Python thing, it’s just good programming practice. (That’s in addition to the likelihood of it crashing as currently written, which is also a pretty general feature of most programming/GUI environments.) It’s getting awfully close to 5pm here so I don’t have time to send modified code right now, but I habitually do the following:

  1. Create a custom event class (derived from wx.PyEvent), which embeds whatever data or message you want to convey to the user inside it.
  1. Bind to this event type in the main window - the event handler should take the data from the custom event, and display the appropriate MessageDialog.
  1. You already have a reference to the main window in your thread, so when you reach the end of run(), create an instance of your custom event, and use wx.PostEvent() to send it to the main window. (Using events this way from a child thread is allowed.)

I believe there is a more abstract way to do this called “pubsub”, but I haven’t learned to use it yet (mostly out of laziness and impatience, not for any technical reason). I’m sure others will chime in though.

-Nat

To unsubscribe, send email to wxPython-users+unsubscribe@googlegroups.com

or visit http://groups.google.com/group/wxPython-users?hl=en

By the way, the code you sent is not runnable as a standalone example - in this case it is obvious to me what you are doing wrong, but as a general rule if you want debugging help it is best to attach a complete sample app so we (i.e. the list) can reproduce any problems you’re having. (Also, it’s much easier to reply with a modified working version when I have something more or less functional to start from.)

-Nat

···

On Wed, Oct 12, 2011 at 4:47 PM, Clay Richmond clayrichmond1@gmail.com wrote:

Thanks Nat what would be a more stable way to do this ? I am a self taught Newbie… thanks for you guidance

On Wed, Oct 12, 2011 at 7:41 PM, Nat Echols nathaniel.echols@gmail.com wrote:

On Wed, Oct 12, 2011 at 4:35 PM, aikidoguy clayrichmond1@gmail.com wrote:

why cant I create wxwidget in any class except the main class… when

ever I try to use a wxwidget and in this case wx.MessageDialog i get

and error "TypeError: in method ‘new_MessageDialog’, expected argument

1 of type 'wxWindow "

You can create a widget in any class you want - but the parent of that widget can’t be just anything. In this case, you are trying to pass a threading.Thread instance as the parent of the wx.MessageDialog. If you don’t care whether that dialog has a parent or not, just pass None as the parent instead.

Also, FYI, I’m almost certain this is going to crash under some circumstances, since you are creating GUI objects in child thread.

-Nat

To unsubscribe, send email to wxPython-users+unsubscribe@googlegroups.com

or visit http://groups.google.com/group/wxPython-users?hl=en

To unsubscribe, send email to wxPython-users+unsubscribe@googlegroups.com

or visit http://groups.google.com/group/wxPython-users?hl=en

Thanks I will do that in the future

···

On Wed, Oct 12, 2011 at 8:05 PM, Nat Echols nathaniel.echols@gmail.com wrote:

By the way, the code you sent is not runnable as a standalone example - in this case it is obvious to me what you are doing wrong, but as a general rule if you want debugging help it is best to attach a complete sample app so we (i.e. the list) can reproduce any problems you’re having. (Also, it’s much easier to reply with a modified working version when I have something more or less functional to start from.)

-Nat

On Wed, Oct 12, 2011 at 4:47 PM, Clay Richmond clayrichmond1@gmail.com wrote:

Thanks Nat what would be a more stable way to do this ? I am a self taught Newbie… thanks for you guidance

On Wed, Oct 12, 2011 at 7:41 PM, Nat Echols nathaniel.echols@gmail.com wrote:

On Wed, Oct 12, 2011 at 4:35 PM, aikidoguy clayrichmond1@gmail.com wrote:

why cant I create wxwidget in any class except the main class… when

ever I try to use a wxwidget and in this case wx.MessageDialog i get

and error "TypeError: in method ‘new_MessageDialog’, expected argument

1 of type 'wxWindow "

You can create a widget in any class you want - but the parent of that widget can’t be just anything. In this case, you are trying to pass a threading.Thread instance as the parent of the wx.MessageDialog. If you don’t care whether that dialog has a parent or not, just pass None as the parent instead.

Also, FYI, I’m almost certain this is going to crash under some circumstances, since you are creating GUI objects in child thread.

-Nat

To unsubscribe, send email to wxPython-users+unsubscribe@googlegroups.com

or visit http://groups.google.com/group/wxPython-users?hl=en

To unsubscribe, send email to wxPython-users+unsubscribe@googlegroups.com

or visit http://groups.google.com/group/wxPython-users?hl=en

To unsubscribe, send email to wxPython-users+unsubscribe@googlegroups.com

or visit http://groups.google.com/group/wxPython-users?hl=en