python中的拖动源

#coding=utf-8
import wx
class DragController(wx.Control):

"""Just a little control to handle dragging the text from a text
control. We use a separate control so as to not interfere with
the native drag-select functionality of the native text control."""

def __init__(self, parent, source, size=(25,25)):
wx.Control.__init__(self, parent, -1, size=size,
style=wx.SIMPLE_BORDER)
self.source = source
self.SetMinSize(size)
self.Bind(wx.EVT_PAINT, self.OnPaint)
self.Bind(wx.EVT_LEFT_DOWN, self.OnLeftDown)

def OnPaint(self, evt):
# draw a simple arrow
dc = wx.BufferedPaintDC(self)
dc.SetBackground(wx.Brush(self.GetBackgroundColour()))
dc.Clear()
w, h = dc.GetSize()
y = h/2
dc.SetPen(wx.Pen("dark blue",2))
dc.DrawLine(w/8,y,w-w/8,y)
def OnLeftDown(self,evt):
text=self.source.GetValue()
data=wx.TextDataObject(text)
dropSource=wx.DropSource(self)#创建释放源
dropSource.SetData(data)#设置数据
result=dropSource.DoDragDrop(wx.Drag_AllowMove)#执行释放
#iftheuserwantstomovethedatathenweshoulddeleteit
#fromthesource
if result== wx.DragMove:
self.source.SetValue("")#如果需要的话,删除源中的数据

class MyFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self,None,title="DropSource")
self.SetSize((400,400))
p=wx.Panel(self)
#createthecontrols
label1=wx.StaticText(p,-1,"Put some text in this control:",size=(200,100))
label2=wx.StaticText(p,-1,
"Then drag from the neighboring bitmap and\n"
"drop in an application that accepts dropped\n"
"text,such as MSWord.",size=(200,100))
text=wx.TextCtrl(p,-1,"Sometext")
dragctl=DragController(p,text)
#setupthelayoutwithsizers
sizer=wx.BoxSizer(wx.VERTICAL)
sizer.Add(label1,0,wx.ALL,5)
hrow=wx.BoxSizer(wx.HORIZONTAL)
hrow.Add(text,1,wx.RIGHT,5)
hrow.Add(dragctl,0)
sizer.Add(hrow,0,wx.EXPAND|wx.ALL,5)
p.SetSizer(sizer)
sizer.Fit(self)
app=wx.PySimpleApp()
frm=MyFrame()
frm.Show()
app.MainLoop()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值