python - 如何复制嵌入在 tkinter Canvas 中的 turtle 框架的内容

标签 python python-3.x tkinter turtle-graphics deep-copy

我正在尝试保存 tkinter Canvas 的当前状态以便稍后显示。这是我尝试使用 deepcopy 的方法:

from tkinter import *
from copy import deepcopy
root=Tk()
cv=Canvas(root)
cv.pack()
cvcopy=deepcopy(cv)
mainloop()

然而,行 cvcopy=deepcopy(cv) 产生了错误:

Traceback (most recent call last):
  File "C:/Users/Fred/Desktop/painting/mcve.py", line 6, in <module>
    cvcopy=deepcopy(cv)
  File "C:\python files\lib\copy.py", line 173, in deepcopy
    y = _reconstruct(x, rv, 1, memo)
  File "C:\python files\lib\copy.py", line 295, in _reconstruct
    state = deepcopy(state, memo)
  File "C:\python files\lib\copy.py", line 146, in deepcopy
    y = copier(x, memo)
  File "C:\python files\lib\copy.py", line 235, in _deepcopy_dict
    y[deepcopy(key, memo)] = deepcopy(value, memo)
  File "C:\python files\lib\copy.py", line 173, in deepcopy
    y = _reconstruct(x, rv, 1, memo)
  File "C:\python files\lib\copy.py", line 295, in _reconstruct
    state = deepcopy(state, memo)
  File "C:\python files\lib\copy.py", line 146, in deepcopy
    y = copier(x, memo)
  File "C:\python files\lib\copy.py", line 235, in _deepcopy_dict
    y[deepcopy(key, memo)] = deepcopy(value, memo)
  File "C:\python files\lib\copy.py", line 173, in deepcopy
    y = _reconstruct(x, rv, 1, memo)
  File "C:\python files\lib\copy.py", line 280, in _reconstruct
    y = callable(*args)
  File "C:\python files\lib\copyreg.py", line 88, in __newobj__
    return cls.__new__(cls, *args)
TypeError: object.__new__(tkapp) is not safe, use tkapp.__new__()

因为显然你不能像那样创建一个新的小部件。下面是我用来在 turtle 屏幕上嵌入和绘制的代码:

cv = tkinter.Canvas(self.root,width=300,height=300)
cv.pack()
t = turtle.RawTurtle(cv)
s = t.getscreen()
def toggledown():
    if t.isdown():
        t.penup()
    else:
        t.pendown()
t.speed(0)
t.ondrag(t.goto)
s.onclick(t.goto)
s.onkey(toggledown, 'space')
s.listen()

有谁知道如何复制屏幕,然后再添加到其他地方吗?

最佳答案

这似乎可以满足您的需求。这不是一个完整的示例,而只是演示了一种实现制作副本目标的方法。

您的问题没有任何实际在 Canvas 上绘制内容的代码,因此我从 tutorial 中借用了一些代码关于使用 tkinter 我发现它是为了让 Canvas 上有一些小部件用于说明目的。它生成一个看起来像这样的窗口,它有一个 Button 和一个 Canvas 小部件,其中仅包含三个不同颜色的矩形小部件:

screenshot of program running

下面的演示代码展示了如何遍历当前在 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()

这是按下复制按钮时打印的内容:

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')}

关于python - 如何复制嵌入在 tkinter Canvas 中的 turtle 框架的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50078142/

相关文章:

python - Pandas 匹配列表中的元素

python - 如何访问应用程序 'File Description' ?

python - 找不到满足 SoundRecognition 要求的版本

python - 使 tkinter 窗口出现在任务栏中

python-3.x - 在Python3/tkinter中如何拦截用户单击顶级窗口上的关闭按钮

python - Tkinter 文本输入的返回值,关闭 GUI

python - Hadoop 集群 - 在运行作业之前,我是否需要在所有机器上复制我的代码?

python - 来自 Pydantic 模型的参数解析器

python - 添加多个条目并从中动态检索数据

python - 为什么我的 .after 没有按预期工作?