0

我正在用 rp2040、RTC 和 NeoPixel 条构建一个基本的日光闹钟。我正在为警报对象构建类,其中包含:触发时间(小时、分钟、秒)、触发函数和被触发函数的参数,这是相关的代码片段:

    class Alarm(Clock):
    def __init__(self, time, trigger, *args):
        self.absolute_alarm = super().calculate_absolute_time(time)
        self.trigger = trigger
        self.args = args
    def trigger_alarm(self):
        self.trigger(*self.args)

在主 code.py这是抛出错误的第 61 行

wake_up_alarm = Alarm((7,0,0), light_bar_soft_fade, 1, pixels_60)

绝对时间只是自午夜以来的秒数,比试图在 12 小时时间中调整小时/分钟更容易。Clock 是一个 NeoPixel 继承的类,它只包含一些发布功能,这一切都按预期工作。当我运行此代码时,我收到此错误:

code.py output:
Traceback (most recent call last):
  File "code.py", line 61, in <module>
TypeError: can't convert tuple to int

我已将其范围缩小到抛出元组对象,但我看不出 Python 认为它需要成为 int 的位置?我该如何纠正?

完整的类代码不会太长,以防万一:

import time
import neopixel
class Clock(neopixel.NeoPixel):
    def __init__(self, pin, n, *, bpp=3, brightness=1.0, auto_write=True, pixel_order=None, debugging=False):
        super().__init__(
            pin, n, bpp=bpp, brightness=brightness, auto_write=auto_write, pixel_order=pixel_order
        )
        if len(self.byteorder) == 3: self.fill((0,0,0))
        if len(self.byteorder) == 4: self.fill((0,0,0,0))
        self.last_minute = self.last_hour = self.last_second = 0
        self.clock_face = self.alarms = []
        self.show()
    def post(self, t):
        self.clock_face = []
        for each in range(0, 12): self.clock_face.append([0, 0, 0, 0])
        self.clock_face[t.tm_hour % 12][0] = 1
        self.clock_face[int(t.tm_min/5)][2] = 1
        for each in range(0, 12): self[each] = self.clock_face[each]
        self.show()
        for alarm in self.alarms:
            if calculate_absolute_time(t) == alarm.absolute_alarm: alarm.trigger()
    def calculate_absolute_time(self, time):
        try:
            absolute_time = (time.tm_hour * 60 + time.tm_min) * 60 + time.tm_sec
        except AttributeError:
            absolute_time = (time[0] * 60 + time[1]) * 60 + time[2]
        return absolute_time

最少的可重现代码

import neopixel
import board
class Clock(neopixel.NeoPixel):
    def __init__(
        self, pin, n, *, bpp=3, brightness=1.0, auto_write=True, pixel_order=None, debugging=False,):
        super().__init__(pin, n, bpp=bpp, brightness=brightness, auto_write=auto_write, pixel_order=pixel_order,)
    def calculate_absolute_time(self, time):
        return True

class Alarm(Clock):
    def __init__(self, time, trigger, *args):
        pass
def light_bar_soft_fade(*args):
    pass

clock_pixels = Clock(board.D10, 12, brightness=1, auto_write=False,
                           pixel_order=(1, 0, 2, 3))
wake_up_alarm = Alarm((7,0,0), light_bar_soft_fade, 1, clock_pixels)

新的错误信息:

Traceback (most recent call last):
  File "code.py", line 18, in <module>
TypeError: can't convert tuple to int
4

1 回答 1

0

我猜你正在将一个元组像一个参数传递给calculate_absolute_time(time),但是你的方法calculate_absolute_time 需要接收一个int。

我想您应该在方法等中设置每个参数的类型。例如:

类时钟:def init(自我):...等...

def calculate_absolute_time(self, time:tuple[int]) -> float:
    ... code ... 
    return float() 

有了这样的东西,您将能够知道哪里出了问题以及错误在哪里。

有关更多信息,我需要获取有关您的代码的更多详细信息。我希望我对你有所帮助。好锁。

于 2021-10-15T17:10:28.770 回答