擅长:python、mysql、java
<p>默认情况下,如果没有选择,<code>currentItem()</code>是树中的第一项。在</p>
<p>单击按钮时,<code>self.treeWidget.clear()</code>被调用。所有的项都从tree小部件中删除,因此不再有第一项:发出信号<code>currentItemChanged</code>。在</p>
<p>如果树中没有任何内容,<code>self.treeWidget.currentItem()</code>将返回<code>None</code>。在<code>current_selection</code>,<code>item</code>是{<cd5>},这就是为什么你得到:</p>
<blockquote>
<p>AttributeError: 'NoneType' object has no attribute 'text'</p>
</blockquote>
<p>要解决此问题,可以在继续操作之前测试<code>item</code>是否为{<cd5>}:</p>
<pre><code>def current_selection(self):
item = self.treeWidget.currentItem()
if item is not None:
self.no = item.text(0)
self.colour = item.text(1)
self.animal = item.text(2)
...
</code></pre>