排除将特定字符输入文本字段 Python guizero

Exclude specific character from being entered into text field Python guizero

我有一个使用 python guizero 的图形用户界面,它有多个文本字段用于将整数值插入数据库。当按下 space 栏时,将执行一些操作,如以下代码所示:

app = App(title="App", layout="auto", width=100%, height=100%)

def toggle(event_data):
    if(event_data.key == " "):
        print("space has been toggled")
        #perform some action

app.when_key_pressed = toggle

切换功能按预期工作,问题是当按下 space 栏时,space 被添加到不需要的文本字段。

有没有办法从文本字段的输入中排除 space 栏?

这将删除所有空格。

from guizero import*

app=App() 

input_box = TextBox(app)


def key_pressed(e):
    if e.key == "":#provents an errors
        return None
    elif ord(e.key) == 32:# key number is the ASCII code for the space bar
        #More key numbers
        #https://theasciicode.com.ar/
        print("space has been pressed")
        #Removes white space
        input_box.value=input_box.value.strip()

input_box.when_key_pressed = key_pressed                      
input_box.when_key_released = key_pressed      

app.display()