我遵循 Yasin Uludag 的一些有用的在线教程来尝试使用 PyQt(或更确切地说 PySide)来创建一个简单的树视图,但我在使用工具提示时遇到问题。在以下代码中,工具提示文本显示在控制台上而不是工具提示窗口中。我见过的所有其他示例都直接在小部件项上使用 setToolTip,但我认为我无法在这种模型/视图方法中直接访问它。我需要对 QTreeView 本身进行一些初始化吗?
class TreeModel(QtCore.QAbstractItemModel):
def __init__(self, root, parent=None):
super(NXTreeModel, self).__init__(parent)
self._rootNode = root
def data(self, index, role):
node = index.internalPointer()
if role == QtCore.Qt.DisplayRole or role == QtCore.Qt.EditRole:
return node.name()
if role == QtCore.Qt.ToolTipRole:
return node.keys()
Run Code Online (Sandbox Code Playgroud) I recently updated the Python version of my base conda environment from 3.8 to 3.9, using mamba update python=3.9, but I can no longer run IPython, because the sqlite3 package appears to be broken.
python
Python 3.9.15 | packaged by conda-forge | (main, Nov 22 2022, 08:55:37)
[Clang 14.0.6 ] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import sqlite3
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/rosborn/opt/miniconda3/lib/python3.9/sqlite3/__init__.py", line 57, …Run Code Online (Sandbox Code Playgroud) 我很困惑,__str__为类定义似乎对str在类实例上使用该函数没有任何影响.例如,我在Django文档中读到:
该
str内置的通话__str__(),以确定对象的人类可读表示.
但这似乎不是真的.以下是模块中的示例,其中text始终假定为unicode:
import six
class Test(object):
def __init__(self, text):
self._text = text
def __str__(self):
if six.PY3:
return str(self._text)
else:
return unicode(self._text)
def __unicode__(self):
if six.PY3:
return str(self._text)
else:
return unicode(self._text)
Run Code Online (Sandbox Code Playgroud)
在Python 2中,它提供以下行为:
>>> a=Test(u'café')
>>> print a.__str__()
café
>>> print a # same error with str(a)
---------------------------------------------------------------------------
UnicodeEncodeError Traceback (most recent call last)
<ipython-input-63-202e444820fd> in <module>()
----> 1 str(a)
UnicodeEncodeError: 'ascii' codec can't encode character u'\xe9' in …Run Code Online (Sandbox Code Playgroud)