Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6986155
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T18:46:22+00:00 2026-05-27T18:46:22+00:00

I bound the event <Return> to a Button, thinking that this would cause the

  • 0

I bound the event <Return> to a Button, thinking that this would cause the command to be run after hitting Enter:

Button(self.f, text="Print", command=self.Printer).pack(side=RIGHT, padx=10, pady=10)
self.button1 = Button(self.f, text="search", command=self.search)
self.button1.bind('<Return>', self.search)
self.button1.pack(side=RIGHT, padx=10, pady=10)

But it doesn’t do anything. What must I do for self.search to be run when Enter is pressed?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T18:46:22+00:00Added an answer on May 27, 2026 at 6:46 pm

    Your code looks fine, but note that the focus must be on the button if you want Return to call self.search(). You can change the focus from widget to widget by pressing Tab. The widget in focus is outlined with a thin black line. You may have to press Tab one or more times to move the focus to the button before pressing Return.

    If you want Return to work anywhere in the GUI window, then change

    self.button1.bind('<Return>', self.search)
    

    to

    root.bind('<Return>', self.search)
    

    where root = tk.Tk().

    For example, compare button.bind with master.bind in the code below:

    import Tkinter as tk
    
    class SimpleApp(object):
        def __init__(self, master, **kwargs):
            title = kwargs.pop('title')
            frame = tk.Frame(master, **kwargs)
            frame.grid()
            button = tk.Button(frame, text = 'search', command = self.search)
            button.grid()
            button.bind('<Return>', self.search)
            # uncomment if you want `<Return>` bound everywhere.
            # master.bind('<Return>', self.search)  
        def search(self,*args):
            print('searching...')
    
    def basic():
        root = tk.Tk()
        app = SimpleApp(root, title = 'Hello, world')
        root.mainloop()
    
    basic()
    

    Alternatively, you could use

        button.bind('<Return>', self.search)
        button.focus()
    

    Doing it this way, pressing Return calls self.search() only when the button has the focus, but the button gets the focus when the app begins.


    Regarding the use of *args and **kwargs:

    **kwargs allows arbitrary keyword arguments to be passed to __init__.

    When **kwargs is used in a function definition like this:

    def __init__(self, master, **kwargs):
    

    and we instantiate SimpleApp like this:

    app = SimpleApp(root, title = 'Hello, world')
    

    then Python sets kwargs to a dict containing the keyword arguments, e.g. {'title':'Hello, world'}. Note that **kwargs is Python syntax which can only be used in function definitions and function calls (see below), but kwargs itself is just a dict.

    kwargs is then passed to Frame:

    frame = tk.Frame(master, **kwargs)
    

    Now, when **kwargs is used in a function call, the key-value pairs in the kwargs dict get expanded so that the above function call is equivalent to

    frame = tk.Frame(master, title = 'Hello, world')
    

    Since Frame can take numerous keyword arguments, and I don’t know them all and don’t wish to enumerate them, it is advantageous to use **kwargs.
    Note also that even if new keyword arguments were added to Frame at some later date, my code will still work, whereas if I spelled out the keywords explicitly then my code would not automatically “upgrade” if Frame‘s allowable keywords were to change.

    *args, similarly, allows you to include arbitrary positional arguments to search:

    def search(self,*args):
    

    Python sets args to a list containing all the positional arguments sent to search when search is called.

    I used *args here because self.search is called with no arguments or one argument.

    When you say

    button = tk.Button(frame, text = 'search', command = self.search)
    

    self.search() is called with no argumens when the button is clicked. But when you say

        button.bind('<Return>', self.search)
    

    self.search(event) is called with one argument when the Return key is pressed. event is a Tkinter.Event which has attributes (time, state, type, widget, x, y, etc) which allow you to learn more about what event took place.

    Another, perhaps better, way to define search would have been

    def search(self, event = None):
        ...
    

    This would have made it clear that search may be passed 0 or 1 arguments, and not simply and arbitary number of arguments. It also would have provided easier access to event if an event were passed to search.

    Reference:

    • saltycrane’s
      explanation
      .
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a gridview control that has on onclick event bound to every cell.
I have the following code bound to the click event of a button: function
I have a div that I have bound the jQuery hover event to activate
I have two types of links that are event-bound. If the clicked-on link has
Is it possible to create an event listener in jQuery that can be bound
I have an event handler bound to the hover event using the .hover method,
Javascript jQuery event handling If on event (for example 'click') bound one function for
How do I delimit a Javascript data-bound string parameter in an anchor OnClick event?
I'm duty-bound by policy to use CVS in this certain project, so even though
I have a simple video html5 tag, bound to an ended event: $('#video').show().trigger(play).bind('ended', function

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.