删除 ListCtrl 中的项目 Python
Delete item in ListCtrl Python
如何清除 Python 中的列表 ctrl?
例如,当我插入第 10 个项目时,前 8 个项目从 listCtrl 中删除,可以继续在 listCtrl 中输入项目。
我该怎么做?
示例:
我插入:1,2,3,4,5,6,7,8,9,10 在我在 listCtrl 中看到的第 10 个项目之后只有 8,9,10,我可以再次插入项目。
我在示例中添加了一些代码。
import wx
import wx.gizmos as gizmos
import time
import datetime
from datetime import timedelta
class CWindow(wx.Frame):
def __init__(self, parent, id, title):
wx.Frame.__init__(self, parent, id, title, style = wx.DEFAULT_FRAME_STYLE & ~ wx.CLOSE_BOX ^ wx.MAXIMIZE_BOX ^ wx.RESIZE_BORDER, size=(600,500))
dataCorrente = datetime.datetime.now()
self.Panel = wx.Panel(self, -1)
self.index = 0
self.ceck = {}
self.ts = ""
self.CTesto = wx.TextCtrl(self.Panel, 1, pos=(10,40), style=wx.TE_PROCESS_ENTER)
self.CTesto.Bind(wx.EVT_TEXT_ENTER,self.add_line)
self.BtnConferma = wx.Button(self.Panel, 12, "Conferma", pos=(130,38))
self.list_ctrl = wx.ListCtrl(self.Panel, pos=(10,90),size=(-1,330),style=wx.LC_REPORT|wx.BORDER_SUNKEN)
self.list_ctrl.InsertColumn(0, 'Name')
self.list_ctrl.InsertColumn(1, 'Start')
self.list_ctrl.InsertColumn(2, 'Finish', width=100)
wx.Button(self.Panel, 22, "Exit", pos=wx.Point(470,400))
self.Bind(wx.EVT_BUTTON, self.close, id=22)
self.Bind(wx.EVT_BUTTON, self.add_line, self.BtnConferma, id=12)
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(self.list_ctrl, 0, wx.ALL|wx.EXPAND, 5)
sizer.Add(self.BtnConferma, 0, wx.ALL|wx.CENTER, 5)
self.led = gizmos.LEDNumberCtrl(self.Panel, -1, pos = (350,25), size = (200,50), style = gizmos.LED_ALIGN_CENTER)
self.led.SetBackgroundColour("#c0c0c0")
self.led.SetForegroundColour("black")
self.OnTimer(None)
self.timer = wx.Timer(self, -1)
self.timer.Start(1000)
self.Bind(wx.EVT_TIMER, self.OnTimer)
style = gizmos.LED_ALIGN_CENTER
def OnTimer(self, event):
current = time.localtime(time.time())
self.ts = time.strftime("%H:%M:%S", current)
self.led.SetValue(self.ts)
if self.ts in self.ceck:
self.list_ctrl.SetItemTextColour(self.ceck.get(self.ts), wx.WHITE)
self.list_ctrl.SetItemBackgroundColour(self.ceck.pop(self.ts), wx.RED)
def add_line(self,event):
val = str(self.CTesto.GetValue())
if val== '':
msg = wx.MessageDialog(self, "Error", "Error", wx.OK| wx.ICON_ERROR)
msg.ShowModal()
msg.Destroy()
else:
if self.list_ctrl.GetItemCount() == 10:
for i in range(7):
self.list_ctrl.DeleteItem(1)
dataCorrente = datetime.datetime.now()
oraAttuale =(dataCorrente.strftime("%H:%M:%S"))
plus = (datetime.datetime.strptime(oraAttuale, "%H:%M:%S") + datetime.timedelta(minutes=1))
global plus2
plus2 = plus.strftime("%H:%M:%S")
if plus2 in self.ceck:
msg = wx.MessageDialog(self, "Duplicate Time", "Error", wx.OK| wx.ICON_ERROR)
msg.ShowModal()
msg.Destroy()
return
self.list_ctrl.InsertItem(self.index, val)
self.list_ctrl.SetItem(self.index, 1, oraAttuale)
self.list_ctrl.SetItem(self.index, 2, str(plus2))
self.index += 1
InsVal = (val + " - " + oraAttuale + " - " + plus2 + '\n')
self.CTesto.Clear()
self.ceck[plus2] = self.index -1
def close(self,event):
hDialog=wx.MessageDialog(self, "°°°", "Exit", wx.OK|wx.CANCEL| wx.ICON_EXCLAMATION)
rispostaDialog=hDialog.ShowModal()
hDialog.Destroy()
if rispostaDialog == wx.ID_OK:
self.Destroy()
app = wx.App()
frame = CWindow(None, -1, "Example")
frame.Show()
frame.Center()
app.MainLoop()
您似乎已经(从原来的 post)回到按输入顺序而不是反向输入顺序排列 listctrl 项目。这意味着您必须再次删除索引 0 而不是索引 1。
如果您刚刚删除了 7 个条目,您也不允许这样的事实,即字典值现在无效。如果项目已被删除,则必须删除它们;如果它们已留在原处,则必须重置为新索引值。
试试这个,看看它是否能让你更接近你想要实现的目标:
import wx
import wx.gizmos as gizmos
import time
import datetime
from datetime import timedelta
class CWindow(wx.Frame):
def __init__(self, parent, id, title):
wx.Frame.__init__(self, parent, id, title, style = wx.DEFAULT_FRAME_STYLE & ~ wx.CLOSE_BOX ^ wx.MAXIMIZE_BOX ^ wx.RESIZE_BORDER, size=(600,500))
dataCorrente = datetime.datetime.now()
self.Panel = wx.Panel(self, -1)
self.index = 0
self.ceck = {}
self.ts = ""
self.CTesto = wx.TextCtrl(self.Panel, 1, pos=(10,40), style=wx.TE_PROCESS_ENTER)
self.CTesto.Bind(wx.EVT_TEXT_ENTER,self.add_line)
self.BtnConferma = wx.Button(self.Panel, 12, "Conferma", pos=(130,38))
self.list_ctrl = wx.ListCtrl(self.Panel, pos=(10,90),size=(-1,330),style=wx.LC_REPORT|wx.BORDER_SUNKEN)
self.list_ctrl.InsertColumn(0, 'Name')
self.list_ctrl.InsertColumn(1, 'Start')
self.list_ctrl.InsertColumn(2, 'Finish', width=100)
wx.Button(self.Panel, 22, "Exit", pos=wx.Point(470,400))
self.Bind(wx.EVT_BUTTON, self.close, id=22)
self.Bind(wx.EVT_BUTTON, self.add_line, self.BtnConferma, id=12)
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(self.list_ctrl, 0, wx.ALL|wx.EXPAND, 5)
sizer.Add(self.BtnConferma, 0, wx.ALL|wx.CENTER, 5)
self.led = gizmos.LEDNumberCtrl(self.Panel, -1, pos = (350,25), size = (200,50), style = gizmos.LED_ALIGN_CENTER)
self.led.SetBackgroundColour("#c0c0c0")
self.led.SetForegroundColour("black")
self.OnTimer(None)
self.timer = wx.Timer(self, -1)
self.timer.Start(1000)
self.Bind(wx.EVT_TIMER, self.OnTimer)
style = gizmos.LED_ALIGN_CENTER
def OnTimer(self, event):
current = time.localtime(time.time())
self.ts = time.strftime("%H:%M:%S", current)
self.led.SetValue(self.ts)
if self.ts in self.ceck:
self.list_ctrl.SetItemTextColour(self.ceck.get(self.ts), wx.WHITE)
self.list_ctrl.SetItemBackgroundColour(self.ceck.pop(self.ts), wx.RED)
def add_line(self,event):
val = str(self.CTesto.GetValue())
if val== '':
msg = wx.MessageDialog(self, "Error", "Error", wx.OK| wx.ICON_ERROR)
msg.ShowModal()
msg.Destroy()
else:
if self.list_ctrl.GetItemCount() == 10:
for i in range(7):
d = self.list_ctrl.GetItem(0,2)
del_text = d.GetText()
#remove the dictionary values for the items being deleted
try: # It may already have been popped from the dict if the colour was changed
self.ceck.pop(del_text)
except:
pass
self.list_ctrl.DeleteItem(0)
self.index -= 1
#reset the dictionary vales for the existing ctrl items they have new index values
new_idx = 0
for i in range (3):
item = self.list_ctrl.GetItemText(i,2)
self.ceck[item] = new_idx
new_idx += 1
dataCorrente = datetime.datetime.now()
oraAttuale =(dataCorrente.strftime("%H:%M:%S"))
plus = (datetime.datetime.strptime(oraAttuale, "%H:%M:%S") + datetime.timedelta(minutes=1))
global plus2
plus2 = plus.strftime("%H:%M:%S")
if plus2 in self.ceck:
msg = wx.MessageDialog(self, "Duplicate Time", "Error", wx.OK| wx.ICON_ERROR)
msg.ShowModal()
msg.Destroy()
return
self.list_ctrl.InsertItem(self.index, val)
self.list_ctrl.SetItem(self.index, 1, oraAttuale)
self.list_ctrl.SetItem(self.index, 2, str(plus2))
self.index += 1
InsVal = (val + " - " + oraAttuale + " - " + plus2 + '\n')
self.CTesto.Clear()
self.ceck[plus2] = self.index -1
def close(self,event):
hDialog=wx.MessageDialog(self, "°°°", "Exit", wx.OK|wx.CANCEL| wx.ICON_EXCLAMATION)
rispostaDialog=hDialog.ShowModal()
hDialog.Destroy()
if rispostaDialog == wx.ID_OK:
self.Destroy()
app = wx.App()
frame = CWindow(None, -1, "Example")
frame.Show()
frame.Center()
app.MainLoop()
如何清除 Python 中的列表 ctrl?
例如,当我插入第 10 个项目时,前 8 个项目从 listCtrl 中删除,可以继续在 listCtrl 中输入项目。
我该怎么做?
示例:
我插入:1,2,3,4,5,6,7,8,9,10 在我在 listCtrl 中看到的第 10 个项目之后只有 8,9,10,我可以再次插入项目。
我在示例中添加了一些代码。
import wx
import wx.gizmos as gizmos
import time
import datetime
from datetime import timedelta
class CWindow(wx.Frame):
def __init__(self, parent, id, title):
wx.Frame.__init__(self, parent, id, title, style = wx.DEFAULT_FRAME_STYLE & ~ wx.CLOSE_BOX ^ wx.MAXIMIZE_BOX ^ wx.RESIZE_BORDER, size=(600,500))
dataCorrente = datetime.datetime.now()
self.Panel = wx.Panel(self, -1)
self.index = 0
self.ceck = {}
self.ts = ""
self.CTesto = wx.TextCtrl(self.Panel, 1, pos=(10,40), style=wx.TE_PROCESS_ENTER)
self.CTesto.Bind(wx.EVT_TEXT_ENTER,self.add_line)
self.BtnConferma = wx.Button(self.Panel, 12, "Conferma", pos=(130,38))
self.list_ctrl = wx.ListCtrl(self.Panel, pos=(10,90),size=(-1,330),style=wx.LC_REPORT|wx.BORDER_SUNKEN)
self.list_ctrl.InsertColumn(0, 'Name')
self.list_ctrl.InsertColumn(1, 'Start')
self.list_ctrl.InsertColumn(2, 'Finish', width=100)
wx.Button(self.Panel, 22, "Exit", pos=wx.Point(470,400))
self.Bind(wx.EVT_BUTTON, self.close, id=22)
self.Bind(wx.EVT_BUTTON, self.add_line, self.BtnConferma, id=12)
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(self.list_ctrl, 0, wx.ALL|wx.EXPAND, 5)
sizer.Add(self.BtnConferma, 0, wx.ALL|wx.CENTER, 5)
self.led = gizmos.LEDNumberCtrl(self.Panel, -1, pos = (350,25), size = (200,50), style = gizmos.LED_ALIGN_CENTER)
self.led.SetBackgroundColour("#c0c0c0")
self.led.SetForegroundColour("black")
self.OnTimer(None)
self.timer = wx.Timer(self, -1)
self.timer.Start(1000)
self.Bind(wx.EVT_TIMER, self.OnTimer)
style = gizmos.LED_ALIGN_CENTER
def OnTimer(self, event):
current = time.localtime(time.time())
self.ts = time.strftime("%H:%M:%S", current)
self.led.SetValue(self.ts)
if self.ts in self.ceck:
self.list_ctrl.SetItemTextColour(self.ceck.get(self.ts), wx.WHITE)
self.list_ctrl.SetItemBackgroundColour(self.ceck.pop(self.ts), wx.RED)
def add_line(self,event):
val = str(self.CTesto.GetValue())
if val== '':
msg = wx.MessageDialog(self, "Error", "Error", wx.OK| wx.ICON_ERROR)
msg.ShowModal()
msg.Destroy()
else:
if self.list_ctrl.GetItemCount() == 10:
for i in range(7):
self.list_ctrl.DeleteItem(1)
dataCorrente = datetime.datetime.now()
oraAttuale =(dataCorrente.strftime("%H:%M:%S"))
plus = (datetime.datetime.strptime(oraAttuale, "%H:%M:%S") + datetime.timedelta(minutes=1))
global plus2
plus2 = plus.strftime("%H:%M:%S")
if plus2 in self.ceck:
msg = wx.MessageDialog(self, "Duplicate Time", "Error", wx.OK| wx.ICON_ERROR)
msg.ShowModal()
msg.Destroy()
return
self.list_ctrl.InsertItem(self.index, val)
self.list_ctrl.SetItem(self.index, 1, oraAttuale)
self.list_ctrl.SetItem(self.index, 2, str(plus2))
self.index += 1
InsVal = (val + " - " + oraAttuale + " - " + plus2 + '\n')
self.CTesto.Clear()
self.ceck[plus2] = self.index -1
def close(self,event):
hDialog=wx.MessageDialog(self, "°°°", "Exit", wx.OK|wx.CANCEL| wx.ICON_EXCLAMATION)
rispostaDialog=hDialog.ShowModal()
hDialog.Destroy()
if rispostaDialog == wx.ID_OK:
self.Destroy()
app = wx.App()
frame = CWindow(None, -1, "Example")
frame.Show()
frame.Center()
app.MainLoop()
您似乎已经(从原来的 post)回到按输入顺序而不是反向输入顺序排列 listctrl 项目。这意味着您必须再次删除索引 0 而不是索引 1。
如果您刚刚删除了 7 个条目,您也不允许这样的事实,即字典值现在无效。如果项目已被删除,则必须删除它们;如果它们已留在原处,则必须重置为新索引值。
试试这个,看看它是否能让你更接近你想要实现的目标:
import wx
import wx.gizmos as gizmos
import time
import datetime
from datetime import timedelta
class CWindow(wx.Frame):
def __init__(self, parent, id, title):
wx.Frame.__init__(self, parent, id, title, style = wx.DEFAULT_FRAME_STYLE & ~ wx.CLOSE_BOX ^ wx.MAXIMIZE_BOX ^ wx.RESIZE_BORDER, size=(600,500))
dataCorrente = datetime.datetime.now()
self.Panel = wx.Panel(self, -1)
self.index = 0
self.ceck = {}
self.ts = ""
self.CTesto = wx.TextCtrl(self.Panel, 1, pos=(10,40), style=wx.TE_PROCESS_ENTER)
self.CTesto.Bind(wx.EVT_TEXT_ENTER,self.add_line)
self.BtnConferma = wx.Button(self.Panel, 12, "Conferma", pos=(130,38))
self.list_ctrl = wx.ListCtrl(self.Panel, pos=(10,90),size=(-1,330),style=wx.LC_REPORT|wx.BORDER_SUNKEN)
self.list_ctrl.InsertColumn(0, 'Name')
self.list_ctrl.InsertColumn(1, 'Start')
self.list_ctrl.InsertColumn(2, 'Finish', width=100)
wx.Button(self.Panel, 22, "Exit", pos=wx.Point(470,400))
self.Bind(wx.EVT_BUTTON, self.close, id=22)
self.Bind(wx.EVT_BUTTON, self.add_line, self.BtnConferma, id=12)
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(self.list_ctrl, 0, wx.ALL|wx.EXPAND, 5)
sizer.Add(self.BtnConferma, 0, wx.ALL|wx.CENTER, 5)
self.led = gizmos.LEDNumberCtrl(self.Panel, -1, pos = (350,25), size = (200,50), style = gizmos.LED_ALIGN_CENTER)
self.led.SetBackgroundColour("#c0c0c0")
self.led.SetForegroundColour("black")
self.OnTimer(None)
self.timer = wx.Timer(self, -1)
self.timer.Start(1000)
self.Bind(wx.EVT_TIMER, self.OnTimer)
style = gizmos.LED_ALIGN_CENTER
def OnTimer(self, event):
current = time.localtime(time.time())
self.ts = time.strftime("%H:%M:%S", current)
self.led.SetValue(self.ts)
if self.ts in self.ceck:
self.list_ctrl.SetItemTextColour(self.ceck.get(self.ts), wx.WHITE)
self.list_ctrl.SetItemBackgroundColour(self.ceck.pop(self.ts), wx.RED)
def add_line(self,event):
val = str(self.CTesto.GetValue())
if val== '':
msg = wx.MessageDialog(self, "Error", "Error", wx.OK| wx.ICON_ERROR)
msg.ShowModal()
msg.Destroy()
else:
if self.list_ctrl.GetItemCount() == 10:
for i in range(7):
d = self.list_ctrl.GetItem(0,2)
del_text = d.GetText()
#remove the dictionary values for the items being deleted
try: # It may already have been popped from the dict if the colour was changed
self.ceck.pop(del_text)
except:
pass
self.list_ctrl.DeleteItem(0)
self.index -= 1
#reset the dictionary vales for the existing ctrl items they have new index values
new_idx = 0
for i in range (3):
item = self.list_ctrl.GetItemText(i,2)
self.ceck[item] = new_idx
new_idx += 1
dataCorrente = datetime.datetime.now()
oraAttuale =(dataCorrente.strftime("%H:%M:%S"))
plus = (datetime.datetime.strptime(oraAttuale, "%H:%M:%S") + datetime.timedelta(minutes=1))
global plus2
plus2 = plus.strftime("%H:%M:%S")
if plus2 in self.ceck:
msg = wx.MessageDialog(self, "Duplicate Time", "Error", wx.OK| wx.ICON_ERROR)
msg.ShowModal()
msg.Destroy()
return
self.list_ctrl.InsertItem(self.index, val)
self.list_ctrl.SetItem(self.index, 1, oraAttuale)
self.list_ctrl.SetItem(self.index, 2, str(plus2))
self.index += 1
InsVal = (val + " - " + oraAttuale + " - " + plus2 + '\n')
self.CTesto.Clear()
self.ceck[plus2] = self.index -1
def close(self,event):
hDialog=wx.MessageDialog(self, "°°°", "Exit", wx.OK|wx.CANCEL| wx.ICON_EXCLAMATION)
rispostaDialog=hDialog.ShowModal()
hDialog.Destroy()
if rispostaDialog == wx.ID_OK:
self.Destroy()
app = wx.App()
frame = CWindow(None, -1, "Example")
frame.Show()
frame.Center()
app.MainLoop()