TkInter Binding Buttons - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: GUI (https://python-forum.io/forum-10.html) +--- Thread: TkInter Binding Buttons (/thread-25596.html) |
TkInter Binding Buttons - ifigazsi - Apr-04-2020 Hi! I wanted to get if a key(Enter/F1/BTN1) was pressed, but it is not working as i thought. :( So the desired outcome: import tkinter as tk def pressed_enter(): print('You pressed Enter') def pressed_F1(): print('You pressed F1') def pressed_btn1(): print('Mouse BTN1 was pressed') root = tk.Tk() root.geometry('200x250') root.bind("<Return>", pressed_enter()) root.bind("<KeyPress-F1>", pressed_F1()) root.bind("<Button-1>", pressed_btn1()) root.mainloop() (Apr-04-2020, 05:10 PM)ifigazsi Wrote: So the desired outcome: - Not to run at start - Run as many times, as a button was pressed. RE: TkInter Binding Buttons - deanhystad - Apr-04-2020 pressed_enter is a function. You can bind a function. pressed_enter() calls the function and is the function's return value (None). You cannot bind None. root.bind("<Return>", pressed_enter())This code works: import tkinter as tk def pressed_enter(event): print(event) def pressed_F1(event): print(event) def pressed_btn1(event): print(event) root = tk.Tk() root.geometry('200x250') root.bind("<Return>", pressed_enter) root.bind("<KeyPress-F1>", pressed_F1) root.bind("<Button-1>", pressed_btn1) root.mainloop() RE: TkInter Binding Buttons - ifigazsi - Apr-05-2020 (Apr-04-2020, 07:20 PM)deanhystad Wrote: pressed_enter is a function. You can bind a function. pressed_enter() calls the function and is the function's return value (None). You cannot bind None. Thanks, now it works! One more: Is it possible that tk.Button and Key binding use the same function? import tkinter as tk root = tk.Tk() root.geometry('250x100') ############# FUNCTIONS ###########x def check_in_database_with_btn(data, chk): if data in chk: print(f'{data} was found in database.') else: print(f'{data} no match in database.') def check_in_database_with_key(event, data, chk): check_in_database_with_btn(data, chk) ########### DATABASE ################## chk_database = ['Eric', 'John', 'Graham', 'Terry'] ############ ENTRY & BUTTON & BIND ###################### field_entry = tk.Entry(root, width=20) field_entry.grid(row=0, column=0) chk_in_database_btn = tk.Button(root, text='Check database', command=lambda: check_in_database_with_btn(field_entry.get(), chk_database)) chk_in_database_btn.grid(row=0, column=1) field_entry.bind("<Return>", lambda event: check_in_database_with_key(event, field_entry.get(), chk_database)) root.mainloop() RE: TkInter Binding Buttons - ifigazsi - Apr-05-2020 Ok, i got the answer: simply, with invoke() (Apr-05-2020, 10:47 AM)ifigazsi Wrote: Is it possible that tk.Button and Key binding use the same function? field_entry.bind("<Return>", lambda event: chk_in_database_btn.invoke()) RE: TkInter Binding Buttons - deanhystad - Apr-06-2020 Yes, you can bind both a key and a button to the same function. You were really close with the lambda. All you had to do was throw away the event which you don't care about. import tkinter as tk root = tk.Tk() root.geometry('250x100') def check_in_database(data, chk): if data in chk: print(f'{data} was found in database.') else: print(f'{data} no match in database.') chk_database = ['Eric', 'John', 'Graham', 'Terry'] field_entry = tk.Entry(root, width=20) field_entry.bind("<Return>", lambda event: check_in_database(field_entry.get(), chk_database)) field_entry.grid(row=0, column=0) tk.Button(root, text='Check database', command=lambda: check_in_database(field_entry.get(), chk_database)) \ .grid(row=0, column=1) root.mainloop()Another useful tool is functools.partial RE: TkInter Binding Buttons - ifigazsi - Apr-06-2020 (Apr-06-2020, 04:36 AM)deanhystad Wrote: Yes, you can bind both a key and a button to the same function. You were really close with the lambda. All you had to do was throw away the event which you don't care about. (Apr-06-2020, 04:36 AM)deanhystad Wrote: Yes, you can bind both a key and a button to the same function. Thank you! :D |