【问题标题】:Print text in new view (unsaved buffer), not in console在新视图(未保存的缓冲区)中打印文本,而不是在控制台中
【发布时间】:2017-04-24 07:32:43
【问题描述】:

1。简要

我不知道如何在新视图中获得输出,而不是在 Sublime Text 控制台中。


2。预期行为

例如,我有倒计时:

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⌘⇧p for Mac)→SublimeREPL: Python RUN current file→我得到预期的行为:


3。实际行为

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 控制台中看到输出,而不是在新视图中。


4。没有帮助

  1. Python write() method 适用于文件,而不适用于未保存的缓冲区。
  2. 我在 Sublime Text 3 API documentation 中找到了如何打开新视图 — sublime.active_window().new_file() — 但我没有找到如何在此视图中打印文本。

【问题讨论】:

    标签: python sublimetext3 sublimetext sublime-text-plugin


    【解决方案1】:

    您可以使用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 })
    

    【讨论】:

    • Keith Hall,对不起,我不明白,如何在这段代码块中的不同操作之间进行延迟。 Part of my code预期行为Time over! 在新文件中打印 → 5 秒延迟 → 文件关闭。 实际行为:延迟 5 秒 → Time over! 打印新文件并同时关闭文件。谢谢。
    • 你可以使用sublime.set_timeout_async来延迟动作——在Time over!写入5秒后关闭视图,你可以在text = 'Time over!'下添加以下内容(缩进级别相同):@987654334 @。我不确定您的 force_quit 命令来自哪里,但您可以使用它来代替 - sublime.set_timeout_async(lambda: view.window().run_command('force_quit'), 5000)
    • 基思霍尔:对不起。 1。设置this project,我安装并添加到PATH环境变量mpg1232。预期行为this Python code works for me,我听到声音。,3。实际行为:如果我从我的项目中运行sacagawea_standardo 命令,则不会播放声音。在另一个我的插件中,我成功使用了subprocess.Popen。我做错了什么? 4。没有帮助:如果我使用os.system 而不是subprocess.Popen,我会得到实际行为,而不是预期。 // 谢谢。
    • 很可能mpg123 找不到mp3 文件,因为它们不在当前工作目录中 - 尝试在subprocess 调用或changing Python's current working directory 中指定文件的完整路径在执行subprocess 调用之前。您还可以使用check=True 作为参数来确保进程成功执行。
    • 'scroll_to_end': True 对我没有影响。滚动条不会用数字滚动到底部。谢谢。
    猜你喜欢
    • 1970-01-01
    • 2019-08-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多