python-3.xtkinterpublish-subscribepypubsub

Python 3 -- using kwargs with an args only module


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.


Solution

  • 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()