+ 11

[solved] Python Tkinter, how to copy attribute from one widget to another one???

I want that the text of the widget "l1" will be copied to widget "l2" when the button is clicked. Could anybody be so kind and correct the code and point me to the error. The interpreter says "AttributeError: 'NoneType' object has no attribute 'config'" but I have no clue what this means in this context. This is my code. It does not work. import tkinter as tk root = tk.Tk() def duplicate(l1,l2): l2.config(text = str(l1.cget('text'))) l1 = tk.Label(root, text="abracadabra").grid() l2 = tk.Label(root, text="---").grid() b3 = tk.Button(root, text="click me", command = lambda : duplicate(l1,l2)).grid() root.mainloop()

7th Feb 2021, 9:12 AM
Jan Markus
3 Respostas
+ 5
I think that is because when using .grid() while still being assigned to a variable, it will return 'None'. So instead of placing the .grid() in the value of l1 and l2, try to use .grid() later after the assignment of the variable. l1 = tk.Label(root, text="abracadabra") l1.grid() l2 = tk.Label(root, text="---") l2.grid() https://code.sololearn.com/ca19A8A20a2A/?ref=app
7th Feb 2021, 9:21 AM
noteve
noteve - avatar
+ 3
As already told by Cyan , grid method will return None, and in your code you are trying to get an attribute for a None type. You can also use l2.config(text=l1['text'])
7th Feb 2021, 10:12 AM
AKSHAY🇮🇳
AKSHAY🇮🇳 - avatar
+ 1
I like to set the text variable attr in attributes to a StringVar(). Then use set() and get() methods to update them
7th Feb 2021, 11:06 AM
Slick
Slick - avatar