我无法找到,如何在新视图中输出,而不是在Sublime Text控制台中。
例如,我有倒数计时器:
import time
seconds = 10
while seconds >= 0:
time.sleep(1)
if seconds == 5:
print("5 seconds")
elif seconds == 0:
print("Time over!")
time.sleep(5)
else:
print(seconds)
seconds -= 1
Ctrl + Shift + P (对于Mac,⌘⇧p)→SublimeREPL: Python RUN current file
→我得到了预期的行为:
Sublime Text插件中的倒数计时器相同:
import time
import sublime_plugin
seconds = 10
class LovernaStandardCommand(sublime_plugin.TextCommand):
def run(self, edit):
current_id = self.view.id()
if current_id:
global seconds
while seconds >= 0:
time.sleep(1)
if seconds == 5:
print("5 seconds")
elif seconds == 0:
print("Time over!")
time.sleep(5)
else:
print(seconds)
seconds -= 1
我运行命令loverna_standard
→我在Sublime Text控制台中看到输出,而不是在新视图中。
sublime.active_window().new_file()
- 但我找不到,如何在此视图中打印文字。答案 0 :(得分:2)
您可以使用append
命令将文本附加到Sublime Text视图。
例如,如果您希望视图在添加新文本时自动滚动到底部:
view.run_command('append', { 'characters': str(seconds) + '\n', 'scroll_to_end': True })
但是,最好每秒设置一次回调而不是睡眠,否则在命令运行时ST将无响应。
import sublime
import sublime_plugin
class LovernaStandardCommand(sublime_plugin.TextCommand):
def run(self, edit, seconds=10):
self.print_seconds(sublime.active_window().new_file(), seconds)
def print_seconds(self, view, seconds):
text = ''
if seconds > 0:
text = str(seconds)
if seconds == 5:
text += ' seconds'
sublime.set_timeout_async(lambda: self.print_seconds(view, seconds - 1), 1000)
else:
text = 'Time over!'
view.run_command('append', { 'characters': text + '\n', 'scroll_to_end': True })