你如何调用 gimp_file_load?

Tha*_*tos 2 python gimp gimpfu


>>> pdb.gimp_file_load.nparams
3
>>> pprint.pprint(pdb.gimp_file_load.params)
((0,
  'run-mode',
  'The run mode { RUN-INTERACTIVE (0), RUN-NONINTERACTIVE (1) }'),
 (4, 'filename', 'The name of the file to load'),
 (4, 'raw-filename', 'The name as entered by the user'))
>>> fname = 'a filename'
>>> img = pdb.gimp_file_load(gimpfu.RUN_NONINTERACTIVE, fname, fname)
Traceback (most recent call last):
  File "<input>", line 1, in <module>
TypeError: wrong number of parameters

那么,我在这里做错了什么?根据方法本身,它需要三个相当详细的参数。我将它想要的三样东西传递给它,然后我收到一个TypeError. 所以:

  1. 我究竟做错了什么?
  2. 有没有这方面的参考手册?
  3. 在参数的元组中,有一个 0、一个 4 和一个 4。这些神奇的常数是什么?根据文档,这些似乎是:

    参数类型(PARAM_* 常量之一)

    但是在这些文档中我没有找到PARAM_常量,而且我没有发现它们内省任何 pdb、gimp 或 gimpfu。

只是为了完整:显而易见的,help(pdb.gimp_file_load), 并不是那么有帮助:

>>> help(pdb.gimp_file_load)
Help on PDBFunction object:

class PDBFunction(__builtin__.object)
 |  Methods defined here:
 |  
 |  __call__(...)
 |      x.__call__(...) <==> x(...)
 |  
 |  __repr__(...)
 |      x.__repr__() <==> repr(x)
 |  
 |  ----------------------------------------------------------------------
 |  Data descriptors defined here:
 |  
 |  nparams
 |  
 |  nreturn_vals
 |  
 |  params
 |  
 |  proc_author
 |  
 |  proc_blurb
 |  
 |  proc_copyright
 |  
 |  proc_date
 |  
 |  proc_help
 |  
 |  proc_name
 |  
 |  proc_type
 |  
 |  return_vals
 |  
 |  ----------------------------------------------------------------------
 |  Data and other attributes defined here:
 |  
 |  __new__ = <built-in method __new__ of type object>
 |      T.__new__(S, ...) -> a new object with type S, a subtype of T
Run Code Online (Sandbox Code Playgroud)

Tha*_*tos 5

这在 GIMP 的“程序数据库”和围绕它的 Python 包装器之间的抽象中似乎有点漏洞。run_mode, AFAICT, 是一个特殊的孩子,是一个可选的,只有关键字的参数。例如,这有效:

>>> img = pdb.gimp_file_load(fname, fname, run_mode=gimpfu.RUN_NONINTERACTIVE)
>>> img
<gimp.Image 'fname.xcf'>
Run Code Online (Sandbox Code Playgroud)

正如这个消息来源所说:

过程浏览器到 Python:

  • 将破折号改为下划线
  • 省略任何运行模式参数
  • 将 -1 更改为无

(强调我的);你真的不需要省略它,它只需要是一个关键字 arg。(如我的示例所示。)关键字 args 必须跟在 Python 中的非关键字 args 之后。您也可以不使用它,我猜它假定某种默认值。(虽然我不知道是哪个。)

据推测,PARAM_我在问题中好奇的 " " 常量揭示了这一点,除了我在文档或自省中找不到整数的符号/命名版本。