在 Tkinter 中获取 Label 的 bg 属性 的值
Getting the value of the bg property of a Label in Tkinter
我一直在学习 ui 使用 Tkinter 编程,并且我已经走得很远了。我可以 configure/change text, fg
, and bg
Label 的属性,但我不知道如何获取 bg
属性 的 Label
。
有什么方法可以将 Tkinter 标签的 bg
值保存到变量中,以便与其他值进行比较?
您可以使用 cget
方法获取小部件属性的值。示例:
if my_widget.cget("background") == "red":
print "The widget is red"
您可以使用 cget()
:
label.cget('background')
或者您可以像对待字典一样对待标签:
label['background']
示例:
from Tkinter import *
main = Tk()
l = Label(main, text = "Label", background = "lime")
l.pack()
if l["background"] == 'lime':
print "Lime!"
if l.cget("background") == 'lime':
print "Still Lime!"
main.mainloop()
控制台输出:
Lime!
Still Lime!
我一直在学习 ui 使用 Tkinter 编程,并且我已经走得很远了。我可以 configure/change text, fg
, and bg
Label 的属性,但我不知道如何获取 bg
属性 的 Label
。
有什么方法可以将 Tkinter 标签的 bg
值保存到变量中,以便与其他值进行比较?
您可以使用 cget
方法获取小部件属性的值。示例:
if my_widget.cget("background") == "red":
print "The widget is red"
您可以使用 cget()
:
label.cget('background')
或者您可以像对待字典一样对待标签:
label['background']
示例:
from Tkinter import *
main = Tk()
l = Label(main, text = "Label", background = "lime")
l.pack()
if l["background"] == 'lime':
print "Lime!"
if l.cget("background") == 'lime':
print "Still Lime!"
main.mainloop()
控制台输出:
Lime!
Still Lime!