prompt toolkit run functions simultaneously

497 views Asked by At

I am trying to run the testprt() function simultaneously with the prompt_toolkit but if I am trying it normally its waiting and after the testprt() functions is run the menu starts but I want it too start simultaneously. I tried it with normal threading and starting the thread in the run function. (This is the code without my threading code) how do I run the testprt() function simultaneously?

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

There are 0 answers