wxPython DnD stops working after SetSizeHints

  • Thread starter Emiliano Molina
  • Start date
E

Emiliano Molina

This has been driving me crazy for a couple of days and I have finally
narrowed it down to the following code. If the commented section is
uncommented the drag and drop handler is never called. I have included
the .xrc file for reference.

Any help is greatly appreciated. An explanation would be great, but at
the moment I would be eternally grateful for a workaround!

Thanks in advance for those of you that spend some time helping out.

Here is the blasted code,

from wxPython.wx import *
from wxPython.xrc import *

class TestFrame(wxFrame):
def __init__(self,parent,ID):
wxFrame.__init__(self,parent,ID,"test
frame",(100,30),(70,60),wxDEFAULT_FRAME_STYLE)
self.panel=wxPanel(self,-1)

class FileDropTarget(wxFileDropTarget):
def __init__(self, window):
wxFileDropTarget.__init__(self)

def OnDropFiles(self, x, y, filenames):
print filenames

class App(wxApp):
def OnInit(self):

self.res=wxXmlResource("test.xrc")
self.frame=self.res.LoadFrame(None,"FRAME1")
self.frame.panel=XRCCTRL(self.frame,"test_list")

dt=FileDropTarget(self.frame)
self.frame.panel.SetDropTarget(dt)

# The following lines break the drag and drop
# self.panel=XRCCTRL(self.frame,"panel")
# sizer=self.panel.GetSizer()
# sizer.SetSizeHints(self.frame)

self.frame.Show()
self.SetTopWindow(self.frame)

return True


def main():
app=App(0)
app.MainLoop()


if __name__=="__main__":
main()


and here the XRC

<?xml version="1.0" ?>
<resource>
<object class="wxFrame" name="FRAME1">
<title></title>
<object class="wxPanel" name="panel">
<size>100,100</size>
<object class="wxBoxSizer">
<orient>wxVERTICAL</orient>
<object class="sizeritem">
<object class="wxStaticBoxSizer">
<label>test_list</label>
<orient>wxVERTICAL</orient>
<object class="sizeritem">
<object class="wxListBox" name="test_list">
<content/>
</object>
</object>
</object>
</object>
</object>
</object>
<size>100,100</size>
</object>
</resource>
 
B

Brett Calcott

"Emiliano Molina" wrote
<snip>
from wxPython.wx import *
from wxPython.xrc import *

class TestFrame(wxFrame):
def __init__(self,parent,ID):
wxFrame.__init__(self,parent,ID,"test frame",(100,30),(70,60),wxDEFAULT_FRAME_STYLE)
self.panel=wxPanel(self,-1)
This gets overwritten -^
class FileDropTarget(wxFileDropTarget):
def __init__(self, window):
unused --------------------^
wxFileDropTarget.__init__(self)

def OnDropFiles(self, x, y, filenames):
print filenames

class App(wxApp):
def OnInit(self):

self.res=wxXmlResource("test.xrc")
self.frame=self.res.LoadFrame(None,"FRAME1")
self.frame.panel=XRCCTRL(self.frame,"test_list")

dt=FileDropTarget(self.frame)
self.frame.panel.SetDropTarget(dt)

# The following lines break the drag and drop
# self.panel=XRCCTRL(self.frame,"panel")
# sizer=self.panel.GetSizer()
# sizer.SetSizeHints(self.frame)

What is self.panel, as opposed to self.frame.panel?
self.frame.Show()
self.SetTopWindow(self.frame)

return True


Hi Emiliano, it isn't clear what you are trying to do here, you seem to
have a panel being created in 3 different places -- which one do you
want? I suspect that the self.panel, is hiding the other panel
that you created (self.frame.panel) which is the drop target (just a
guess)

I've never used xrc, but this works for me (is it what you want?)

class App(wxApp):
def OnInit(self):

self.res=wxXmlResource("test.xrc")
self.frame=self.res.LoadFrame(None,"FRAME1")
self.frame.panel=XRCCTRL(self.frame,"panel")

dt=FileDropTarget()
self.frame.panel.SetDropTarget(dt)

# The following lines break the drag and drop
sizer=self.frame.panel.GetSizer()
sizer.SetSizeHints(self.frame)

self.frame.Show()
self.SetTopWindow(self.frame)

return True
 
E

Emiliano Molina

self.panel=wxPanel(self,-1)
This gets overwritten -^


unused --------------------^

Yes, you are 100% right, its there because I have been cutting
down the original program bit by bit to find the problem! It
should no longer be there.
What is self.panel, as opposed to self.frame.panel?

self.panel is where I am temporarily storing the window that
I am adding the DnD to. One of the steps that I took when
trying to work out where the problem was was to start with
nothing but a frame and a panel and see if I could drop files
into the panel. That worked fine, it was only when I added a
sizer and then had to fit and set hints that DnD broke.

I'm sorryt that the naming is a bit confusing, I posted this
last night as a last desperate attempt before going to bed!
Hi Emiliano, it isn't clear what you are trying to do here, you seem to
have a panel being created in 3 different places -- which one do you
want? I suspect that the self.panel, is hiding the other panel
that you created (self.frame.panel) which is the drop target (just a
guess)

The confusion is understandable, I should have spent more time
tidying up the code before posting.

self.frame.panel is used to store the retrieved control to which
the DnD handler is attached. Its called panel because originally
it WAS a panel, its now a wxListBox.

self.panel is used to store the retrieved panel (this one IS a panel,
sorry for the confusion) from which we can then obtain the main sizer
for the frame. If we don't have the sizer we can't call SetSizeHints
to arrange the controls in the window. Its the calling of SetSizeHints
that seems to break the DnD handling. If I don't call SetSizeHints
then DnD works fine and I can drop a file onto the list in the frame
and its name is printed. The frame looks terrible of course, the
controls are only arranged properly when I call SetSizeHints.
I've never used xrc, but this works for me (is it what you want?)

some helpful code

return True

I tried the code you sent on the iBook and no luck, but you mentioned
that it worked so I sent it over to the PC and tried it again. It worked!

I had a close look at it and you have changed the drop target from the
list to the main panel. I did the same with my code on the PC and it
also worked. So the conclusion is that neither your or my code works on
the Mac. Both sets of code work on the PC as long as the drop target is
the panel and not the list.

Thanks for your help and time. Now I have to figure out what is
happening on the Mac.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,069
Latest member
SimplyleanKetoReviews

Latest Threads

Top