Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I bind the enter key to a function in tkinter?

I am a Python beginning self-learner, running on MacOS.

I'm making a program with a text parser GUI in tkinter, where you type a command in a Entry widget, and hit a Button widget, which triggers my parse() funct, ect, printing the results to a Text widget, text-adventure style.

> Circumvent the button

I can't let you do that, Dave.

I'm trying to find a way to get rid of the need to haul the mouse over to the Button every time the user issues a command, but this turned out harder than I thought.

I'm guessing the correct code looks like self.bind('<Return>', self.parse())? But I don't even know where to put it. root, __init__, parse(), and create_widgets() don't want it.

To be clear, the only reason anyone should hit enter in the prog is to trigger parse(), so it doesn't need to be espoused to the Entry widget specifically. Anywhere it works is fine.

In response to 7stud, the basic format:

from tkinter import * import tkinter.font, random, re  class Application(Frame):          def __init__(self, master):         Frame.__init__(self, master, ...)         self.grid()         self.create_widgets()         self.start()               def parse(self):         ...       def create_widgets(self):                  ...          self.submit = Button(self, text= "Submit Command.", command= self.parse, ...)         self.submit.grid(...)           root = Tk() root.bind('<Return>', self.parse)  app = Application(root)  root.mainloop() 
like image 953
Ghosty Avatar asked Jun 08 '13 05:06

Ghosty


People also ask

How do you bind in Tkinter?

In the below example we see how to bind the mouse click events on a tkinter window to a function call. In the below example we call the events to display the left-button double click, right button click and scroll-button click to display the position in the tkinter canvas where the buttons were clicked.


2 Answers

Try running the following program. You just have to be sure your window has the focus when you hit Return--to ensure that it does, first click the button a couple of times until you see some output, then without clicking anywhere else hit Return.

import tkinter as tk  root = tk.Tk() root.geometry("300x200")  def func(event):     print("You hit return.") root.bind('<Return>', func)  def onclick():     print("You clicked the button")  button = tk.Button(root, text="click me", command=onclick) button.pack()  root.mainloop() 

Then you just have tweak things a little when making both the button click and hitting Return call the same function--because the command function needs to be a function that takes no arguments, whereas the bind function needs to be a function that takes one argument(the event object):

import tkinter as tk  root = tk.Tk() root.geometry("300x200")  def func(event):     print("You hit return.")  def onclick(event=None):     print("You clicked the button")  root.bind('<Return>', onclick)  button = tk.Button(root, text="click me", command=onclick) button.pack()  root.mainloop() 

Or, you can just forgo using the button's command argument and instead use bind() to attach the onclick function to the button, which means the function needs to take one argument--just like with Return:

import tkinter as tk  root = tk.Tk() root.geometry("300x200")  def func(event):     print("You hit return.")  def onclick(event):     print("You clicked the button")  root.bind('<Return>', onclick)  button = tk.Button(root, text="click me") button.bind('<Button-1>', onclick) button.pack()  root.mainloop() 

Here it is in a class setting:

import tkinter as tk  class Application(tk.Frame):     def __init__(self):         self.root = tk.Tk()         self.root.geometry("300x200")          tk.Frame.__init__(self, self.root)         self.create_widgets()      def create_widgets(self):         self.root.bind('<Return>', self.parse)         self.grid()          self.submit = tk.Button(self, text="Submit")         self.submit.bind('<Button-1>', self.parse)         self.submit.grid()      def parse(self, event):         print("You clicked?")      def start(self):         self.root.mainloop()   Application().start() 
like image 74
7stud Avatar answered Sep 28 '22 19:09

7stud


Another alternative is to use a lambda:

ent.bind("<Return>", (lambda event: name_of_function())) 

Full code:

from tkinter import * from tkinter.messagebox import showinfo  def reply(name):     showinfo(title="Reply", message = "Hello %s!" % name)   top = Tk() top.title("Echo")  Label(top, text="Enter your name:").pack(side=TOP) ent = Entry(top) ent.bind("<Return>", (lambda event: reply(ent.get()))) ent.pack(side=TOP) btn = Button(top,text="Submit", command=(lambda: reply(ent.get()))) btn.pack(side=LEFT)  top.mainloop() 

As you can see, creating a lambda function with an unused variable "event" solves the problem.

like image 38
Seçkin Savaşçı Avatar answered Sep 28 '22 19:09

Seçkin Savaşçı