[英]Trying to make draggable children of a scrolledwindow in wXPython
我正在嘗試創建其父級為ScrolledWindow的子級,並攔截鼠標拖動以允許將子級完全定位在父級的客戶區域內。 我希望客戶端虛擬滾動區域根據需要擴展以適應子代的位置(以便滾動大於窗口的圖)。 拖動似乎是有問題的,因為它想一直向左移動試圖將對象向左或向上拖動是有問題的。 當小部件超出當前窗口邊界時,它也不滾動。 在使用此代碼的驅動程序中,所有ConceptNode小部件都將SemNetWidget指定為其父級。 我不使用大小調整器,因為應該保持孩子的絕對位置(拖動位置)。 我不認為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()
司機:
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()
您應該創建一個小的可運行示例,以演示您的操作: http : //wiki.wxpython.org/MakingSampleApps
同時,如果您只是四處移動自己繪制的對象,那么您可能會發現DragImage演示很有用。 可以在wxPython演示中找到它。 否則,我建議在正式的wxPython郵件列表/ Google網上論壇上詢問,因為那里有很多經驗豐富的wxPython開發人員: https ://groups.google.com/forum/?pli =1#! forum/wxpython-users
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.