【问题标题】:Exclude specific character from being entered into text field Python guizero排除特定字符输入到文本字段Python guizero
【发布时间】:2021-12-03 18:26:52
【问题描述】:

我有一个使用 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

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

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

【问题讨论】:

    标签: python user-input guizero


    【解决方案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()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-04-11
      • 2021-11-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-09
      相关资源
      最近更新 更多