gpt4 book ai didi

python - wxpython的列表控件中的删除

转载 作者:太空宇宙 更新时间:2023-11-03 19:06:59 25 4
gpt4 key购买 nike

我的代码生成以下错误 -“无法检索有关列表控件项 3 的信息”。

import wx

DATA = [("0", "Zero"), ("1", "One"), ("2", "Two")]
class MainWindow(wx.Frame):
def __init__(self, *args, **kwargs):
wx.Frame.__init__(self, *args, **kwargs)

self.panel = wx.Panel(self)

self.list = wx.ListCtrl(self.panel, style=wx.LC_REPORT)
self.list.InsertColumn(0, "Index")
self.list.InsertColumn(1, "Number")
for data in DATA:
self.list.Append((data[0], data[1]))

self.button = wx.Button(self.panel, label="Delete")
self.button.Bind(wx.EVT_BUTTON, self.OnButton)

self.sizer = wx.BoxSizer(wx.VERTICAL)
self.sizer.Add(self.list, 1, wx.ALL | wx.EXPAND, 5)
self.sizer.Add(self.button, 0, wx.ALL | wx.EXPAND, 5)
self.panel.SetSizerAndFit(self.sizer)

self.Show()

def OnButton(self, e):
current_items = self.list.GetItemCount()
while ((current_items) >= 0) :
if (self.list.GetItemText(current_items) == "1" or self.list.GetItemText(current_items-1) == "2"):
self.list.DeleteItem(current_items)
wx.MessageBox("Delete item ", 'Delete Information',wx.OK)
else:
break
current_items-=1

if __name__ == "__main__":
app = wx.App(False)
win = MainWindow(None)
win.Centre()
app.MainLoop()

谁能告诉我代码有什么问题吗?我应该怎么做才能解决这个错误?提前致谢。

最佳答案

也许你想要这样的东西?

import wx

DATA = [("0", "Zero"), ("1", "One"), ("2", "Two")]
class MainWindow(wx.Frame):
def __init__(self, *args, **kwargs):
wx.Frame.__init__(self, *args, **kwargs)

self.panel = wx.Panel(self)

self.list = wx.ListCtrl(self.panel, style=wx.LC_REPORT)
self.list.InsertColumn(0, "Index")
self.list.InsertColumn(1, "Number")
for data in DATA:
self.list.Append((data[0], data[1]))

self.button = wx.Button(self.panel, label="Delete")
self.button.Bind(wx.EVT_BUTTON, self.OnButton)

self.sizer = wx.BoxSizer(wx.VERTICAL)
self.sizer.Add(self.list, 1, wx.ALL | wx.EXPAND, 5)
self.sizer.Add(self.button, 0, wx.ALL | wx.EXPAND, 5)
self.panel.SetSizerAndFit(self.sizer)

self.Show()

def OnButton(self, e):
current_items = self.list.GetItemCount() - 1
while ((current_items) >= 0) :
if (self.list.GetItemText(current_items) == "1" or self.list.GetItemText(current_items) == "2"):
self.list.DeleteItem(current_items)
wx.MessageBox("Delete item ", 'Delete Information',wx.OK)
else:
break
current_items-=1

if __name__ == "__main__":
app = wx.App(False)
win = MainWindow(None)
win.Centre()
app.MainLoop()

关于python - wxpython的列表控件中的删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14472849/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com