将自定义属性添加到 Tk 小部件 [英] Add custom attributes to a Tk widget
本文介绍了将自定义属性添加到 Tk 小部件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的主要目标是向小部件添加诸如隐藏标签或字符串之类的内容,以保存其上的简短信息.我想到了创建一个新的自定义 Button 类(在本例中我需要按钮),它继承了所有旧选项.
My main target is to add something like an hidden tag or string to a widget, to save short information on it. I got the idea of creating a new custom Button class (in this case I need buttons), which inherits all the old options.
这是代码:
form tkinter import *
class NButton(Button):
def __init__(self, master, tag=None, *args, **kwargs):
Button.__init__(self, master, *args, **kwargs)
self.master, self.tag = master, tag
创建新的 NButton 实例时没有问题:
No trouble when creating a new NButton instance:
aria1 = NButton(treewindow, bd=2, relief=GROOVE, text="Trasmissione\naerea 1", bg="#99c4ff", tag="aria 1")
aria1.place(x=20, y=20)
尝试获取tag
的值时出现问题:
The problems come out when I try to get the value of tag
:
aria1["tag"]
它返回:
_tkinter.TclError: 未知选项-tag"
_tkinter.TclError: unknown option "-tag"
我该如何解决这个问题?
How can I solve this?
推荐答案
您需要访问自定义选项作为对象属性:
You need to access your custom options as object attributes:
print(aria1.tag)
这篇关于将自定义属性添加到 Tk 小部件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文