我有一个使用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切换功能按预期工作,问题是当按下空格键时,会在文本字段中添加一个不需要的空格。
有没有办法从文本字段的输入中排除空格键?
发布于 2021-10-15 18:25:49
这将删除所有空格。
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()https://stackoverflow.com/questions/69586589
复制相似问题