是否可以在 Tkinter 中获取小部件设置?
如果我能得到像下面这样的东西那就太棒了。
伪代码:
U = widget1.SettingsGet()
Print U
在打印 U 时,将返回类似这样的内容:
widget1(background='green',foreground='grey',boarderwidth=10, relief='flat')
能够获取小部件设置将非常有用。这样我就可以相应地操作其他小部件。
It'd be awesome if I could get something like the below.
Pseudo Code:
U = widget1.SettingsGet()
Print U
Upon printing U something like this would be returned:
widget1(background='green',foreground='grey',boarderwidth=10, relief='flat')
It would be really useful to be able to get a widgets settings. So that I can manipulate other widgets accordingly.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果你知道你需要什么设置,你可以使用
cget
方法来获取值,例如它将打印
如果你想知道所有可用的选项,widget.config包含配置,如果你希望您可以创建您可能需要的全部或部分设置,例如
输出:
If you know what settings you need, you can just use the
cget
method to get values e.g.It will print
If you want to know all the available options, widget.config contains the config and from that if you wish you can create all or a subset of settings which you may need e.g.
Output:
要从 cget 获取小部件的所有属性,可以使用keys() 获取属性,然后使用cget 获取这些属性的值。 IE:
此代码返回:
那是很多空格>。<
在此块中,第一个是键,第二个是所述键的值。键方法(对于所有小部件)返回所述小部件中的所有键。 Config 返回键及其适用范围(光标与光标),其中键仅给出所有属性的列表。
To get all attributes of a widget from cget, you can use keys() to get the attributes and then cget to get the value of those atributes. IE:
This code returns:
That was a lot of spaces >.<
In this block the first is the key and the second is the value of said key. The keys method (for all widgets) returns all of the keys in said widget. Config returns the keys and what they apply to (cursor vs Cursor) where keys just gives a list of all attributes.
它可能不应该出现在生产代码中,因为它使用
_tkinter
,但你可以这样做:which 为我输出:
It probably shouldn't be in production code since it makes use of
_tkinter
, but you can do:which outputs for me: