Home Download Buy Blog Forum Support

ST2 Input

ST2 Input

Postby samkerr on Thu Jul 14, 2011 2:11 pm

In ST2, is there a way to get input from the user by using a type of dialog box or something similar?

I could just have the user write some things in a buffer, but that seems cumbersome and error-prone when a dialog box with a text field would be better.
samkerr
 
Posts: 51
Joined: Mon Aug 03, 2009 11:20 pm

Re: ST2 Input

Postby guillermooo on Thu Jul 14, 2011 2:15 pm

You can use .show_quick_panel() for that?

>>

I meant: .show_input_panel()
guillermooo
 
Posts: 729
Joined: Thu Jul 23, 2009 9:06 am

Re: ST2 Input

Postby samkerr on Thu Jul 14, 2011 2:47 pm

I found the show_input_panel method, but am having trouble using it.

If I use code as

Code: Select all
self.view.window().show_input_panel("User","Hi",RemoteCommand.getUsername,None,None)


nothing shows up. But if I use code as

Code: Select all
view.window().show_input_panel("User","Hi",RemoteCommand.getUsername,None,None)


I get the following error:

Code: Select all
Traceback (most recent call last):
  File ".\sublime_plugin.py", line 249, in run_
  File ".\remote.py", line 44, in run
    view.window().show_input_panel("User","Hi",MyCommand.CallbackFunction,None,None)
AttributeError: 'Edit' object has no attribute 'window'


My function signature looks like

Code: Select all
def run(self, view, mode):


Any ideas on how to make the input panel show up?
samkerr
 
Posts: 51
Joined: Mon Aug 03, 2009 11:20 pm

Re: ST2 Input

Postby guillermooo on Thu Jul 14, 2011 2:57 pm

Code: Select all
import sublime, sublime_plugin

class ExampleCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        self.view.window().show_input_panel("Say something:", 'something', self.on_done, None, None)
   
    def on_done(self, user_input):
        sublime.status_message("User said: " + user_input)


I keep forgetting the edit object too. I believe that's what's causing troubles here.
guillermooo
 
Posts: 729
Joined: Thu Jul 23, 2009 9:06 am

Re: ST2 Input

Postby jbjornson on Thu Jul 14, 2011 3:00 pm

The signature for a TextCommand should be:
Code: Select all
def run(self, edit)


From a TextCommand, you can access the window via:
Code: Select all
self.view.window()


Assuming you are using a TextCommand, try the following code:
Code: Select all
self.view.window().show_input_panel('Enter a value:', 'default text', self.callback_function, None, None)


Hope that helps.
jbjornson
 
Posts: 350
Joined: Tue Oct 27, 2009 4:25 pm
Location: Zurich, Switzerland

Re: ST2 Input

Postby jbjornson on Thu Jul 14, 2011 3:01 pm

Oops, looks like guillermooo beat me to it :-)
jbjornson
 
Posts: 350
Joined: Tue Oct 27, 2009 4:25 pm
Location: Zurich, Switzerland

Re: ST2 Input

Postby samkerr on Thu Jul 14, 2011 3:06 pm

But when I do the self.view.window() call, no actual input window appears.

Actually, it seems like the window appears sporadically, but I cannot get it to appear every time...
samkerr
 
Posts: 51
Joined: Mon Aug 03, 2009 11:20 pm

Re: ST2 Input

Postby guillermooo on Thu Jul 14, 2011 3:15 pm

Hm... There's no "window" per se; it's just a panel along the bottom of the window. But I suppose you were referring to that?

For the record, it works fine on Windows...
guillermooo
 
Posts: 729
Joined: Thu Jul 23, 2009 9:06 am

Re: ST2 Input

Postby samkerr on Thu Jul 14, 2011 3:25 pm

Well the problem gets even stranger.

If I type 'view.window().show_input_panel('Enter a value:', 'default text', None, None, None)' into the Sublime command window (ctrl+`), the input panel shows up just fine. But 'self.view.window().show_input_panel('Enter a value:', 'default text', None, None, None)' fails from my plugin file.

For reference, I am on Windows 7 x64, Beta build 2076.
samkerr
 
Posts: 51
Joined: Mon Aug 03, 2009 11:20 pm

Re: ST2 Input

Postby jps on Sat Jul 16, 2011 3:21 am

As a general rule, you should almost never be using view.window()

If you're operating on a window, you're usually better off being a WindowCommand. Take a look at Default/goto_line.py for an example.
jps
Site Admin
 
Posts: 3071
Joined: Wed Mar 19, 2008 12:33 pm

Next

Return to Plugin Development

Who is online

Users browsing this forum: No registered users and 5 guests