wxpython AttributeError:'ListCtrl'对象没有属性'EnableCheckBoxes'

问题描述 投票:0回答:1

我正在运行一个简单的wxpython ListCtrl示例。

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'。如果删除此行,则没有错误。

我在https://wxpython.org/Phoenix/docs/html/wx.ListCtrl.html?highlight=listctrl#wx.ListCtrl.EnableCheckBoxes处引用ListCtrl的wxpython文档,它应该是受支持的函数。有人可以解释为什么我遇到属性错误吗?

python wxpython
1个回答
0
投票

这是在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

© www.soinside.com 2019 - 2024. All rights reserved.