Passage d'arguments avec partial
Bonjour,
Je m'interroge sur la possibilité d'utiliser Partial() afin de passer des arguments nommés dans widget.after(). (Veuillez noter que ma question ne porte que sur l'utilisation de Partial() ...)
Sa mise en oeuvre avec tk.Button ne pose aucun problème:
Code:
1 2 3 4 5 6 7 8 9
| from functools import partial
import tkinter as tk
root = tk.Tk()
def action(arg1, arg2=None):
print(f"arg1 = {arg1}, arg2 = {arg2}")
b = tk.Button(root, text="ok", command= partial(action,arg2=100, arg1=200))
b.grid()
root.mainloop() |
Par contre, utilisé avec la méthode after comme ci dessous:
Code:
1 2 3 4 5 6 7 8
| from functools import partial
import tkinter as tk
root = tk.Tk()
def action(arg1, arg2=None):
print(f"arg1 = {arg1}, arg2 = {arg2}")
root.after(1000,partial(action,arg2=100, arg1=200))
root.mainloop() |
Le code renvoit l'exception :
Code:
1 2 3 4 5 6 7
| Traceback (most recent call last):
File "/home/fab/bidon1.py", line 10, in <module>
root.after(1000,partial(action,arg2=100, arg1=200))
File "/usr/lib/python3.6/tkinter/__init__.py", line 755, in after
callit.__name__ = func.__name__
AttributeError: 'functools.partial' object has no attribute '__name__'
>>> |
Qu'en est il?