{"id":1639,"date":"2020-12-11T10:19:21","date_gmt":"2020-12-11T10:19:21","guid":{"rendered":"https:\/\/www.pythontutorial.net\/?page_id=1639"},"modified":"2025-04-03T06:09:42","modified_gmt":"2025-04-03T06:09:42","slug":"tkinter-event-binding","status":"publish","type":"page","link":"https:\/\/www.pythontutorial.net\/tkinter\/tkinter-event-binding\/","title":{"rendered":"Tkinter Event Binding"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you&#8217;ll learn about the Tkinter event binding mechanism.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='introduction-to-the-tkinter-event-binding'>Introduction to the Tkinter event binding <a href=\"#introduction-to-the-tkinter-event-binding\" class=\"anchor\" id=\"introduction-to-the-tkinter-event-binding\" title=\"Anchor for Introduction to the Tkinter event binding\">#<\/a><\/h2>\n\n\n\n<p>Assigning a <a href=\"https:\/\/www.pythontutorial.net\/python-basics\/python-functions\/\">function<\/a> to an event of a widget is known as <strong>event binding<\/strong>. The assigned function will be invoked automatically when the event occurs.<\/p>\n\n\n\n<p>In the <a href=\"https:\/\/www.pythontutorial.net\/tkinter\/tkinter-command\/\">previous tutorial<\/a>, you learned how to bind a function to an event of a widget via the <code><a href=\"https:\/\/www.pythontutorial.net\/tkinter\/tkinter-command\/\">command<\/a><\/code> option. However, it&#8217;s important to note that not all <a href=\"https:\/\/www.pythontutorial.net\/tkinter\/tkinter-ttk\/\">Tkinter widgets<\/a> support the <code>command<\/code> option.<\/p>\n\n\n\n<p>Therefore, Tkinter provides you with an alternative way for event binding via the <code>bind()<\/code> method. <\/p>\n\n\n\n<p>The following shows the general syntax of the <code>bind()<\/code> method:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">widget.bind(event, handler, add=<span class=\"hljs-literal\">None<\/span>)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>When an <code>event<\/code> occurs in the <code>widget<\/code>, Tkinter will automatically invoke the <code>handler<\/code> with the event details.<\/p>\n\n\n\n<p>If you want to register an additional handler, you can pass the <code>'+'<\/code> to the <code>add<\/code> argument. This allows you to have multiple event handlers responding to the same event.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='tkinter-event-binding-examples'>Tkinter event binding examples <a href=\"#tkinter-event-binding-examples\" class=\"anchor\" id=\"tkinter-event-binding-examples\" title=\"Anchor for Tkinter event binding examples\">#<\/a><\/h2>\n\n\n\n<p>The following program illustrates how to bind the <code>return_pressed<\/code> function to the <code>Return<\/code> key pressed event of the <code>'Save'<\/code> button:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">import<\/span> tkinter <span class=\"hljs-keyword\">as<\/span> tk\n<span class=\"hljs-keyword\">from<\/span> tkinter <span class=\"hljs-keyword\">import<\/span> ttk\n\n\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">return_pressed<\/span><span class=\"hljs-params\">(event)<\/span>:<\/span>\n    print(<span class=\"hljs-string\">'Return key pressed.'<\/span>)\n\n\nroot = tk.Tk()\n\nbtn = ttk.Button(root, text=<span class=\"hljs-string\">'Save'<\/span>)\nbtn.bind(<span class=\"hljs-string\">'&lt;Return&gt;'<\/span>, return_pressed)\n\n\nbtn.focus()\nbtn.pack(expand=<span class=\"hljs-literal\">True<\/span>)\n\nroot.mainloop()<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In this example, the following statement calls the <code>bind()<\/code> method on the button widget to bind the <code>Return<\/code> key pressed event:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">btn.bind(<span class=\"hljs-string\">'&lt;Return&gt;'<\/span>, return_pressed)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The following example illustrates how to use the <code>bind()<\/code> method to register multiple handlers for the same event:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">import<\/span> tkinter <span class=\"hljs-keyword\">as<\/span> tk\n<span class=\"hljs-keyword\">from<\/span> tkinter <span class=\"hljs-keyword\">import<\/span> ttk\n\n\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">return_pressed<\/span><span class=\"hljs-params\">(event)<\/span>:<\/span>\n    print(<span class=\"hljs-string\">'Return key pressed.'<\/span>)\n\n\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">log<\/span><span class=\"hljs-params\">(event)<\/span>:<\/span>\n    print(event)\n\n\nroot = tk.Tk()\n\nbtn = ttk.Button(root, text=<span class=\"hljs-string\">'Save'<\/span>)\nbtn.bind(<span class=\"hljs-string\">'&lt;Return&gt;'<\/span>, return_pressed)\nbtn.bind(<span class=\"hljs-string\">'&lt;Return&gt;'<\/span>, log, add=<span class=\"hljs-string\">'+'<\/span>)\n\n\nbtn.focus()\nbtn.pack(expand=<span class=\"hljs-literal\">True<\/span>)\n\nroot.mainloop()<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>When you move the focus to the button and press the <code>Return<\/code> key, Tkinter automatically invokes the <code>return_pressed<\/code> and <code>log<\/code> functions.<\/p>\n\n\n\n<p>The following binds the <code>log()<\/code> function to the <code>Return<\/code> key pressed event of the <code>'Save'<\/code> button:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">btn.bind(<span class=\"hljs-string\">'&lt;Return&gt;'<\/span>, log, add=<span class=\"hljs-string\">'+'<\/span>)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In this statement, the third argument <code>add='+'<\/code> registered additional handler, which is the <code>log()<\/code> function. <\/p>\n\n\n\n<p>If you don&#8217;t specify the <code>add='+'<\/code> argument, the <code>bind()<\/code> method will replace the existing handler (<code>return_pressed<\/code>) by the new one (<code>log<\/code>).<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='event-patterns'>Event patterns <a href=\"#event-patterns\" class=\"anchor\" id=\"event-patterns\" title=\"Anchor for Event patterns\">#<\/a><\/h2>\n\n\n\n<p>Tkinter uses event patterns to map event names with handlers. For example, the <code>&lt;Return&gt;<\/code> denotes the Return key pressed.<\/p>\n\n\n\n<p>The following shows the general syntax of an event pattern:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">&lt;modifier-type-detail&gt;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In this syntax, an event is surrounded by angle brackets (<code>&lt;&gt;<\/code>). Inside the angle brackets, there are zero or more modifiers, an event type, and detailed information about the event. <\/p>\n\n\n\n<p>For example, the <code>&lt;KeyPress-A&gt;<\/code> denotes a keyboard press of the key <code>A<\/code> . and <code>&lt;Alt-Control-KeyPress-KP_Delete&gt;<\/code> represents a keypress of <code>Alt + Ctrl + Delete<\/code>.<\/p>\n\n\n\n<p>The following section shows the most commonly used event modifiers, event types, and event details.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id='event-modifiers'>Event modifiers <a href=\"#event-modifiers\" class=\"anchor\" id=\"event-modifiers\" title=\"Anchor for Event modifiers\">#<\/a><\/h3>\n\n\n\n<p>The following table lists the most commonly used event modifiers:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Event Modifier<\/th><th>Meaning<\/th><\/tr><\/thead><tbody><tr><td>Alt<\/td><td>The Alt key is held<\/td><\/tr><tr><td>Control<\/td><td>The Ctrl key is held<\/td><\/tr><tr><td>Shift<\/td><td>The Shift key is held<\/td><\/tr><tr><td>Any<\/td><td>This modifier makes an event type general. For example, the event pattern <code>&lt;Any-KeyPress&gt;<\/code> applies to the keypress of any key.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\" id='event-types'>Event types <a href=\"#event-types\" class=\"anchor\" id=\"event-types\" title=\"Anchor for Event types\">#<\/a><\/h3>\n\n\n\n<p>The following table shows the most commonly used event types:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Type<\/th><th>Name<\/th><th>Description<\/th><\/tr><\/thead><tbody><tr><td>36<\/td><td><code>Activate<\/code><\/td><td>The state option of a widget changes from inactive to active.<\/td><\/tr><tr><td>4<\/td><td><code>Button<\/code><\/td><td>One mouse button is pressed<\/td><\/tr><tr><td>5<\/td><td><code>ButtonRelease<\/code><\/td><td>One mouse button is released<\/td><\/tr><tr><td>22<\/td><td><code>Configure<\/code><\/td><td>The size of the widget is changed<\/td><\/tr><tr><td>37<\/td><td><code>Deactivate<\/code><\/td><td>The state option of a widget changes from active to inactive.<\/td><\/tr><tr><td>17<\/td><td><code>Destroy<\/code><\/td><td>A widget is being destroyed.<\/td><\/tr><tr><td>7<\/td><td><code>Enter<\/code><\/td><td>The mouse pointer is moved into a visible part of a widget.<\/td><\/tr><tr><td>12<\/td><td><code>Expose<\/code><\/td><td>Some part of the widget or application is visible after having been covered up by another window.<\/td><\/tr><tr><td>9<\/td><td><code>FocusIn<\/code><\/td><td>The input focus was moved into a widget.<\/td><\/tr><tr><td>10<\/td><td><code>FocusOut<\/code><\/td><td>The input focus was moved out of a widget.<\/td><\/tr><tr><td>2<\/td><td><code>KeyPress<\/code><\/td><td>A key is pressed.<\/td><\/tr><tr><td>3<\/td><td><code>KeyRelease<\/code><\/td><td>A key is released<\/td><\/tr><tr><td>8<\/td><td><code>Leave<\/code><\/td><td>The mouse pointer is moved out of a widget.<\/td><\/tr><tr><td>19<\/td><td><code>Map<\/code><\/td><td>A widget is being placed on a container e.g., calling the pack() or grid() method.<\/td><\/tr><tr><td>6<\/td><td><code>Motion<\/code><\/td><td>The mouse pointer is moved entirely within a widget.<\/td><\/tr><tr><td>38<\/td><td><code>MouseWheel<\/code><\/td><td>The user moved the mouse wheel up or down.<\/td><\/tr><tr><td>18<\/td><td><code>Unmap<\/code><\/td><td>A widget is being unmapped and is no longer visible, for example when calling the <code>grid_remove()<\/code>&nbsp;method on the widget.<\/td><\/tr><tr><td>15<\/td><td><code>Visibility<\/code><\/td><td>At least some part of the application window becomes visible on the screen.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\" id='event-detail'>Event Detail <a href=\"#event-detail\" class=\"anchor\" id=\"event-detail\" title=\"Anchor for Event Detail\">#<\/a><\/h3>\n\n\n\n<p>The following table shows several ways to name keys:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th><code>.keysym<\/code><\/th><th><code>.keycode<\/code><\/th><th><code>.keysym_num<\/code><\/th><th>Key<\/th><\/tr><\/thead><tbody><tr><td><code>Alt_L<\/code><\/td><td><code>64<\/code><\/td><td><code>65513<\/code><\/td><td>The left-hand&nbsp;alt&nbsp;key<\/td><\/tr><tr><td><code>Alt_R<\/code><\/td><td><code>113<\/code><\/td><td><code>65514<\/code><\/td><td>The right-hand&nbsp;alt&nbsp;key<\/td><\/tr><tr><td><code>BackSpace<\/code><\/td><td><code>22<\/code><\/td><td><code>65288<\/code><\/td><td>backspace<\/td><\/tr><tr><td><code>Cancel<\/code><\/td><td><code>110<\/code><\/td><td><code>65387<\/code><\/td><td>break<\/td><\/tr><tr><td><code>Caps_Lock<\/code><\/td><td><code>66<\/code><\/td><td><code>65549<\/code><\/td><td>CapsLock<\/td><\/tr><tr><td><code>Control_L<\/code><\/td><td><code>37<\/code><\/td><td><code>65507<\/code><\/td><td>The left-hand&nbsp;control&nbsp;key<\/td><\/tr><tr><td><code>Control_R<\/code><\/td><td><code>109<\/code><\/td><td><code>65508<\/code><\/td><td>The right-hand&nbsp;control&nbsp;key<\/td><\/tr><tr><td><code>Delete<\/code><\/td><td><code>107<\/code><\/td><td><code>65535<\/code><\/td><td>Delete<\/td><\/tr><tr><td><code>Down<\/code><\/td><td><code>104<\/code><\/td><td><code>65364<\/code><\/td><td>\u2193<\/td><\/tr><tr><td><code>End<\/code><\/td><td><code>103<\/code><\/td><td><code>65367<\/code><\/td><td>end<\/td><\/tr><tr><td><code>Escape<\/code><\/td><td><code>9<\/code><\/td><td><code>65307<\/code><\/td><td>esc<\/td><\/tr><tr><td><code>Execute<\/code><\/td><td><code>111<\/code><\/td><td><code>65378<\/code><\/td><td>SysReq<\/td><\/tr><tr><td><code>F1<\/code><\/td><td><code>67<\/code><\/td><td><code>65470<\/code><\/td><td>Function key&nbsp;F1<\/td><\/tr><tr><td><code>F2<\/code><\/td><td><code>68<\/code><\/td><td><code>65471<\/code><\/td><td>Function key&nbsp;F2<\/td><\/tr><tr><td><code>F<sub>i<\/sub><\/code><\/td><td><code>66+i<\/code><\/td><td><code>65469+i<\/code><\/td><td>Function key&nbsp;F<sub>i<\/sub><\/td><\/tr><tr><td><code>F12<\/code><\/td><td><code>96<\/code><\/td><td><code>65481<\/code><\/td><td>Function key&nbsp;F12<\/td><\/tr><tr><td><code>Home<\/code><\/td><td><code>97<\/code><\/td><td><code>65360<\/code><\/td><td>home<\/td><\/tr><tr><td><code>Insert<\/code><\/td><td><code>106<\/code><\/td><td><code>65379<\/code><\/td><td>insert<\/td><\/tr><tr><td><code>Left<\/code><\/td><td><code>100<\/code><\/td><td><code>65361<\/code><\/td><td>\u2190<\/td><\/tr><tr><td><code>Linefeed<\/code><\/td><td><code>54<\/code><\/td><td><code>106<\/code><\/td><td>Linefeed (control-J)<\/td><\/tr><tr><td><code>KP_0<\/code><\/td><td><code>90<\/code><\/td><td><code>65438<\/code><\/td><td>0 on the keypad<\/td><\/tr><tr><td><code>KP_1<\/code><\/td><td><code>87<\/code><\/td><td><code>65436<\/code><\/td><td>1 on the keypad<\/td><\/tr><tr><td><code>KP_2<\/code><\/td><td><code>88<\/code><\/td><td><code>65433<\/code><\/td><td>2 on the keypad<\/td><\/tr><tr><td><code>KP_3<\/code><\/td><td><code>89<\/code><\/td><td><code>65435<\/code><\/td><td>3 on the keypad<\/td><\/tr><tr><td><code>KP_4<\/code><\/td><td><code>83<\/code><\/td><td><code>65430<\/code><\/td><td>4 on the keypad<\/td><\/tr><tr><td><code>KP_5<\/code><\/td><td><code>84<\/code><\/td><td><code>65437<\/code><\/td><td>5 on the keypad<\/td><\/tr><tr><td><code>KP_6<\/code><\/td><td><code>85<\/code><\/td><td><code>65432<\/code><\/td><td>6 on the keypad<\/td><\/tr><tr><td><code>KP_7<\/code><\/td><td><code>79<\/code><\/td><td><code>65429<\/code><\/td><td>7 on the keypad<\/td><\/tr><tr><td><code>KP_8<\/code><\/td><td><code>80<\/code><\/td><td><code>65431<\/code><\/td><td>8 on the keypad<\/td><\/tr><tr><td><code>KP_9<\/code><\/td><td><code>81<\/code><\/td><td><code>65434<\/code><\/td><td>9 on the keypad<\/td><\/tr><tr><td><code>KP_Add<\/code><\/td><td><code>86<\/code><\/td><td><code>65451<\/code><\/td><td>+ on the keypad<\/td><\/tr><tr><td><code>KP_Begin<\/code><\/td><td><code>84<\/code><\/td><td><code>65437<\/code><\/td><td>The center key (same key as 5) on the keypad<\/td><\/tr><tr><td><code>KP_Decimal<\/code><\/td><td><code>91<\/code><\/td><td><code>65439<\/code><\/td><td>Decimal (<code>.<\/code>) on the keypad<\/td><\/tr><tr><td><code>KP_Delete<\/code><\/td><td><code>91<\/code><\/td><td><code>65439<\/code><\/td><td>delete&nbsp;on the keypad<\/td><\/tr><tr><td><code>KP_Divide<\/code><\/td><td><code>112<\/code><\/td><td><code>65455<\/code><\/td><td><code>\/<\/code>&nbsp;on the keypad<\/td><\/tr><tr><td><code>KP_Down<\/code><\/td><td><code>88<\/code><\/td><td><code>65433<\/code><\/td><td>\u2193 on the keypad<\/td><\/tr><tr><td><code>KP_End<\/code><\/td><td><code>87<\/code><\/td><td><code>65436<\/code><\/td><td>end&nbsp;on the keypad<\/td><\/tr><tr><td><code>KP_Enter<\/code><\/td><td><code>108<\/code><\/td><td><code>65421<\/code><\/td><td>enter&nbsp;on the keypad<\/td><\/tr><tr><td><code>KP_Home<\/code><\/td><td><code>79<\/code><\/td><td><code>65429<\/code><\/td><td>home&nbsp;on the keypad<\/td><\/tr><tr><td><code>KP_Insert<\/code><\/td><td><code>90<\/code><\/td><td><code>65438<\/code><\/td><td>insert&nbsp;on the keypad<\/td><\/tr><tr><td><code>KP_Left<\/code><\/td><td><code>83<\/code><\/td><td><code>65430<\/code><\/td><td>\u2190 on the keypad<\/td><\/tr><tr><td><code>KP_Multiply<\/code><\/td><td><code>63<\/code><\/td><td><code>65450<\/code><\/td><td>\u00d7 on the keypad<\/td><\/tr><tr><td><code>KP_Next<\/code><\/td><td><code>89<\/code><\/td><td><code>65435<\/code><\/td><td>PageDown&nbsp;on the keypad<\/td><\/tr><tr><td><code>KP_Prior<\/code><\/td><td><code>81<\/code><\/td><td><code>65434<\/code><\/td><td>PageUp&nbsp;on the keypad<\/td><\/tr><tr><td><code>KP_Right<\/code><\/td><td><code>85<\/code><\/td><td><code>65432<\/code><\/td><td>\u2192 on the keypad<\/td><\/tr><tr><td><code>KP_Subtract<\/code><\/td><td><code>82<\/code><\/td><td><code>65453<\/code><\/td><td><code>-<\/code>&nbsp;on the keypad<\/td><\/tr><tr><td><code>KP_Up<\/code><\/td><td><code>80<\/code><\/td><td><code>65431<\/code><\/td><td>\u2191 on the keypad<\/td><\/tr><tr><td><code>Next<\/code><\/td><td><code>105<\/code><\/td><td><code>65366<\/code><\/td><td>PageDown<\/td><\/tr><tr><td><code>Num_Lock<\/code><\/td><td><code>77<\/code><\/td><td><code>65407<\/code><\/td><td>NumLock<\/td><\/tr><tr><td><code>Pause<\/code><\/td><td><code>110<\/code><\/td><td><code>65299<\/code><\/td><td>pause<\/td><\/tr><tr><td><code>Print<\/code><\/td><td><code>111<\/code><\/td><td><code>65377<\/code><\/td><td>PrintScrn<\/td><\/tr><tr><td><code>Prior<\/code><\/td><td><code>99<\/code><\/td><td><code>65365<\/code><\/td><td>PageUp<\/td><\/tr><tr><td><code>Return<\/code><\/td><td><code>36<\/code><\/td><td><code>65293<\/code><\/td><td>Enter&nbsp;key<\/td><\/tr><tr><td><code>Right<\/code><\/td><td><code>102<\/code><\/td><td><code>65363<\/code><\/td><td>\u2192<\/td><\/tr><tr><td><code>Scroll_Lock<\/code><\/td><td><code>78<\/code><\/td><td><code>65300<\/code><\/td><td>ScrollLock<\/td><\/tr><tr><td><code>Shift_L<\/code><\/td><td><code>50<\/code><\/td><td><code>65505<\/code><\/td><td>The left-hand&nbsp;shift&nbsp;key<\/td><\/tr><tr><td><code>Shift_R<\/code><\/td><td><code>62<\/code><\/td><td><code>65506<\/code><\/td><td>The right-hand&nbsp;shift&nbsp;key<\/td><\/tr><tr><td><code>Tab<\/code><\/td><td><code>23<\/code><\/td><td><code>65289<\/code><\/td><td>The&nbsp;tab&nbsp;key<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id='binding-events-to-root-window'>Binding events to root window <a href=\"#binding-events-to-root-window\" class=\"anchor\" id=\"binding-events-to-root-window\" title=\"Anchor for Binding events to root window\">#<\/a><\/h2>\n\n\n\n<p>So far, you have learned how to bind an event to a particular widget. Tkinter also allows you to bind an event to the top-level window. <\/p>\n\n\n\n<p>In this case, the syntax for the <code>bind()<\/code> is the same except that you can call it on the root window like this:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">root.bind(<span class=\"hljs-string\">'&lt;Return&gt;'<\/span>, handler)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id='the-levels-of-binding'>The levels of binding <a href=\"#the-levels-of-binding\" class=\"anchor\" id=\"the-levels-of-binding\" title=\"Anchor for The levels of binding\">#<\/a><\/h2>\n\n\n\n<p>In the previous example, you have learned how to bind an event to a particular instance of a widget. This is an <strong>instance-level binding<\/strong>.<\/p>\n\n\n\n<p>Tkinter also allows you to bind an event to all the instances of a widget. For example, you can bind the event to all the <code>Entry<\/code> widgets in a program:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">root.bind_class(<span class=\"hljs-string\">'Entry'<\/span>, <span class=\"hljs-string\">'&lt;Control-V&gt;'<\/span>, paste)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>By the way, you use the <code><a href=\"https:\/\/www.pythontutorial.net\/tkinter\/tkinter-entry\/\">Entry<\/a><\/code> widget to create a textbox in Tkinter.<\/p>\n\n\n\n<p>This is a <strong>class-level binding<\/strong> because you bind the event to a class instead of an instance.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='unbinding-events'>Unbinding events <a href=\"#unbinding-events\" class=\"anchor\" id=\"unbinding-events\" title=\"Anchor for Unbinding events\">#<\/a><\/h2>\n\n\n\n<p>Sometimes, you may want to undo the effect of an earlier binding. To do it, you can use the <code>unbind()<\/code> method:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">widget.unbind(event)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The following example unbinds the event from the <code>btn<\/code> button:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-10\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">btn.unbind(<span class=\"hljs-string\">'&lt;Return&gt;'<\/span>)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-10\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id='summary'>Summary <a href=\"#summary\" class=\"anchor\" id=\"summary\" title=\"Anchor for Summary\">#<\/a><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Use the <code>bind()<\/code> method to bind an event to a widget.<\/li>\n\n\n\n<li>Tkinter supports both instance-level and class-level bindings.<\/li>\n<\/ul>\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Was this tutorial helpful ?<\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"1639\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/tkinter\/tkinter-event-binding\/\"\n\t\t\t\tdata-post-title=\"Tkinter Event Binding\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"1639\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/tkinter\/tkinter-event-binding\/\"\n\t\t\t\tdata-post-title=\"Tkinter Event Binding\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\t\t\t<input type=\"button\" name=\"wth-submit\" class=\"wth-btn wth-btn-submit\" id=\"wth-submit\" \/>\n\t\t\t<input type=\"button\" class=\"wth-btn wth-btn-cancel\" value=\"Cancel\" \/>\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, you&#8217;ll learn about the Tkinter event binding and how to use it to associate a function to an event of a widget.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":1232,"menu_order":5,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-1639","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/1639","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/comments?post=1639"}],"version-history":[{"count":4,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/1639\/revisions"}],"predecessor-version":[{"id":7406,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/1639\/revisions\/7406"}],"up":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/1232"}],"wp:attachment":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/media?parent=1639"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}