如何使用计时器对象在def
内的class
之间传递参数?
def onrepeat(self,user, arg):
args = arg.split()
messagearg = args[0]
colorarg = args[1]
if colorarg == "black":
color = "&0"
if colorarg == "dark blue" or "darkblue":
color = "&1"
if colorarg == "dark green" or "darkgreen":
color = "&2"
if colorarg == "dark teal" or "darkteal":
color = "&3"
if colorarg == "dark red" or "darkred":
color = "&4"
if colorarg == "purple":
color = "&5"
if colorarg == "gold":
color = "&6"
if colorarg == "gray":
color = "&7"
if colorarg == "dark gray" or "darkgrey" or "dark grey" or "darkgrey":
color = "&8"
if colorarg == "blue":
color = "&9"
if colorarg == "bright green" or "brightgreen":
color = "&a"
if colorarg == "teal":
color = "&b"
if colorarg == "red":
color = "&c"
if colorarg == "pink":
color = "&d"
if colorarg == "yellow":
color = "&e"
if colorarg == "white":
color = "&f"
if colorarg == "":
color = "&f"
self.timerobj = Timer(3.0, self.repeat2)
self.timerobj.start()
def repeat2(self,messagearg,color):
self.bot.sendMessage(color + messagearg)
这会给我一个错误说:
repeat2 needs 3 arguments while only 2 are given.
为了解决这个问题,我改变了
self.timerobj = Timer(3.0, self.repeat2)
到
self.timerobj = Timer(3.0, self.repeat2(messagearg,color))
但这也不起作用!
答案 0 :(得分:-1)
计时器具有以下签名
class threading.Timer(interval, function, args=[], kwargs={})
请注意,它接受args
和kwargs
。所以你只需要提到它们就可以将你的args发送到Timer函数。试试这个:
Timer(3.0, self.repeat2,messagearg,color)