开发者

timer objects (python) with arguments

开发者 https://www.devze.com 2023-02-26 19:59 出处:网络
How do you pass arguments between defs within a class using the timer object? def onrepeat(self,user, arg):

How do you pass arguments between defs within a class using the timer object?

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)

This will give me an error saying:

repeat2 needs 3 arguments while only 2 are given.

To fix this I change

self.timerobj = Timer(3.0, self.repeat2) 

to

self.timerobj = Timer(3.0, self.repeat2(messagearg,color))

but that doesn't work either!


Timer has the following signature

class threading.Timer(interval, function, args=[], kwargs={})

Note that it accepts args and kwargs. So you send your args to Timer function by just mentioning them. Try this:

Timer(3.0, self.repeat2,messagearg,color)
0

精彩评论

暂无评论...
验证码 换一张
取 消