提示工具包同时运行功能

时间:2018-09-14 22:45:20

标签: python-3.x prompt-toolkit

im试图与hint_toolkit同时运行testprt()函数,但是如果我尝试正常运行它,则等待并在运行testprt()函数之后,菜单启动,但我希望它也同时启动。我尝试使用普通线程并在run函数中启动线程。 (这是没有线程代码的代码)如何同时运行testprt()函数?

from __future__ import unicode_literals
from prompt_toolkit.application import Application
from prompt_toolkit.key_binding import KeyBindings
from prompt_toolkit.layout.containers import VSplit, HSplit, Window
from prompt_toolkit.layout.controls import FormattedTextControl, BufferControl
from prompt_toolkit.layout.layout import Layout
from prompt_toolkit.buffer import Buffer
from prompt_toolkit import prompt
from prompt_toolkit.eventloop.defaults import use_asyncio_event_loop
from prompt_toolkit.patch_stdout import patch_stdout
import threading
import  time


## LOGIC
def testprt():
    up_r_buffer.text = "1\n"
    time.sleep(1)
    up_r_buffer.text = "2\n"
    time.sleep(1)
    up_r_buffer.text = "3\n"
    time.sleep(1)
    up_r_buffer.text = "4\n"
    time.sleep(1)
    up_r_buffer.text = "5\n"
    time.sleep(1)


up_r_buffer = Buffer()
up_l_buffer = Buffer()
down_buffer = Buffer()

# 1. The layout
top_text = (
    "Menu.\n"
    "[q] Quit [TAB] To switch."
)

left_top = Window(BufferControl(buffer=up_l_buffer))
right_top = Window(BufferControl(buffer=up_r_buffer))
down_buffer = Window(BufferControl(buffer=down_buffer))


body = HSplit([
     Window(FormattedTextControl(top_text), height=2, style='reverse'),
    Window(height=1, char='-'),  # Horizontal line in the middle.
    VSplit([
        left_top,
        Window(width=1, char='|'),
        right_top
    ]),
    Window(height=1, char='-'),  # Horizontal line in the middle.
    VSplit([
        down_buffer
    ]),
])

# 2. Key bindings
kb = KeyBindings()


@kb.add('q')
def _(event):
    " Quit application. "
    event.app.exit()

@kb.add('tab')
def _(event):
    event.app.layout.focus_next()


@kb.add('s-tab')
def _(event):
    event.app.layout.focus_previous()

@kb.add('m')
def _(event):
    up_r_buffer.text = "FuckOFF\n"

# 3. The `Application`
application = Application(
    layout=Layout(body),
    key_bindings=kb,
    full_screen=True,
    )


def run():
    application.run()

if __name__ == '__main__':
    run()

0 个答案:

没有答案