如何获得 Tkinter 小部件属性? [英] How can I get a Tkinter widget attribute?

查看:33
本文介绍了如何获得 Tkinter 小部件属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个按钮,并设置了它的回调,但是我怎样才能像 fg 这样获得按钮的属性?

I create a Button, and set its callback, but how can I get the attribute of the button like fg?

from Tkinter import *

def callback(self):
    tkMessageBox.showinfo("button", color)

top = Tk()
frame = Frame(top)
frame.pack()

greenbutton = Button(frame, text="Brown", fg="brown", command=callback)
greenbutton.pack( side = RIGHT )

bluebutton = Button(frame, text="Blue", fg="blue", command=callback)
bluebutton.pack( side = LEFT )

top.mainloop()

我只想当我点击蓝色按钮时它会告诉我它是蓝色的.

I just want when I click the button blue and it will tell me it is blue.

推荐答案

每个小部件都有一个名为 cget 的方法,您可以使用它来获取配置值:

Every widget has a method named cget which you can use to get configured values:

print("the foreground of bluebutton is", bluebutton.cget("fg"))

这篇关于如何获得 Tkinter 小部件属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆