Sublime Forum

show_quick_panel - on_done - more arguments

#1

Hi there,
I’ve been experimenting recently with the call back function run from self.window.show_quick_panel and I can’t figure out if it is possible to pass more then one (implicit) argument - which is the selection index. Thanks in advance for any ideas.
Kuba

0 Likes

#2

Ok, I think I got it - I was missing lambda…

Originally the function “run” was called without the lambda - btw. I still don’t quite understand how python knows if run is a function…
self.window.show_quick_panel(option, run ,sublime.MONOSPACE_FONT)

With lambda I can pass more then one argument to the function.
self.window.show_quick_panel(hdaOptions, lambda id: run(id,arg2,arg3) ,sublime.MONOSPACE_FONT)

Cheers,
kuba

0 Likes

#3

It looks like you have a problem with seeing functions as variables.

Essentially, everything in Python is an object, including integers and functions for example. Some objects are “callable”, like functions and classes. You can assign functions and classes to variables as you please. Defining a function with the “def func(a):” statement for example creates a function and assigns it to the “func” variable. You can then call that function by accessing the variable and adding parenthesis behind it to denote the parameters, which can be empty (e.g. “func(1)”). lambdas in Python are a kind of anonymous functions in that they can be created inline and their expression value can be used without assigning it to a variable, which the “def func():” syntax requires.

So, going back to the Sublime API, the “show_quick_panel” function has the following signature:

show_quick_panel(items, on_done, <flags>, <selected_index>, <on_highlighted>)

Which means you need to specify at least 2 and at most 5 parameters. “on_done” and “on_highlighted” are so-called “callbacks” in that they are called (using the way I outlined above) in certain situations, such as when the selection has changed or when the panel has been closed. This allows you to take action on these events in time without knowing when they occur beforehand. Anyway, you should pass an object/variable that is “callable”, which is a function in most cases, so that the API can call it later. Python tracks the types of objects internally so it would know if your object is actually callable or not. If it’s not, it will throw an error like ‘TypeError: ‘int’ object is not callable’ when trying to execute something like ‘1()’. You can test this before triggering the exception by using the built-in “callable” function which returns a boolean.

Regarding your example, I’m not exactly sure what you want to achieve with that. If I saw your whole code I could probably fix the issue but it’s not clear with the 2 lines you pasted here. (Also I suspect you actually want to use “self.run” and not just “run”.)

0 Likes