【问题标题】:How to call method keys in Python 3如何在 Python 3 中调用方法键
【发布时间】:2015-09-09 09:49:50
【问题描述】:

我现在正在学习 Tkinter。要获得有关方法的更多信息,我阅读了 pydocs。

问题是我打电话的时候:

>>>help(Text.configure)

我明白了:

configure(self, cnf=None, **kw)
Configure resources of a widget.

The values for resources are specified as keyword
arguments. To get an overview about
the allowed keyword arguments call the method keys.

那么我将如何调用或列出所有方法键?

我试过了:

>>>Text.configure.keys()

>>>Text.configure().keys()

但它们当然不起作用,因为它是一个函数而不是字典。

【问题讨论】:

  • 或许可以试试Text().keys()
  • @ReutSharabani 那是TypeError;它是一个实例方法,而不是一个类方法
  • @jonrsharpe 是的,我在猜,但我看到你已经回答了。
  • 也刚刚意识到>>>help(Text) 也显示了它们,只是它们的名称不同。

标签: python python-3.x tkinter


【解决方案1】:

keys是所有Widget子类的实例方法,可以通过如下方式访问:

>>> from Tkinter import Text
>>> help(Text.keys)
Help on method keys in module Tkinter:

keys(self) unbound Tkinter.Text method
    Return a list of all resource names of this widget.

>>> Text().keys()
['autoseparators', 'background', 'bd', 'bg', 'blockcursor', 'borderwidth', 'cursor', 
 'endline', 'exportselection', 'fg', 'font', 'foreground', 'height', 'highlightbackground',
 'highlightcolor', 'highlightthickness', 'inactiveselectbackground', 'insertbackground',
 'insertborderwidth', 'insertofftime', 'insertontime', 'insertwidth', 'maxundo', 'padx',
 'pady', 'relief', 'selectbackground', 'selectborderwidth', 'selectforeground', 'setgrid',
 'spacing1', 'spacing2', 'spacing3', 'startline', 'state', 'tabs', 'tabstyle', 'takefocus',
 'undo', 'width', 'wrap', 'xscrollcommand', 'yscrollcommand']

【讨论】:

  • 所以这只适用于 Tkinter,对吧?因此,如果函数或方法接受 **kw,我如何检查它们是什么?或者 keys 方法是某种约定?
  • @T.Chmelevskij 是的,只有 Tkinter(据我所知)。在一般情况下,请遵循文档中为您正在使用的功能/方法提供的任何指导。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-01-15
  • 2014-11-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多