Wxpython focus and crash problem

I would like to implement drag & drop in wxPython that works in a similar way to WordPad / Eclipse etc. I mean the following:

when something falls on WordPad, WordPad is on top, focus and text are added. In Eclipse, the editor text is pasted, the Eclipse window gets focus and is on top.

When I implement drag & drop using wxPython target window, it is not brought to front. I applied drag & drop in a similar way (drag):

import wx

class DragFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None)
        self.tree = wx.TreeCtrl(self, wx.ID_ANY)
        root = self.tree.AddRoot("root item")
        self.tree.AppendItem(root, "child 1")
        self.tree.Bind(wx.EVT_TREE_BEGIN_DRAG, self.__onBeginDrag)        
    def __onBeginDrag(self, event):
        tdo = wx.PyTextDataObject(self.tree.GetItemText(event.GetItem()))
        dropSource = wx.DropSource(self.tree)
        dropSource.SetData(tdo)
        dropSource.DoDragDrop(True)

app = wx.PySimpleApp()
frame = DragFrame()
app.SetTopWindow(frame)
frame.Show()
app.MainLoop()

      


Second program (fall):

import wx
class TextDropTarget(wx.TextDropTarget):
    def __init__(self, obj):
        wx.TextDropTarget.__init__(self)
        self.obj = obj
    def OnDropText(self, x, y, data):
        self.obj.WriteText(data + '\n\n')
        wx.MessageBox("Error", "Error", style = wx.ICON_ERROR)

class DropFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None)
        text = wx.TextCtrl(self, wx.ID_ANY)
        text.SetDropTarget(TextDropTarget(text))

app = wx.PySimpleApp()
frame = DropFrame()
app.SetTopWindow(frame)
frame.Show()
app.MainLoop()

      

When you run both programs, place the windows in the center of the screen (part of the dropdown window is visible) and then drag the node from the drop window to the drop window - the target window displays a message box that is not visible, the target window is hidden behind the source window.

How do I implement drag & drop that will focus on the second (target) window? I tried adding window.Show (), window.SetFocus (), even using some WinAPI functionality (via win32gui). I think there must be some standard way of doing this. What am I missing?

+2


source to share


2 answers


You need to do whatever you want using the DragOver method of the DropTarget method eg. there you can raise and set focus on your window

example working code for target



import wx
class TextDropTarget(wx.TextDropTarget):
    def __init__(self, obj, callback):
        wx.TextDropTarget.__init__(self)
        self.obj = obj
        self._callback = callback

    def OnDropText(self, x, y, data):
        self.obj.WriteText(data + '\n\n')
        wx.MessageBox("Error", "Error", style = wx.ICON_ERROR)

    def OnDragOver(self, *args):
        wx.CallAfter(self._callback)
        return wx.TextDropTarget.OnDragOver(self, *args)

class DropFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None)
        text = wx.TextCtrl(self, wx.ID_ANY)
        text.SetDropTarget(TextDropTarget(text, self._callback))

    def _callback(self):
        self.Raise()
        self.SetFocus()

app = wx.PySimpleApp()
frame = DropFrame()
app.SetTopWindow(frame)
frame.Show()
app.MainLoop()

      

+1


source


Doesn't this work?

class DropFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None)
        text = wx.TextCtrl(self, wx.ID_ANY)
        self.SetFocus() # Set the focus to this window, allowing it to receive keyboard input.
        text.SetDropTarget(TextDropTarget(text))

      

wx.Frame

inherits from wx.Window

, which has SetFocus(self)

.




I just tested it and it works. Just moved SetFocus

up SetDropTarget

as this behavior is cleaner.

0


source







All Articles