|
|||||||
|
|
|
![]() |
|
|
Strumenti |
|
|
#1 |
|
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()
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 |
|
|
|
|
|
#2 |
|
Senior Member
Iscritto dal: May 2005
Messaggi: 8699
|
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: Codice:
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()
__________________
. Thread Ufficiali: EasyCrypt ~ Old Files Manager ~ OSD Clock The real me is no match for the legend . |
|
|
|
|
|
#3 |
|
Junior Member
Iscritto dal: Sep 2023
Messaggi: 6
|
Azzz...
si certo studiare sul web ma ci sono cose nel flusso che possono bloccarti. però devo capire: avevo scritto: Codice:
if __name__ == "__main__":
window.mainloop()
Codice:
window.mainloop()
if __name__ == "__main__":
main()
window.mainloop() ? infatti se la tolgo sono daccapo |
|
|
|
|
| Strumenti | |
|
|
Tutti gli orari sono GMT +1. Ora sono le: 07:31.



















