我正在按照我想知道的步骤在拖放中寻找“重置”选项。
导入wx
Class MyTarget(wx.TextDropTarget): def init (自身,对象): wx.TextDropTarget。初始化(自己) self.object =对象
def OnDropText(self,x,y,data): self.object.InsertStringItem(0,数据)
Mywin类(wx.Frame):
def __init__(self, parent, title):
super(Mywin, self).__init__(parent, title = title,size = (-1,300))
panel = wx.Panel(self)
self.index = 0
box = wx.BoxSizer(wx.HORIZONTAL) #horizontal line
languages = ['C', 'C++', 'Java', 'Python', 'Perl', 'JavaScript',
'PHP', 'VB.NET','C#']
self.lst1 = wx.ListCtrl(panel, -1, style = wx.LC_LIST)
self.lst2 = wx.ListCtrl(panel, -1, style = wx.LC_LIST)
self.display = wx.Button(panel,label="Reset",pos=(10,90))
#self.but = wx.Button(panel,-1,"Press")
for lang in languages:
self.lst1.InsertStringItem(0,lang)
#Completed--------
#self.lst2.InsertStringItem(0,lang)
dt = MyTarget(self.lst2)
self.lst2.SetDropTarget(dt)
wx.EVT_LIST_BEGIN_DRAG(self, self.lst1.GetId(), self.OnDragInit)
dt = MyTarget(self.lst1)
self.lst1.SetDropTarget(dt)
wx.EVT_LIST_BEGIN_DRAG(self, self.lst2.GetId(), self.OnDragInit1)
#Completed++++++++++
self.display.Bind(wx.EVT_BUTTON,self.onReset)
box.Add(self.lst1,1,wx.EXPAND)
box.Add(self.lst2, 1, wx.EXPAND)
box.Add(self.display,1,wx.EXPAND)
panel.SetSizer(box)
panel.Fit()
self.Centre()
self.Show(True)
def OnDragInit(self, event):
text = self.lst1.GetItemText(event.GetIndex())
tobj = wx.TextDataObject(text)
src = wx.DropSource(self.lst1)
src.SetData(tobj)
src.DoDragDrop(True)
self.lst1.DeleteItem(event.GetIndex())
def OnDragInit1(self,event):
text = self.lst2.GetItemText(event.GetIndex())
tobj = wx.TextDataObject(text)
src = wx.DropSource(self.lst2)
sit = src.SetData(tobj)
src.DoDragDrop(True)
self.lst2.DeleteItem(event.GetIndex())
"""def onReset(self,event):
languages = ['C', 'C++', 'Java', 'Python', 'Perl', 'JavaScript',
'PHP', 'VB.NET','C#']
self.lst1.SetData(languages)
#print("Hello")
pass"""
def onReset(self, event):
""""""
#||||||||Clearing the values||||||||
#-----------------------------------
self.lst1.ClearAll()
self.lst2.ClearAll()
languages = ['C', 'C++', 'Java', 'Python', 'Perl', 'JavaScript',
'PHP', 'VB.NET','C#']
self.lst1 = wx.ListCtrl(self, -1, style = wx.LC_LIST)
#self.but = wx.Button(panel,-1,"Press")
for lang in languages:
self.lst1.InsertStringItem(0,lang)
"""count = self.lst1.GetItemCount()
for row in range(count):
item = self.lst1.GetItem( itemIdx=row, col=0)
print (item.GetText())"""
#font.SetWeight(wx.FONTWEIGHT_BOLD)
# self.lst1.SetItemFont(self.index)
ex = wx.App() Mywin(无,“拖放演示”) 例如MainLoop()`
答案 0 :(得分:1)
首先,请允许我列出您的代码中的问题,而不是Reset
问题。
您的缩进是一团糟,很难理解,因此许多可以回答您问题的人都不会打扰。
使人们易于回答您的问题。
您似乎使用的是wxPython
或教科书的非常旧的版本,不建议使用许多项目。
绑定事件的方法是历史性的。
InsertStringItem
已过时。
在MyTarget(wx.TextDropTarget)
中,两个init
引用都应为__init__
MyTarget(wx.TextDropTarget)
应该返回True
languages
只能定义一次。
最后,在self.Update
末尾使用OnReset
重新显示列表。
检查代码与下面的代码之间的区别。
import wx
class MyTarget(wx.TextDropTarget):
def __init__(self, object):
wx.TextDropTarget.__init__(self)
self.object = object
def OnDropText(self, x, y, data):
self.object.InsertItem(0, data)
return True
class Mywin(wx.Frame):
def __init__(self, parent, title):
super(Mywin, self).__init__(parent, title = title,size = (-1,300))
panel = wx.Panel(self)
self.index = 0
box = wx.BoxSizer(wx.HORIZONTAL) #horizontal line
self.languages = ['C', 'C++', 'Java', 'Python', 'Perl', 'JavaScript',
'PHP', 'VB.NET','C#']
self.lst1 = wx.ListCtrl(panel, -1, style = wx.LC_LIST)
self.lst2 = wx.ListCtrl(panel, -1, style = wx.LC_LIST)
self.display = wx.Button(panel,label="Reset",pos=(10,90))
for lang in self.languages:
self.lst1.InsertItem(0,lang)
#Completed--------
#self.lst2.InsertStringItem(0,lang)
dt = MyTarget(self.lst2)
self.lst2.SetDropTarget(dt)
self.lst1.Bind(wx.EVT_LIST_BEGIN_DRAG, self.OnDragInit)
dt = MyTarget(self.lst1)
self.lst1.SetDropTarget(dt)
self.lst2.Bind(wx.EVT_LIST_BEGIN_DRAG, self.OnDragInit1)
#Completed++++++++++
self.display.Bind(wx.EVT_BUTTON,self.onReset)
box.Add(self.lst1,1,wx.EXPAND)
box.Add(self.lst2, 1, wx.EXPAND)
box.Add(self.display,1,wx.EXPAND)
panel.SetSizer(box)
panel.Fit()
self.Centre()
self.Show(True)
def OnDragInit(self, event):
text = self.lst1.GetItemText(event.GetIndex())
tobj = wx.TextDataObject(text)
src = wx.DropSource(self.lst1)
src.SetData(tobj)
src.DoDragDrop(True)
self.lst1.DeleteItem(event.GetIndex())
def OnDragInit1(self,event):
text = self.lst2.GetItemText(event.GetIndex())
tobj = wx.TextDataObject(text)
src = wx.DropSource(self.lst2)
sit = src.SetData(tobj)
src.DoDragDrop(True)
self.lst2.DeleteItem(event.GetIndex())
def onReset(self, event):
#||||||||Clearing the values||||||||
#-----------------------------------
self.lst1.ClearAll()
self.lst2.ClearAll()
for lang in self.languages:
self.lst1.InsertItem(0,lang)
self.Update()
ex = wx.App()
Mywin(None,'Drag&Drop Demo')
ex.MainLoop()