View Single Post
Old 28-09-2023, 08:29   #1
steno12
Junior Member
 
Iscritto dal: Sep 2023
Messaggi: 6
[Python] All'avvio non visualizza la finestra

Buongiorno a tutti

spero sia postato bene il codice.

voglio digitare quindi intercettare il tasto premuto e scriverlo in una label
la label dove scrivere è "welcome_label1"

ok

ma se lancio il codice non visualizza la finestra e qu8indi non posso fare nulla.

Ma... leggi sotto...


Codice:
import tkinter as tk
from pynput import keyboard  
import keyboard
from pynput.keyboard import Key, Controller 
from pynput.keyboard import Listener  

key = 0

# instantiating the Controller class  
the_keyboard = Controller()  

# using the press() and release() methods  
# the_keyboard.press('x')  
# the_keyboard.release('x')  

window = tk.Tk()
window.geometry("600x600")
window.title("Hello TkInter!")
window.resizable(False, False)
window.configure(background="white")


def close_window():
   window.destroy()

  
def first_print():
    text = "Hello World!"
    text_output = tk.Label(window, text=text, fg="red", font=("Helvetica", 16))
    text_output.grid(row=10, column=60, sticky="W")
    

first_button = tk.Button(text="Saluta!", command=first_print)
first_button.grid(row=10, column=5, sticky="W")



terzo_button = tk.Button(text = "Click and Quit", command = close_window)
terzo_button.grid(row=10, column=30, sticky="W")




def on_press(key):

   welcome_label1 = tk.Label(window,
                         text=key,
                         font=("Helvetica", 10))
   welcome_label1.grid(row=10, column=10, sticky="N", padx=20, pady=10)



with Listener(
        on_press=on_press) as the_listener:
      #  on_release=on_release) as listener:
      the_listener.join()

if __name__ == "__main__":
               window.mainloop()


se levo la funzione def on_press(key): e tolgo:

Codice:
with Listener(
        on_press=on_press) as the_listener:
      #  on_release=on_release) as listener:
      the_listener.join()
giustamente la finestra si crea

dove sbaglio?

mi sto riavvicinando alla programmazione con python, dall'ultimo post fatto, ci ho messo giorni a capire di intentare il code in python se no avevo errori azzz ):.

Stavo per lasciare perdere in quanto molto frustrato ma non voglio mollare.

Grazie per l'aiuto
steno12 è offline   Rispondi citando il messaggio o parte di esso