PDA

View Full Version : [Python] All'avvio non visualizza la finestra


steno12
28-09-2023, 09:29
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...




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:


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

x_Master_x
28-09-2023, 12:38
Studia da internet, prendi un libro python. Nessuno dice che devi mollare, ma mi dai l'impressione di andare a "sentimento"

Ecco un esempio funzionante:

import tkinter as tk
from pynput.keyboard import Key, Listener

def close_window():
window.destroy()

def first_print():
global entered_text
text = "Hello World!"
entered_text = text
update_text_output()

def on_press(key):
global entered_text
if key == Key.space:
entered_text += " "
elif hasattr(key, 'char'):
entered_text += key.char
update_text_output()

def update_text_output():
text_output.config(text=entered_text)

def main():
global window, entered_text, text_output

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

entered_text = ""

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")

text_output = tk.Label(window, text="", fg="red", font=("Helvetica", 16))
text_output.grid(row=10, column=60, sticky="W")

with Listener(on_press=on_press) as the_listener:
window.mainloop()

if __name__ == "__main__":
main()

steno12
28-09-2023, 17:14
Azzz... ;) spettacolo! funzionante Grazie x_Master_x

si certo studiare sul web ma ci sono cose nel flusso che possono bloccarti.

perņ devo capire:

avevo scritto:

if __name__ == "__main__":

window.mainloop()


mentre tu scrivi:


window.mainloop()

if __name__ == "__main__":
main()


main() riporta al main questo lo capisco ma non capisco dove porta
window.mainloop() ?

infatti se la tolgo sono daccapo ;)