Trying to create a wxEvent or wx.CommandEvent Object

Hello

I have an application that has a TextCtrl & when I drag & drop a file
onto it I want to create a wx.Event object (or wx.CommandEvent) , set
the wxEvent's event object to the file dropped in the TextCtrl
( event.SetEventObject( file )) then send that Event object in a
function.

I know that I should not create my own wxEvent objects but right now
its the best way to do it.

But I get an error when I go to set the wxEvent's event object which
says it expects an wxObject as the parameter. Can you tell me how to
fix this?

Background info:
My app architecture is the MVC model. So when my FileDrop object(an
extended wx.FileDropTarget object) has a file dropped into it, I call
the 'View' class's/component's on_file_drop_command( self, event )
function. Therefore you can see the need to create an event object &
pass it to the 'View' class.

My code:

class FileDrop( wx.FileDropTarget ):

    def __init__( self, _window, _parent_frame ):
        """ """

        wx.FileDropTarget.__init__( self )

        self.window = _window
        self.parent_frame = _parent_frame

    def OnDropFiles( self, x, y, fileNames ):
        """ """

        file_list = []

        for file in fileNames:

            event = wx.CommandEvent( wx.wxEVT_DROP_FILES )
            event.SetEventObject( wx.wxObject( file ) ) # fails here
            self.parent_frame.on_input_change( event )

My view class:

class ApplicationView:

    def on_input_file_change_command( self, event ):
        """ """

        evt_type = event.GetEventType()

        if evt_type == wx.EVT_TEXT:

            file_name = self.input_tf.GetValue()

        else: # this will be when I get the file drop occurence

            file_name = event.GetEventObject()

        self.controller.window_proc( GS_INPUT_FILE_CHANGE, file_name )

I think using PubSub would be easier than trying to use events,
although the wx.PostEvent syntax isn't too hard.

···

On Oct 2, 3:07 am, Sascha <nill...@yahoo.com> wrote:

Hello

I have an application that has a TextCtrl & when I drag & drop a file
onto it I want to create a wx.Event object (or wx.CommandEvent) , set
the wxEvent's event object to the file dropped in the TextCtrl
( event.SetEventObject( file )) then send that Event object in a
function.

I know that I should not create my own wxEvent objects but right now
its the best way to do it.

But I get an error when I go to set the wxEvent's event object which
says it expects an wxObject as the parameter. Can you tell me how to
fix this?

Background info:
My app architecture is the MVC model. So when my FileDrop object(an
extended wx.FileDropTarget object) has a file dropped into it, I call
the 'View' class's/component's on_file_drop_command( self, event )
function. Therefore you can see the need to create an event object &
pass it to the 'View' class.

My code:

class FileDrop( wx.FileDropTarget ):

def \_\_init\_\_\( self, \_window, \_parent\_frame \):
    &quot;&quot;&quot; &quot;&quot;&quot;

    wx\.FileDropTarget\.\_\_init\_\_\( self \)

    self\.window       = \_window
    self\.parent\_frame = \_parent\_frame

def OnDropFiles\( self, x, y, fileNames \):
    &quot;&quot;&quot; &quot;&quot;&quot;

    file\_list = \[\]

    for file in fileNames:

        event = wx\.CommandEvent\( wx\.wxEVT\_DROP\_FILES \)
        event\.SetEventObject\( wx\.wxObject\( file \) \)   \# fails here
        self\.parent\_frame\.on\_input\_change\( event \)

My view class:

class ApplicationView:

def on\_input\_file\_change\_command\( self, event \):
    &quot;&quot;&quot; &quot;&quot;&quot;

    evt\_type = event\.GetEventType\(\)

    if evt\_type == wx\.EVT\_TEXT:

        file\_name = self\.input\_tf\.GetValue\(\)

    else:  \# this will be when I get the file drop occurence

        file\_name = event\.GetEventObject\(\)

    self\.controller\.window\_proc\( GS\_INPUT\_FILE\_CHANGE, file\_name \)

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

Blog: http://blog.pythonlibrary.org

Also see: wxPython API Documentation — wxPython Phoenix 4.2.2 documentation

···

On 4 October 2010 15:05, Mike Driscoll <kyosohma@gmail.com> wrote:

On Oct 2, 3:07 am, Sascha <nill...@yahoo.com> wrote:

Hello

I have an application that has a TextCtrl & when I drag & drop a file
onto it I want to create a wx.Event object (or wx.CommandEvent) , set
the wxEvent's event object to the file dropped in the TextCtrl
( event.SetEventObject( file )) then send that Event object in a
function.

I know that I should not create my own wxEvent objects but right now
its the best way to do it.

But I get an error when I go to set the wxEvent's event object which
says it expects an wxObject as the parameter. Can you tell me how to
fix this?

Background info:
My app architecture is the MVC model. So when my FileDrop object(an
extended wx.FileDropTarget object) has a file dropped into it, I call
the 'View' class's/component's on_file_drop_command( self, event )
function. Therefore you can see the need to create an event object &
pass it to the 'View' class.

My code:

class FileDrop( wx.FileDropTarget ):

def \_\_init\_\_\( self, \_window, \_parent\_frame \):
    &quot;&quot;&quot; &quot;&quot;&quot;

    wx\.FileDropTarget\.\_\_init\_\_\( self \)

    self\.window       = \_window
    self\.parent\_frame = \_parent\_frame

def OnDropFiles\( self, x, y, fileNames \):
    &quot;&quot;&quot; &quot;&quot;&quot;

    file\_list = \[\]

    for file in fileNames:

        event = wx\.CommandEvent\( wx\.wxEVT\_DROP\_FILES \)
        event\.SetEventObject\( wx\.wxObject\( file \) \)   \# fails here
        self\.parent\_frame\.on\_input\_change\( event \)

My view class:

class ApplicationView:

def on\_input\_file\_change\_command\( self, event \):
    &quot;&quot;&quot; &quot;&quot;&quot;

    evt\_type = event\.GetEventType\(\)

    if evt\_type == wx\.EVT\_TEXT:

        file\_name = self\.input\_tf\.GetValue\(\)

    else:  \# this will be when I get the file drop occurence

        file\_name = event\.GetEventObject\(\)

    self\.controller\.window\_proc\( GS\_INPUT\_FILE\_CHANGE, file\_name \)

I think using PubSub would be easier than trying to use events,
although the wx.PostEvent syntax isn't too hard.

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

Blog: http://blog.pythonlibrary.org

--
To unsubscribe, send email to wxPython-users+unsubscribe@googlegroups.com
or visit http://groups.google.com/group/wxPython-users?hl=en

The EventObject attribute is supposed to be the widget from which the event originates. If you use wx.PyEvent or wx.PyCommandEvent as your base class (or use wx.lib.newevent to create a new class) then you can assign your own attributes to the event object just like any other Python object:

  evt.droppedFile = theFile

···

On 10/2/10 1:07 AM, Sascha wrote:

Hello

I have an application that has a TextCtrl& when I drag& drop a file
onto it I want to create a wx.Event object (or wx.CommandEvent) , set
the wxEvent's event object to the file dropped in the TextCtrl
( event.SetEventObject( file )) then send that Event object in a
function.

I know that I should not create my own wxEvent objects but right now
its the best way to do it.

But I get an error when I go to set the wxEvent's event object which
says it expects an wxObject as the parameter. Can you tell me how to
fix this?

--
Robin Dunn
Software Craftsman