Tkinter:获取 Canvas 属性和选项值
Tkinter: Getting Canvas Attribute and Options Values
我想这是一个相当基本的 Tkinter 问题 - 但我是一个菜鸟,经过一番搜索后我还没有看到这个答案。
我希望能够检查我的 canvas 在 Tkinter 中的属性。
所以,
canvas = tk.Canvas(root, 200,200, bg="blue")
canvas2 = tk.Canvas(root, 200,200, bg="red")
canvases = [canvas, canvas2]
我要找的是检查 canvas 的属性的东西。例如 -
for canvas in canvases:
if canvas.get_color() == "red": # IS THERE SOMETHING LIKE get_color... or get_attr(bg)?
print("HECK YA")
else:
print("I'm feeling blue")
感谢您的帮助!
您可以调用canvas.config('attribute')
来获取给定属性的值。
比如canvas.config('bg')
returns背景值
不带参数调用 canvas.config()
将 return 当前配置的字典
Universal Widget methods that relate to configuration of options:
The methods are defined on all widgets. In the descriptions, w can be any widget of any type.
w.cget(option)
: Returns the current value of option as a string. You can also get the value of an option for widget w as w[option].
w.config(option=value, ...)
Same as .configure().
w.configure(option=value, ...)
Set the values of one or more options. For the options whose names are Python reserved words (class, from, in), use a trailing underbar: 'class_', 'from_', 'in_'.
You can also set the value of an option for widget w with the statement w[option] = value
If you call the .config()
method on a widget with no arguments, you'll get a dictionary of all the widget's current options. The keys are the option names (including aliases like bd for borderwidth). The value for each key is:
for most entries, a five-tuple: (option name, option database key, option database class, default value, current value)
; or,
for alias names (like 'fg'), a two-tuple: (alias name, equivalent standard name)
.
我想这是一个相当基本的 Tkinter 问题 - 但我是一个菜鸟,经过一番搜索后我还没有看到这个答案。
我希望能够检查我的 canvas 在 Tkinter 中的属性。 所以,
canvas = tk.Canvas(root, 200,200, bg="blue")
canvas2 = tk.Canvas(root, 200,200, bg="red")
canvases = [canvas, canvas2]
我要找的是检查 canvas 的属性的东西。例如 -
for canvas in canvases:
if canvas.get_color() == "red": # IS THERE SOMETHING LIKE get_color... or get_attr(bg)?
print("HECK YA")
else:
print("I'm feeling blue")
感谢您的帮助!
您可以调用canvas.config('attribute')
来获取给定属性的值。
比如canvas.config('bg')
returns背景值
不带参数调用 canvas.config()
将 return 当前配置的字典
Universal Widget methods that relate to configuration of options:
The methods are defined on all widgets. In the descriptions, w can be any widget of any type.
w.cget(option)
: Returns the current value of option as a string. You can also get the value of an option for widget w as w[option].
w.config(option=value, ...)
Same as .configure().
w.configure(option=value, ...)
Set the values of one or more options. For the options whose names are Python reserved words (class, from, in), use a trailing underbar: 'class_', 'from_', 'in_'.You can also set the value of an option for widget w with the statement
w[option] = value
If you call the
.config()
method on a widget with no arguments, you'll get a dictionary of all the widget's current options. The keys are the option names (including aliases like bd for borderwidth). The value for each key is:for most entries, a five-tuple:
(option name, option database key, option database class, default value, current value)
; or,for alias names (like 'fg'), a two-tuple:
(alias name, equivalent standard name)
.