参数1具有意外的类型’float’,适用于Python版本> 3.10

huangapple go评论74阅读模式
英文:

argument 1 has unexpected type 'float' with python version > 3.10

问题

自从Python 3.10版本以后,当使用asynqt asyncio.sleep函数时,出现了"argument 1 has unexpected type 'float'"错误,而在Python 3.9版本中正常工作。

复现该错误的代码如下,并且在**await asyncio.sleep(0.1)**这一行失败。这个问题在使用整数值时是正常的。在之前的Python版本中也是正常的:

import functools
import sys, traceback
import asyncio, asyncqt
from PyQt5 import QtWidgets, uic
from PyQt5.QtWidgets import QMainWindow

# 未知异常处理程序
def handle_excepthook(type, message, stack):
    print(f'An unhandled exception occurred: {message}. Traceback: {traceback.format_tb(stack)}')

class MainUi(QMainWindow):
    def __init__(self):
        pass

async def main():
    app = QtWidgets.QApplication(sys.argv)
    app.setQuitOnLastWindowClosed(True)

    loop = asyncqt.QEventLoop(app)
    asyncio.set_event_loop(loop)

    await asyncio.sleep(0.1)  # 使用浮点数值会失败

    ui = MainUi()
    ui.show()

    with loop:
        # 在UI关闭之前运行事件循环
        await asyncio.gather(loop.create_task(loop.run_forever()), loop.run_in_executor(None, functools.partial(ui.show)))

    # 清理和关闭应用程序
    loop.close()
    app.exec_()

if __name__ == '__main__':
    try:
        asyncio.run(main())
    except Exception as e:
        print("An error occurred:", e)
        traceback.print_exc()
        sys.exit(1)

这会产生以下错误:

File "C:\PLDesign\AntennaNoiseTracking\venv\Lib\site-packages\asyncqt\__init__.py", line 202, in add_callback
    timerid = self.startTimer(delay * 1000)
TypeError: startTimer(self, interval: int, timerType: Qt.TimerType = Qt.CoarseTimer): argument 1 has unexpected type 'float'

有什么想法吗
谢谢
Stéphane
英文:

Since Python 3.10 I've a trouble with argument 1 has unexpected type 'float' error when using asynqt asyncio.sleep functions whereas it was working well with Python 3.9

the code to reproduce is below and fails on the line await asyncio.sleep(0.1). This works well with interger values. Was working with previous version of Python :

import functools
import sys, traceback
import asyncio, asyncqt
from PyQt5 import QtWidgets, uic
from PyQt5.QtWidgets import QMainWindow

# unknown exception handler
def handle_excepthook(type, message, stack):
    print(f'An unhandled exception occured: {message}. Traceback: {traceback.format_tb(stack)}')

class MainUi(QMainWindow):
    def __init__(self):
        pass

async def main():
    app = QtWidgets.QApplication(sys.argv)
    app.setQuitOnLastWindowClosed(True)

    loop = asyncqt.QEventLoop(app)
    asyncio.set_event_loop(loop)

    await asyncio.sleep(0.1) # fails with float values

    ui = MainUi()
    ui.show()

    with loop:
        # Run the event loop until the UI is closed
        await asyncio.gather(loop.create_task(loop.run_forever()), loop.run_in_executor(None, functools.partial(ui.show)))

    # Cleanup and close the application
    loop.close()
    app.exec_()


if __name__ == '__main__':
    try:
        asyncio.run(main())
    except Exception as e:
        print("An error occurred:", e)
        traceback.print_exc()
        sys.exit(1)

this produces the error :

  File "C:\PLDesign\AntennaNoiseTracking\venv\Lib\site-packages\asyncqt\__init__.py", line 202, in add_callback
    timerid = self.startTimer(delay * 1000)
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: startTimer(self, interval: int, timerType: Qt.TimerType = Qt.CoarseTimer): argument 1 has unexpected type 'float'

Any idea ?
Thanks
Stéphane

答案1

得分: 0

在尝试联系 asynqt 的所有者时,我发现它已经三年没有维护了。

你可以使用 QAsync,它是维护的,然后在创建事件循环时将 asynqt 替换为 qasync:

def main():
    app = QtWidgets.QApplication(sys.argv)
    loop = qasync.QEventLoop(app)
    asyncio.set_event_loop(loop)

从浮点数到整数的即时强制类型转换显然已在 qasync 中得到修正,现在一切都运行得很完美。

英文:

While trying to contact the owner of asynqt I've figured out that it's not maintained anymore since three years.

You can use QAsync instead which is maintained and then replace asynqt by qasync while creating the event loop :

def main():
    app = QtWidgets.QApplication(sys.argv)
    loop = qasync.QEventLoop(app)
    asyncio.set_event_loop(loop)

The on-the-fly casting from float to int is apparently corrected in qasync and the stuff runs perfectly now.

huangapple
  • 本文由 发表于 2023年7月13日 22:37:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/76680598.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定