Вот что-то, что, кажется, может выполнить то, что вы хотите. Это не полномасштабный пример, а скорее демонстрация одного подхода к достижению вашей цели - сделать копию.
В вашем вопросе нет кода, который на самом деле что-то рисует на Canvas
, поэтому я позаимствовал его из учебника по использованию tkinter
, который я нашел, чтобы получить Canvas
с несколькими виджетами на нем в целях иллюстрации. Он создает окно, похожее на это, которое имеет один Button
и один Canvas
виджет, содержащий только три разноцветных виджета прямоугольника:
![screenshot of program running](https://i.stack.imgur.com/LmshC.png)
Вот демонстрационный код, показывающий, как перебирать все виджеты, в настоящее время Canvas
:
from pprint import pprint, pformat
from tkinter import Button, Tk, Canvas, Frame, BOTH
class Example(Frame):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.master.title("Colours")
self.pack(fill=BOTH, expand=1)
button = Button(self, text="Copy", command=self.copy_canvas)
button.pack()
self.canvas = Canvas(self)
self.canvas.create_rectangle(30, 10, 120, 80,
outline="#fb0", fill="#fb0")
self.canvas.create_rectangle(150, 10, 240, 80,
outline="#f50", fill="#f50")
self.canvas.create_rectangle(270, 10, 370, 80,
outline="#05f", fill="#05f")
self.canvas.pack(fill=BOTH, expand=1)
def copy_canvas(self):
# Iterate through all the items in self.canvas and print each
# one's type and options (which could be used to recreate it).
for id in self.canvas.find_all():
item_type = self.canvas.type(id)
options = self.canvas.itemconfigure(id)
formatted_options = pformat(options, indent=4)
print('id: {}, type: {!r}\n{}'.format(
id, item_type, formatted_options))
def main():
root = Tk()
ex = Example()
root.geometry("400x100+300+300")
root.mainloop()
if __name__ == '__main__':
main()
и вот что он печатает при нажатии кнопки Copy :
id: 1, type: 'rectangle'
{ 'activedash': ('activedash', '', '', '', ''),
'activefill': ('activefill', '', '', '', ''),
'activeoutline': ('activeoutline', '', '', '', ''),
'activeoutlinestipple': ('activeoutlinestipple', '', '', '', ''),
'activestipple': ('activestipple', '', '', '', ''),
'activewidth': ('activewidth', '', '', '0.0', '0.0'),
'dash': ('dash', '', '', '', ''),
'dashoffset': ('dashoffset', '', '', '0', '0'),
'disableddash': ('disableddash', '', '', '', ''),
'disabledfill': ('disabledfill', '', '', '', ''),
'disabledoutline': ('disabledoutline', '', '', '', ''),
'disabledoutlinestipple': ('disabledoutlinestipple', '', '', '', ''),
'disabledstipple': ('disabledstipple', '', '', '', ''),
'disabledwidth': ('disabledwidth', '', '', '0.0', '0'),
'fill': ('fill', '', '', '', '#fb0'),
'offset': ('offset', '', '', '0,0', '0,0'),
'outline': ('outline', '', '', 'black', '#fb0'),
'outlineoffset': ('outlineoffset', '', '', '0,0', '0,0'),
'outlinestipple': ('outlinestipple', '', '', '', ''),
'state': ('state', '', '', '', ''),
'stipple': ('stipple', '', '', '', ''),
'tags': ('tags', '', '', '', ''),
'width': ('width', '', '', '1.0', '1.0')}
id: 2, type: 'rectangle'
{ 'activedash': ('activedash', '', '', '', ''),
'activefill': ('activefill', '', '', '', ''),
'activeoutline': ('activeoutline', '', '', '', ''),
'activeoutlinestipple': ('activeoutlinestipple', '', '', '', ''),
'activestipple': ('activestipple', '', '', '', ''),
'activewidth': ('activewidth', '', '', '0.0', '0.0'),
'dash': ('dash', '', '', '', ''),
'dashoffset': ('dashoffset', '', '', '0', '0'),
'disableddash': ('disableddash', '', '', '', ''),
'disabledfill': ('disabledfill', '', '', '', ''),
'disabledoutline': ('disabledoutline', '', '', '', ''),
'disabledoutlinestipple': ('disabledoutlinestipple', '', '', '', ''),
'disabledstipple': ('disabledstipple', '', '', '', ''),
'disabledwidth': ('disabledwidth', '', '', '0.0', '0'),
'fill': ('fill', '', '', '', '#f50'),
'offset': ('offset', '', '', '0,0', '0,0'),
'outline': ('outline', '', '', 'black', '#f50'),
'outlineoffset': ('outlineoffset', '', '', '0,0', '0,0'),
'outlinestipple': ('outlinestipple', '', '', '', ''),
'state': ('state', '', '', '', ''),
'stipple': ('stipple', '', '', '', ''),
'tags': ('tags', '', '', '', ''),
'width': ('width', '', '', '1.0', '1.0')}
id: 3, type: 'rectangle'
{ 'activedash': ('activedash', '', '', '', ''),
'activefill': ('activefill', '', '', '', ''),
'activeoutline': ('activeoutline', '', '', '', ''),
'activeoutlinestipple': ('activeoutlinestipple', '', '', '', ''),
'activestipple': ('activestipple', '', '', '', ''),
'activewidth': ('activewidth', '', '', '0.0', '0.0'),
'dash': ('dash', '', '', '', ''),
'dashoffset': ('dashoffset', '', '', '0', '0'),
'disableddash': ('disableddash', '', '', '', ''),
'disabledfill': ('disabledfill', '', '', '', ''),
'disabledoutline': ('disabledoutline', '', '', '', ''),
'disabledoutlinestipple': ('disabledoutlinestipple', '', '', '', ''),
'disabledstipple': ('disabledstipple', '', '', '', ''),
'disabledwidth': ('disabledwidth', '', '', '0.0', '0'),
'fill': ('fill', '', '', '', '#05f'),
'offset': ('offset', '', '', '0,0', '0,0'),
'outline': ('outline', '', '', 'black', '#05f'),
'outlineoffset': ('outlineoffset', '', '', '0,0', '0,0'),
'outlinestipple': ('outlinestipple', '', '', '', ''),
'state': ('state', '', '', '', ''),
'stipple': ('stipple', '', '', '', ''),
'tags': ('tags', '', '', '', ''),
'width': ('width', '', '', '1.0', '1.0')}