[Python] using the "print" command

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

  1. Stack

    Stack Membro Participativo

    I am a beginner, using VS Code on my Windows PC and connecting to a Raspberry Pi 4B over SSH. The following code shard runs well on the remote pi but it has a weird problem... The print() commands print on my host PC's terminal within VS Code but the window stuff displays on the remote pi... How do I get the print to also display on the remote pi? I've highlighted the problematic print statements with #<---------------------this

    Thanks for your help!

    import numpy as np
    import time
    import tkinter as tk
    import sys
    import os
    # to get rid of error "_tkinter.TclError: no display name and no $DISPLAY environment variable" while running remotely from PC over SSH
    if os.environ.get('DISPLAY','') == '':
    print('no display found. Using :0.0')#<---------------------this
    os.environ.__setitem__('DISPLAY', ':0.0')
    # Create the main window
    window = tk.Tk()

    msg = "Roll a dice!"

    print(np.random.randint(1,9))#<---------------------this
    time.sleep(1)

    # Create the main window
    # window = tk.Tk()
    window.title("My First Tkinter App")

    # Create a label
    label = tk.Label(window, text="Hello, Tkinter!")
    label.pack()

    # Create a button
    button = tk.Button(window, text="Click me!",
    command=lambda: print("Button clicked!"))#<----this
    button.pack()

    # Start the main event loop
    window.mainloop()



    I tried to use the "window." function (don't know if that's what it is called) but it doesn't have a print attribute...

    Continue reading...

Compartilhe esta Página