简体   繁体   English

尝试在wXPython中使滚动窗口成为可拖动的子代

[英]Trying to make draggable children of a scrolledwindow in wXPython

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. 我正在尝试创建其父级为ScrolledWindow的子级,并拦截鼠标拖动以允许将子级完全定位在父级的客户区域内。 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. 在使用此代码的驱动程序中,所有ConceptNode小部件都将SemNetWidget指定为其父级。 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: 我不认为wxPython提供了一种按中心而不是按角定位对象的方法,因为它会使其中的一些代码更容易编写:

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()

Driver: 司机:

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()

You should create a small runnable example that demonstrates what you're doing: http://wiki.wxpython.org/MakingSampleApps 您应该创建一个小的可运行示例,以演示您的操作: http : //wiki.wxpython.org/MakingSampleApps

In the meantime, if you're just moving around objects that you drew yourself, then you might find the DragImage demo useful. 同时,如果您只是四处移动自己绘制的对象,那么您可能会发现DragImage演示很有用。 It can be found in the wxPython demo. 可以在wxPython演示中找到它。 Otherwise, I would recommend asking on the official wxPython mailing list / Google group as there are a lot of experienced wxPython developers there: https://groups.google.com/forum/?pli=1#!forum/wxpython-users 否则,我建议在正式的wxPython邮件列表/ Google网上论坛上询问,因为那里有很多经验丰富的wxPython开发人员: https ://groups.google.com/forum/?pli =1#! forum/wxpython-users

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM