Sublime Forum

ST2 Input

#1

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.

0 Likes

#2

You can use .show_quick_panel() for that?

I meant: .show_input_panel()

0 Likes

#3

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

If I use code as

self.view.window().show_input_panel("User","Hi",RemoteCommand.getUsername,None,None)

nothing shows up. But if I use code as

view.window().show_input_panel("User","Hi",RemoteCommand.getUsername,None,None)

I get the following error:

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

def run(self, view, mode):

Any ideas on how to make the input panel show up?

0 Likes

#4

[code]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)

[/code]

I keep forgetting the edit object too. I believe that’s what’s causing troubles here.

0 Likes

#5

The signature for a TextCommand should be:

def run(self, edit)

From a TextCommand, you can access the window via:

self.view.window()

Assuming you are using a TextCommand, try the following code:

self.view.window().show_input_panel('Enter a value:', 'default text', self.callback_function, None, None)

Hope that helps.

0 Likes

#6

Oops, looks like guillermooo beat me to it :smile:

0 Likes

#7

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…

0 Likes

#8

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…

0 Likes

#9

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.

0 Likes

#10

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.

0 Likes

#11

From the goto line, it uses

class PromptGotoLineCommand(sublime_plugin.WindowCommand):

    def run(self):
        self.window.show_input_panel("Goto Line:", "", self.on_done, None, None)
        pass

but, with my Plugin, I use
.TextCommand, it seems I have to use the window() syntax

class Example1Command(sublime_plugin.TextCommand):
    def run(self, edit):
        # 'something' is the default message
        self.view.window().show_input_panel("Say something-:", 'something', self.on_done, None, None)

Is there anything wrong with that?

Thanks
Rob

0 Likes

#12

import sublime, sublime_plugin

class foxpro_brow(sublime_plugin.WindowCommand):
def run(self):
self.window.show_input_panel(“Browse:”, “”, self.on_done, None, None)
pass<---- you are missing this keyword.

def on_done(self,user_input):
	self.window.run_command("exec",{"cmd":"brow.bat",user_input],"working_dir": "\\program\\start\\"})
	pass<---- you are missing this keyword.

I just noticed u are missing a keyword “pass” just after showing the input panel or doing the job it has to. Try it out.

0 Likes