0

我有一个使用 python guizero 的 gui,它具有多个用于将整数值插入数据库的文本字段。当按下空格键时,会执行一些操作,如以下代码所示:

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

切换功能按预期工作,问题是当按下空格键时,文本字段中添加了一个不需要的空格。

有没有办法从输入到文本字段中排除空格键?

4

1 回答 1

1

这将删除所有空格。

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()
于 2021-10-15T18:25:48.863 回答