wxpython AttributeError:“ListCtrl”对象没有属性“EnableCheckBox”

2024-10-01 09:41:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在运行一个简单的wxpythonlistctrl示例

import wx  

players = [('Tendulkar', '15000', '100'), ('Dravid', '14000', '1'), 
   ('Kumble', '1000', '700'), ('KapilDev', '5000', '400'), 
   ('Ganguly', '8000', '50')] 

class Mywin(wx.Frame): 

   def __init__(self, parent, title): 
      super(Mywin, self).__init__(parent, title = title) 

      panel = wx.Panel(self) 
      box = wx.BoxSizer(wx.HORIZONTAL)

      self.list = wx.ListCtrl(panel, -1, style = wx.LC_REPORT) 
      self.list.InsertColumn(0, 'name', width = 100) 
      self.list.InsertColumn(1, 'runs', wx.LIST_FORMAT_RIGHT, 100) 
      self.list.InsertColumn(2, 'wkts', wx.LIST_FORMAT_RIGHT, 100)

      self.list.EnableCheckBoxes()  # problem line

      for i in players: 
         index = self.list.InsertStringItem(0, i[0]) 
         self.list.SetStringItem(index, 1, i[1]) 
         self.list.SetStringItem(index, 2, i[2]) 

      box.Add(self.list,1,wx.EXPAND) 
      panel.SetSizer(box) 
      panel.Fit() 
      self.Centre() 

      self.Show(True)  

ex = wx.App() 
Mywin(None,'ListCtrl Demo') 
ex.MainLoop()

但是,行self.list.EnableCheckBoxes()给出了错误AttributeError: 'ListCtrl' object has no attribute 'EnableCheckBoxes'。如果我删除这一行,我没有错误

我在这里引用ListCtrl的wxpython文档https://wxpython.org/Phoenix/docs/html/wx.ListCtrl.html?highlight=listctrl#wx.ListCtrl.EnableCheckBoxes,它应该是一个受支持的函数。有人能解释一下为什么我会出现属性错误吗


Tags: selfboxindextitleinit错误listparent
1条回答
网友
1楼 · 发布于 2024-10-01 09:41:27

这是在版本4.1.0中引入的,因此可能是您运行的是旧版本的wxPython

它“启用”复选框,但不check它们,尽管这可以通过CheckItem(index,True)实现

见下文:

import wx

players = [('Tendulkar', '15000', '100'), ('Dravid', '14000', '1'),
   ('Kumble', '1000', '700'), ('KapilDev', '5000', '400'),
   ('Ganguly', '8000', '50')]

class Mywin(wx.Frame):

   def __init__(self, parent, title):
      super(Mywin, self).__init__(parent, title = title)

      panel = wx.Panel(self)
      box = wx.BoxSizer(wx.HORIZONTAL)

      self.list = wx.ListCtrl(panel, -1, style = wx.LC_REPORT)
      self.list.InsertColumn(0, 'name', width = 100)
      self.list.InsertColumn(1, 'runs', wx.LIST_FORMAT_RIGHT, 100)
      self.list.InsertColumn(2, 'wkts', wx.LIST_FORMAT_RIGHT, 100)

      self.list.EnableCheckBoxes(True)  # problem line

      for i in players:
        index = self.list.InsertItem(0, i[0])
        self.list.SetItem(index, 1, i[1])
        self.list.SetItem(index, 2, i[2])
        self.list.CheckItem(index,True)

      box.Add(self.list,1,wx.EXPAND)
      panel.SetSizer(box)
      panel.Fit()
      self.Centre()

      self.Show(True)

ex = wx.App()
Mywin(None,'ListCtrl Demo')
ex.MainLoop()

enter image description here

相关问题 更多 >