如何在创建另一个相同类型的对象时复制一个 tkinter 对象的选项?
How to copy one tkinter object's options when creating another object of the same type?
我正在尝试使用来自另一个矩形的选项在 Tkinter 中绘制一个矩形。我无法硬编码从第一个矩形中获取的 options/which 选项,因为我事先不知道它会有哪些选项。
我使用 options = canvas.itemconfig(first)
获取第一个矩形选项的字典,然后使用绘制第二个矩形
second = canvas.create_rectangle(150, 50, 300, 150, **options)
但出现以下错误:
_tkinter.TclError: bitmap "stipple {} {} {} {}" not defined
然后我过滤选项字典以删除没有值的参数(例如 stipple
),但随后收到以下错误消息:
_tkinter.TclError: unknown color name "black red"
因为 outline
有两个值("black"
和 "red"
)虽然我在绘制第一个矩形时只给它一个值
我也给了第一个矩形两个标签,'rect'
和'orig'
,已经改为'rect orig'
以下是选项字典在过滤没有值的参数之前和之后的样子:
原文字典:
{'stipple': ('stipple', '', '', '', ''), 'disabledoutlinestipple': ('disabledoutlinestipple', '', '', '', ''), 'offset': ('offset', '', '', '0,0', '0,0'), 'dash': ('dash', '', '', '', ''), 'disabledwidth': ('disabledwidth', '', '', '0.0', '0'), 'activeoutlinestipple': ('activeoutlinestipple', '', '', '', ''), 'dashoffset': ('dashoffset', '', '', '0', '0'), 'activewidth': ('activewidth', '', '', '0.0', '0.0'), 'fill': ('fill', '', '', '', 'blue'), 'disabledoutline': ('disabledoutline', '', '', '', ''), 'disabledfill': ('disabledfill', '', '', '', ''), 'disableddash': ('disableddash', '', '', '', ''), 'width': ('width', '', '', '1.0', '1.0'), 'state': ('state', '', '', '', ''), 'outlinestipple': ('outlinestipple', '', '', '', ''), 'disabledstipple': ('disabledstipple', '', '', '', ''), 'activedash': ('activedash', '', '', '', ''), 'tags': ('tags', '', '', '', 'rect orig'), 'activestipple': ('activestipple', '', '', '', ''), 'activeoutline': ('activeoutline', '', '', '', ''), 'outlineoffset': ('outlineoffset', '', '', '0,0', '0,0'), 'activefill': ('activefill', '', '', '', ''), 'outline': ('outline', '', '', 'black', 'red')}
过滤字典:
{'outline': ('black', 'red'), 'width': ('1.0', '1.0'), 'offset': ('0,0', '0,0'), 'disabledwidth': ('0.0', '0'), 'outlineoffset': ('0,0', '0,0'), 'dashoffset': ('0', '0'), 'activewidth': ('0.0', '0.0'), 'tags': ('rect orig',), 'fill': ('blue',)}
这里是原始代码:
from Tkinter import *
root = Tk()
canvas = Canvas(root, width=600, height=400)
canvas.pack()
first = canvas.create_rectangle(50, 50, 200, 150, outline="red",
fill="blue", tags=("rect", "org"))
options = canvas.itemconfig(first)
print options
#second = canvas.create_rectangle(150, 50, 300, 150, **options)
root.mainloop()
如您所见,itemconfig
不 return 只是一个简单的 key/value 对字典。对于每个选项,它将 return 一个由以下五项组成的元组:
- 选项名称
- 选项数据库的选项名称
- 选项 class 选项数据库
- 默认值
- 当前值
如果您想复制所有选项,则需要为每个选项 return 编辑最后一项。
你可以通过字典理解很容易地做到这一点:
config = canvas.itemconfig(canvas_tag_or_id)
new_config = {key: config[key][-1] for key in config.keys()}
canvas.create_rectangle(coords, **new_config)
有关详细信息,请参阅
我正在尝试使用来自另一个矩形的选项在 Tkinter 中绘制一个矩形。我无法硬编码从第一个矩形中获取的 options/which 选项,因为我事先不知道它会有哪些选项。
我使用 options = canvas.itemconfig(first)
获取第一个矩形选项的字典,然后使用绘制第二个矩形
second = canvas.create_rectangle(150, 50, 300, 150, **options)
但出现以下错误:
_tkinter.TclError: bitmap "stipple {} {} {} {}" not defined
然后我过滤选项字典以删除没有值的参数(例如 stipple
),但随后收到以下错误消息:
_tkinter.TclError: unknown color name "black red"
因为 outline
有两个值("black"
和 "red"
)虽然我在绘制第一个矩形时只给它一个值
我也给了第一个矩形两个标签,'rect'
和'orig'
,已经改为'rect orig'
以下是选项字典在过滤没有值的参数之前和之后的样子:
原文字典:
{'stipple': ('stipple', '', '', '', ''), 'disabledoutlinestipple': ('disabledoutlinestipple', '', '', '', ''), 'offset': ('offset', '', '', '0,0', '0,0'), 'dash': ('dash', '', '', '', ''), 'disabledwidth': ('disabledwidth', '', '', '0.0', '0'), 'activeoutlinestipple': ('activeoutlinestipple', '', '', '', ''), 'dashoffset': ('dashoffset', '', '', '0', '0'), 'activewidth': ('activewidth', '', '', '0.0', '0.0'), 'fill': ('fill', '', '', '', 'blue'), 'disabledoutline': ('disabledoutline', '', '', '', ''), 'disabledfill': ('disabledfill', '', '', '', ''), 'disableddash': ('disableddash', '', '', '', ''), 'width': ('width', '', '', '1.0', '1.0'), 'state': ('state', '', '', '', ''), 'outlinestipple': ('outlinestipple', '', '', '', ''), 'disabledstipple': ('disabledstipple', '', '', '', ''), 'activedash': ('activedash', '', '', '', ''), 'tags': ('tags', '', '', '', 'rect orig'), 'activestipple': ('activestipple', '', '', '', ''), 'activeoutline': ('activeoutline', '', '', '', ''), 'outlineoffset': ('outlineoffset', '', '', '0,0', '0,0'), 'activefill': ('activefill', '', '', '', ''), 'outline': ('outline', '', '', 'black', 'red')}
过滤字典:
{'outline': ('black', 'red'), 'width': ('1.0', '1.0'), 'offset': ('0,0', '0,0'), 'disabledwidth': ('0.0', '0'), 'outlineoffset': ('0,0', '0,0'), 'dashoffset': ('0', '0'), 'activewidth': ('0.0', '0.0'), 'tags': ('rect orig',), 'fill': ('blue',)}
这里是原始代码:
from Tkinter import *
root = Tk()
canvas = Canvas(root, width=600, height=400)
canvas.pack()
first = canvas.create_rectangle(50, 50, 200, 150, outline="red",
fill="blue", tags=("rect", "org"))
options = canvas.itemconfig(first)
print options
#second = canvas.create_rectangle(150, 50, 300, 150, **options)
root.mainloop()
如您所见,itemconfig
不 return 只是一个简单的 key/value 对字典。对于每个选项,它将 return 一个由以下五项组成的元组:
- 选项名称
- 选项数据库的选项名称
- 选项 class 选项数据库
- 默认值
- 当前值
如果您想复制所有选项,则需要为每个选项 return 编辑最后一项。
你可以通过字典理解很容易地做到这一点:
config = canvas.itemconfig(canvas_tag_or_id)
new_config = {key: config[key][-1] for key in config.keys()}
canvas.create_rectangle(coords, **new_config)
有关详细信息,请参阅