Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hide typed characters in tkinter.text widget

I am trying to write little application similar to hyperTerminal: Each character typed in Text widget is sent on serial port and each character received on the serial port is displayed in the Text widget. The problem is the equipment connected to serial port returns an echo of characters it receives. So I get 2 characters in the Text widget (the typed and the echoed)

I tried to delete every typed characters, but I'm looking for a better solution.

def clavier(event):
    global sp
    edit.delete("insert-1c") # delete typed character
    try:
        sp.write(event.char.encode('Latin-1'))
    except:
        edit.insert(INSERT,'\nPort fermé, choisissez un port\n', 'rouge')

edit = Text(cadre, width=50,height=20,yscrollcommand = Vscroll.set)
edit.bind("<KeyRelease>", clavier)

Is there any way to hide the typed characters?

like image 861
A. Oumnad Avatar asked Dec 18 '25 18:12

A. Oumnad


1 Answers

I'm not completely sure of how you are using the Text widget with the serial port, but it seems that you want to override the default action when you press the key. You can accomplish it by returning the string "break" in the handler function:

edit.bind("<KeyPress>", lambda e: "break")

I've tried it on Python 2.7 (with Tkinter 8.1) and I suppose it should also work on your versions, but if it does not, let me know.

like image 71
A. Rodas Avatar answered Dec 20 '25 09:12

A. Rodas



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!