[Python] How do I display terminal output from one python script in another kivy script

Discussão em 'Python' iniciado por Stack, Fevereiro 16, 2025.

  1. Stack

    Stack Membro Participativo

    I have a python script named "main.py" which basically uses the microphone of the user, recognizes commands and executes a task. If I say "Hello" the script will process this, answer with "Hello" and execute a command like this: print("Hello user!")

    What I'm trying to do is display the output or basically the "print" from the main function into the kivy ouput_textbox. I've already asked chatgpt but it still doesn't work. The following code is from the boot.py file which starts the kivy window. The window itself works fine.

    def bootEngram(self, instance):
    try:
    output_textbox = TextInput(
    readonly=True,
    background_color=(0, 0, 0, 1),
    foreground_color="#55ead4",
    font_size=14,
    font_name=fontFile,
    multiline=True
    )

    command = ["python", "D:\\silverhand\\code\\main.py"]
    process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True, bufsize=1, universal_newlines=True)

    def update_output_text(instance, line):
    instance.text += line

    def read_output():
    for line in iter(process.stdout.readline, ''):
    Clock.schedule_once(lambda dt: update_output_text(output_textbox, line)) # Sichere UI-Aktualisierung
    process.stdout.close()

    for err in iter(process.stderr.readline, ''):
    Clock.schedule_once(lambda dt: update_output_text(output_textbox, f"Error: {err}"))
    process.stdout.close()
    process.stderr.close()

    process.wait()
    Clock.schedule_once(lambda dt: output_popup.dismiss())

    threading.Thread(target=read_output, daemon=True).start()

    popup_content = BoxLayout(orientation="vertical", spacing = 10)


    title_bar = BoxLayout(orientation="horizontal", size_hint_y=None, height=40, padding=(10, 5))

    title_label = Label(
    text="Engram",
    color="#f3e600",
    font_name=fontFile,
    font_size=20,
    bold=True,
    halign="left",
    size_hint_x = 1,
    valign = "middle"
    )

    close_button = Button(
    text="<",
    size_hint=(None, None),
    size=(30, 30),
    background_color=(0, 0, 0, 1),
    color=(1, 1, 1, 1),
    bold=True,
    size_hint_x = None
    )

    close_button.bind(on_press=lambda x: output_popup.dismiss())
    #close_button.bind(on_press=lambda x: process.stdin.write("exit\n"))

    title_bar.add_widget(title_label)

    popup_content.add_widget(output_textbox)
    popup_content.add_widget(close_button)


    output_popup = Popup(
    title="Engram",
    title_color="#f3e600",
    title_size=20,
    title_font=fontFile,
    size_hint=(0.9, 0.9),
    separator_color="#c5003c",
    #separator_height = 0,
    auto_dismiss=False,
    content=popup_content
    )

    output_popup.open()

    except Exception as e:
    print(f"Error: {e}")

    Continue reading...

Compartilhe esta Página