Note that there are some explanatory texts on larger screens.

plurals
  1. POTrying to make draggable children of a scrolledwindow in wXPython
    primarykey
    data
    text
    <p>I am trying to create children whose parent is a ScrolledWindow and intercept mouse drags to allow repositioning children absolutely within the client area of the parent. I want the client virtual scrolled area to expand as needed to accommodate the position of the children (so a larger-than-window graph gets scrolled). The dragging seems to be problematic since it wants to move by the corners all the time trying to drag an object left or up is problematic. It's also not scrolling when the widgets go beyond the current window boundaries. In the driver using this code all the ConceptNode widgets specify the SemNetWidget as their parent. I'm not using using a sizer since the absolute positioning (dragged positions) of the children should be maintained. I don't suppose wxPython provides a way to position objects by centers rather than by corners as it would make some of this much easier to code:</p> <pre><code>class SemNetWidget(wx.ScrolledWindow): def __init__(self,edit,*args,**kwargs): self.editor=edit super(SemNetWidget,self).__init__(*args,**kwargs) self.SetScrollbars(1,1,1,1) class ConceptNode(wx.StaticText): count=0 def __init__(self,nm,*args,**kwargs): if not kwargs.has_key("style"): kwargs["style"]=0 kwargs["style"]=wx.SIMPLE_BORDER|wx.ALIGN_CENTRE super(ConceptNode,self).__init__(*args,**kwargs) par=args[0] self.nm=nm self.mcap=False self.par=par self.SetLabel(" %s " % self.nm) self.Move((0,15*self.count)) # so new nodes don't overlap self.par.FitInside() self.Bind(wx.EVT_MOUSE_EVENTS,self.onDrag) self.Bind(wx.EVT_MOTION,self.onDrag) self.Bind(wx.EVT_MOUSE_CAPTURE_LOST,self.onUncap) ConceptNode.count+=1 def onUncap(self,evt): self.mcap=False self.drag=None def onDrag(self,evt): if evt.Dragging() and self.drag is not None: #cdc=wx.ClientDC(self) #self.PrepareDC(cdc) #pos=list(evt.GetLogicalPosition(cdc)) pos=evt.GetPosition() dx=pos[0]-self.drag['x'] dy=pos[1]-self.drag['y'] self.SetPosition((self.drag['ox']+dx, self.drag['oy']+dy), wx.SIZE_ALLOW_MINUS_ONE) if evt.LeftDown(): pos=evt.GetPosition() opos=self.GetPosition() self.drag={'x':pos[0],'y':pos[1], 'ox':opos[0],'oy':opos[1]} self.CaptureMouse() self.mcap=True evt.Skip() if evt.LeftUp(): self.drag=None if self.mcap: self.ReleaseMouse() </code></pre> <p>Driver:</p> <pre><code>if __name__=="__main__": app=wx.App() window=wx.Frame(None,wx.ID_ANY) frame=SemNetWidget(None,window) # None:=No editor object c1=ConceptNode("Concept1",frame) c2=ConceptNode("Concept2",frame) window.Show() app.MainLoop() </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload