I have 2 widgets, I need to copy all attributes (pos, size, canvas, etc.) from one widget to another somehow (and then move the last to new pos). Probably I can copy attributes one by one, but is there some built-in function? It seems Python's copy makes only shell copy (I can't move duplicate etc.) and deepcopy fails.
问题:
回答1:
One thing you can do is to make use of the copy function in python. This does not copy all the pos/size values but you will have all the attributes.
Ex:
from copy import copy
new_box = copy(self.current_box)
Hope this helps.
回答2:
I'm not aware of a pre-existing way to do this in general, but you could probably fairly easily make a function to do it. You can get a list of the widget properties through the properties()
method of EventDispatcher
, though you'd also need to manually keep track of any non-kivy-property attributes you want to copy, and might need to check to make sure it's safe to copy them all.
Depending on the situation, there may also be other possibilities. For instance, if the widget is instantiated from a set of arguments in the first place and never really modified much, you could just save the argument list and use it to construct a new widget. There might also be more efficient alternatives - if you don't need to interact with the 'copy', you don't need to make a new widget at all, but could draw the original one to a Fbo and simply re-use its texture. This would be a more advanced use of kivy, but isn't that hard, let me know if you're interested in it but don't know how to do it.