How to deal with none letter keyboard input errors in python guizero/tkinter? How to deal with none letter keyboard input errors in python guizero/tkinter? tkinter tkinter

How to deal with none letter keyboard input errors in python guizero/tkinter?


Adding this at the beginning of key_pressed function will eliminate the error when Caps Lock/Shift or any key that returns an empty string is pressed.

if e.key=='':    return

The following has worked in preventing the Tab key from selecting the text

if ord(e.key) == 9: #Tab key    print('Tab pressed')    input_box.append(' '*4)    input_box.disable()    input_box.after(1,input_box.enable)

Basically I have disabled the widget followed by enabling it after 1 millisecond.

UPDATE

Another approach could be to bind the Tab key to the Entry widget used internally (which can be accessed by using the tk property as stated in the docs). I would recommend this approach over the previous, also, because the append method adds the text at the end, there is no built in method to insert the text at the current location, so you would ultimately end up using the insert method of tkinter.Entry with the index as 'insert'.

def select_none(event):    input_box.tk.insert('insert',' '*4)    return 'break'input_box.tk.bind('<Tab>',select_none)

Using return 'break' at the end of the function prevents other event handlers from being executed.