【Python】int() argument must be a string, a bytes-like object or a number, not ‘KeyboardModifier‘

illustrate

According to Python official website , Python3.7 also supports WIN7.
The requirements.txt corresponding to the project is:

cycler==0.11.0
fonttools==4.38.0
kiwisolver==1.4.4
matplotlib==3.5.3
Nuitka==1.5.7
numpy==1.21.6
ordered-set==4.1.0
packaging==23.1
Pillow==9.5.0
pyparsing==3.0.9
PySide2==5.15.2.1
python-dateutil==2.8.2
shiboken2==5.15.2.1
six==1.16.0
typing-extensions==4.5.0
zstandard==0.21.0

The nuitka command corresponding to the project is:

nuitka --standalone  --output-dir=out  --plugin-enable=pyside2  --nofollow-import-to=tkinter  --windows-disable-console

solution

from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.backends.backend_qt5agg import NavigationToolbar2QT as NavigationToolbar

The error is mainly the interface compatibility between PySide6 and matplotlib. error location code

_MODIFIER_KEYS = [
    (_to_int(getattr(_enum("QtCore.Qt.KeyboardModifier"), mod)),
     _to_int(getattr(_enum("QtCore.Qt.Key"), key)))
    for mod, key in [
        ("ControlModifier", "Key_Control"),
        ("AltModifier", "Key_Alt"),
        ("ShiftModifier", "Key_Shift"),
        ("MetaModifier", "Key_Meta"),
    ]
]

According to gpt,

这段代码是一个用于将 Qt 的键盘修饰符和按键转换为整数类型的列表。具体来说,代码中定义了一个名为 _MODIFIER_KEYS 的列表,其中包含了 4 个二元组,每个二元组分别表示一个修饰符和对应的按键,且每个元素都是整数类型。
这段代码通过调用 getattr() 方法获取了 PySide6.QtCore.Qt.KeyboardModifier 和 PySide6.QtCore.Qt.Key 枚举类对应的属性,使用 _to_int() 函数将枚举值转换为整数类型,并最终构造成二元组的形式存放在 _MODIFIER_KEYS 列表中。
这个列表可以用于接收 Qt 的快捷键事件后进行处理,比如判断按下的键是否为某个快捷键等。
需要注意的是,这里使用了 _enum() 函数获取枚举类,但代码中未给出该函数的实现,可能是在其他地方定义的函数,因此如果要运行这段代码,还需要先确保 _enum() 函数已经定义并导入。

According to matplotlib official issues , the solution is,

upgrade mpl to >=3.6.2 or downgrade pyside to <6.4.0

matplotlib under Python3.7 supports up to 3.5, so use pyside==6.3.0. At this time, you need to pay attention that NavigationToolbar2QT needs canvas and parent two parameters.

class NavigationToolbar2QT(NavigationToolbar2, QtWidgets.QToolBar):
.......
    def __init__(self, canvas, parent, coordinates=True):
.......

Guess you like

Origin blog.csdn.net/qq_25262697/article/details/130854528