代码之家  ›  专栏  ›  技术社区  ›  Rahul

如何将enter键绑定到tkinter按钮

  •  8
  • Rahul  · 技术社区  · 7 年前

    我正试图约束 Enter键 用一个 按钮 .

    在下面的代码中,我试图从条目小部件中获取条目, 当按钮 bt 按下,它调用 enter() 获取条目的方法。

    我还希望通过按 Enter键 ,我没有得到预期的结果。

    输入小部件中输入的值未被读取,并且 进来 方法,只在我的数据库中插入一个空白

    帮我解决我的问题,谢谢!

    from tkinter import *
    import sqlite3
    
    conx = sqlite3.connect("database_word.db")
    
    def count_index():
        cur = conx.cursor()
        count = cur.execute("select count(word) from words;")
        rowcount = cur.fetchone()[0]
        return rowcount
    
    def enter():  #The method that I am calling from the Button
        x=e1.get()
        y=e2.get()
        ci=count_index()+1
        conx.execute("insert into words(id, word, meaning) values(?,?,?);",(ci,x,y))
        conx.commit()
    
    fr =Frame()  #Main
    bt=Button(fr)  #Button declaration
    fr.pack(expand=YES)
    l1=Label(fr, text="Enter word").grid(row=1,column=1)
    l2=Label(fr, text="Enter meaning").grid(row=2,column=1)
    e1=Entry(fr)
    e2=Entry(fr)
    e1.grid(row=1,column=2)
    e2.grid(row=2,column=2)
    e1.focus()
    e2.focus()
    bt.config(text="ENTER",command=enter)
    bt.grid(row=3,column=2)
    bt.bind('<Return>',enter)   #Using bind
    
    fr.mainloop()
    
    2 回复  |  直到 7 年前
        1
  •  11
  •   Novel    7 年前

    2件事:

    您需要修改函数以接受以下参数: bind 传球。(你不需要使用它,但你需要接受它)。

    def enter(event=None):
    

    您需要将函数而不是结果传递给 绑定 方法所以放下 () .

    bt.bind('<Return>',enter)
    

    注意,这将返回到 按钮 ,所以如果其他东西有焦点,那么这将不起作用。您可能希望将此绑定到 框架 .

    fr.bind('<Return>',enter)
    

    如果你想要一种按下对焦按钮的方法,空格键已经可以做到了。

        2
  •  9
  •   cbjeukendrup    5 年前

    在问题和小说的答案中,首先,按钮绑定到 enter 函数使用 bt.config(text="ENTER",command=enter) . 之后,该函数被绑定到回车键,但实际上,按钮和回车键仍然是相当独立的。

    对于这个问题,我假设这不是一个真正的问题,但是如果您经常想更改按钮的命令,那么每次都必须更改对回车键的绑定可能会很无聊。

    所以你可能会想:有没有办法给回车键 自动地 与按钮相同的命令?

    是的,有!

    Tkinter按钮具有 invoke() 方法调用按钮的命令并返回命令的返回值:

    import tkinter as TK
    
    def func():
        print('the function is called!')
    
    root = TK.Tk()
    button= TK.Button(root, text='call the function', command=func) # not func()!
    button.pack()
    button.invoke() # output: 'the function is called!'
    root.mainloop()
    

    我们可以简单地将此方法绑定到enter键:

    root.bind('<Return>', button.invoke) # again without '()'!
    

    但是等等,这会产生与之前相同的问题: button.invoke() 方法不接受 event 参数,但我们无法(轻松)更改该方法。解决方案是使用lambda:

    root.bind('<Return>', lambda event=None: button.invoke()) # This time *with* '()'!!!
    

    为了完整起见,我将给出以下示例:

    import tkinter as TK
    
    root = TK.Tk()
    button= TK.Button(root)
    button.pack()
    
    def func1():
        print('func1 is called!')
        button.config(text='call func2', command=func2)
    def func2():
        print('func2 is called!')
    
    button.config(text='call func1', command=func1)
    
    root.bind('<Return>', lambda event=None: button.invoke())
    root.mainloop()
    

    说明:

    • 第一次按enter键或单击按钮时,“调用func1!”将打印。
    • 第二次按enter键或单击按钮时,将调用“func2!”将打印。enter键应与按钮相同,尽管键的绑定没有更改。