小编Ray*_*orn的帖子

在 PyQT 中显示 QTreeView 项目的工具提示

我遵循 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)

pyqt tooltip

5
推荐指数
2
解决办法
5173
查看次数

How to fix a Mac base conda environment when sqlite3 is broken

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)

python sqlite conda

5
推荐指数
1
解决办法
2108
查看次数

如何在不引发UnicodeEncodeError的情况下覆盖str函数?

我很困惑,__str__为类定义似乎对str在类实例上使用该函数没有任何影响.例如,我在Django文档中读到:

该print语句和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)

python unicode python-2.x

4
推荐指数
1
解决办法
837
查看次数

标签 统计

python ×2

conda ×1

pyqt ×1

python-2.x ×1

sqlite ×1

tooltip ×1

unicode ×1