1

Pibrella のボタンが押されると、10 秒ごとに電子メールを送信するスクリプトを作成しています。ボタンをもう一度押すと停止するようにします。

たとえば、初めて押すと、10 秒ごとに電子メールが送信されます。次に、2 回目にスレッドを停止します。

これが私の現在のコードです。どこが間違っているのか教えてもらえますか?

import pibrella, signal
import threading
import time
import smtplib

server = smtplib.SMTP("smtp.gmail.com",587)
server.ehlo()
server.starttls()
server.ehlo()

server.login("email@gmail.com", "EmailPassword")

isPressed = 0

def pressed(pin):
    global isPressed
    if isPressed == 0:
        isPressed = 1
        sendMail()
    else:
        pibrella.light.off()
        t.cancel()
        isPressed = 0

def sendMail():
    pibrella.light.pulse()
    server.sendmail("email@gmail.com", "receiver@gmail.com", "Hello World")
    t.start()

t = threading.Timer(10, sendMail).start()

pibrella.button.pressed(pressed)
pibrella.pause()

server.close()

私が今得ているエラーは以下に掲載されています。メールが実際に送信されていることに注意してください。

Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/pibrella.py", line 262, in handle_callback
    callback(self)
  File "sendText.py", line 27, in pressed
    sendMail()
  File "sendText.py", line 36, in sendMail
    t.start()
AttributeError: 'NoneType' object has no attribute 'start'
Exception in thread Thread-14:
Traceback (most recent call last):
  File "/usr/lib/python2.7/threading.py", line 552, in __bootstrap_inner
    self.run()
  File "/usr/lib/python2.7/threading.py", line 760, in run
    self.function(*self.args, **self.kwargs)
  File "sendText.py", line 36, in sendMail
    t.start()
AttributeError: 'NoneType' object has no attribute 'start'
4

1 に答える 1