1

I am writing a gui in tkinter and using a publish/subscribe module (pyPubSub) to inform different parts of the program of what is occurring if they're subscribed. So, I have two functions that I need to work together. From tkinter, I'm using:

after_idle(callback, *args)

to call the message sending within the mainloop. As you can see, it only accepts *args for the arguments to send to the callback. The callback I'm sending is from pyPubSub:

sendMessage(topic, **kwargs)

So, I end up with this:

root.after_idle(pub.sendMessage, ?)

My question is, how do I make args work with kwargs? I have to call after_idle with positional arguments to send with the callback, but the callback requires keyword arguments only.

Oliver
  • 27,510
  • 9
  • 72
  • 103
linus72982
  • 1,418
  • 2
  • 16
  • 31

1 Answers1

4

You could always use lambda, here's a short example that does nothing.

import tkinter as tk

def test(arg1, arg2):
    print(arg1, arg2)

root = tk.Tk()
root.after_idle(lambda: test(arg1=1, arg2=2))
root.mainloop()
Pythonista
  • 11,377
  • 2
  • 31
  • 50
  • I guess I'm not sure how that would fix it. For example, I have gui_obj.after_idle(pub.sendMessage, *package) as it requires and package looks like this: [, ], for instance (sometimes there are more than one arg, sometimes not, it doesn't work either way). When after_idle registers the callback, it calls sendMessage(, ) and I can't figure out how to get it to send it as sendMessage(, msg=) as sendMessage requires. I keep getting: TypeError: sendMessage() takes 2 positional arguments but 3 were given – linus72982 Aug 02 '16 at 03:07
  • Never mind, I figured it out. I need to use lambda as the callback function that will then call sendMessage correctly. Thanks! – linus72982 Aug 02 '16 at 03:14
  • The "takes 2 positional arguments" exception was caused by after_idle not sending it correctly. sendMessage wanted 2 positional args (self and topic) and then kwargs. after_idle was sending topic and the optional arguments both as positional so it was getting 3 positionals (1 of which is self) when it only wanted 2 (technically just one from me and 1 from itself) – linus72982 Aug 02 '16 at 03:17