{"id":35273,"date":"2022-10-30T08:43:01","date_gmt":"2022-10-30T08:43:01","guid":{"rendered":"https:\/\/www.askpython.com\/?p=35273"},"modified":"2023-01-28T15:11:24","modified_gmt":"2023-01-28T15:11:24","slug":"bind-in-tkinter","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python-modules\/tkinter\/bind-in-tkinter","title":{"rendered":"Bind in Tkinter: A Complete Guide"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">In this article let&#8217;s try to understand the concept of bind used in Tkinter. Before starting with this topic, we must remember that Python\u2019s Tkinter package is used to design GUI-based interfaces. Tkinter has a variety of built-in functionalities and extensions that can be used to enhance the functionality and performance of the application.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What is bind?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The basic definition of the word bind is stick together or cause to stick together in a single mass. Similarly, Tkinter bind is used to connect an event passed in the widget along with the event handler. The event handler is the function that gets invoked when the events take place.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nwidget.bind(sequence=None, func=None, add=None)\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">The&nbsp;<code><em><code>sequence<\/code><\/em><\/code>&nbsp;the argument describes what event we expect, and the&nbsp;<em>func&nbsp;<\/em>argument is a function to be called when that event happens to the widget. If there was already a binding for that event for this widget, normally the old callback is replaced with <em>func<\/em>, but you can preserve both callbacks by passing&nbsp;<code>add='+'<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The events can be bonded to an event handler using the bind function at different levels.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">1. Instance-level binding<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">One can bind an event to one specific widget. To bind an event of a widget, call the&nbsp;<code>.bind()<\/code>&nbsp;method on that widget.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>widget.bind(event, event handler)<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>Event&nbsp;<\/strong>\u2013 occurrence caused by the user that might reflect changes.<\/li><li><strong>Event Handler<\/strong>&nbsp;\u2013 function in your application that gets invoked when the event takes place.<\/li><li><strong>Bind&nbsp;<\/strong>\u2013 configuring an event handler (python function) that is called when an event occurs to a widget.<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Following is an example of how to bind an event to a particular instance of a widget.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport tkinter as tk\nclass Display:\n    def __init__(self):\n        self.root = tk.Tk()\n        self.entry1 = tk.Entry(self.root)\n        self.entry1.bind(&quot;&lt;KeyPress&gt;&quot;, self.onKeyPress)\n        self.entry1.pack()\n        self.root.mainloop()\n\n    def onKeyPress(self, event):\n        print(&quot;Key has been Pressed.&quot;)\ndisplay = Display()\n\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">Output<\/h3>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"816\" height=\"563\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/10\/Screenshot-627.png\" alt=\"Implementation\" class=\"wp-image-35861\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/10\/Screenshot-627.png 816w, https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/10\/Screenshot-627-300x207.png 300w, https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/10\/Screenshot-627-768x530.png 768w\" sizes=\"auto, (max-width: 816px) 100vw, 816px\" \/><figcaption>Implementation<\/figcaption><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Multiple Binding<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">What if we need to bind more than one function to a particular widget? Passing two statements containing the<code> bind the <\/code>function will certainly not work because the second statement will override the first statement hence only the second function will be invoked as shown below<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"763\" height=\"552\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/10\/Screenshot-619.png\" alt=\"Implementation\" class=\"wp-image-35853\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/10\/Screenshot-619.png 763w, https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/10\/Screenshot-619-300x217.png 300w\" sizes=\"auto, (max-width: 763px) 100vw, 763px\" \/><figcaption>Implementation<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">This problem is solved by adding one extra parameter <code><strong>add<\/strong><\/code> to the second statement that uses the <code>bind<\/code> function. Also do not forget to assign add=&#8221;+&#8221;. This will invoke both functions. Hence we can enable multiple binding in Tkinter.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"765\" height=\"556\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/10\/Screenshot-620.png\" alt=\"Implementation\" class=\"wp-image-35854\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/10\/Screenshot-620.png 765w, https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/10\/Screenshot-620-300x218.png 300w\" sizes=\"auto, (max-width: 765px) 100vw, 765px\" \/><figcaption>Implementation<\/figcaption><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">2. Class-level binding<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">One can bind an event to all widgets of a class. For example, you might set up all&nbsp;<code>Button<\/code>&nbsp;widgets to respond to middle mouse button clicks by changing back and forth between English and Japanese labels. To bind an event to all widgets of a class, call the&nbsp;<code>.bind_class()<\/code>&nbsp;method on any widget. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The &#8220;class&#8221; mentioned in &nbsp;<code>bind_class<\/code>&nbsp;refers to the internal class name used by the tk library, not the python class name. <code>bind_class<\/code>&nbsp;is a method available to all widgets and simply calls the Tk bind command again, however not with the instance name, but the widget class name.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nw.bind_class(className, sequence=None, func=None, add=None)\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">The basic working of <code>.bind_class<\/code> is the same as the <code>.bind<\/code> function.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For example, suppose you have several widgets of the same class, here let&#8217;s consider we have multiple entry widgets and we need to set all of them to the same function. Rather than having to call&nbsp;<code>.bind()<\/code>&nbsp;for every one of them, you can set them all up with one call something like this:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nfrom tkinter import *\nimport tkinter as tk\n\nclass Display:\n    def __init__(self):\n        self.root = tk.Tk()\n        self.entry_username = tk.Entry(self.root)\n        self.entry_birthday = tk.Entry(self.root)\n        self.entry_password= tk.Entry(self.root)\n        self.entry_username.bind_class(&quot;Entry&quot;, &quot;&lt;Return&gt;&quot;, self.onReturn)\n\n        self.entry_username.pack()\n        self.entry_birthday.pack()\n        self.entry_password.pack()\n\n        #to know the class type of the widget use following command.\n        #print(self.entry_username.winfo_class())\n        self.root.mainloop()\n\n    def onReturn(self, event):\n        print(&quot;Return pressed&quot;)\ndisplay = Display()\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\">Output<\/h2>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1007\" height=\"577\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/10\/Screenshot-622.png\" alt=\"Implementation\" class=\"wp-image-35858\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/10\/Screenshot-622.png 1007w, https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/10\/Screenshot-622-300x172.png 300w, https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/10\/Screenshot-622-768x440.png 768w\" sizes=\"auto, (max-width: 1007px) 100vw, 1007px\" \/><figcaption>Implementation<\/figcaption><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">3. Application-level binding<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">One can set up a binding so that a certain event calls a handler no matter what widget has the focus or is under the mouse.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nw.bind_all(sequence=None,\u00a0 func=None,\u00a0 add=None)\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">Like&nbsp;<code>.bind()<\/code>, but applies to all widgets in the entire application.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For example, you might have multiple widgets of the same type throughout the program, they make be part of different classes. To bind an event at the application level, call the&nbsp;<code>.bind_all()<\/code>&nbsp;method on any widget.  While using .bind_all you do not need to mention the class name as the bind is applied on each and every event of the application. Here&#8217;s how to implement it,<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nfrom tkinter import *\nimport tkinter as tk\n\nclass Display:\n    def __init__(self):\n        self.root = tk.Tk()\n        self.entry_username = tk.Entry(self.root)\n        self.entry_birthday = tk.Entry(self.root)\n        self.entry_password = tk.Entry(self.root)\n        self.entry_username.bind_all(&quot;&lt;Return&gt;&quot;, self.onReturn)\n\n        self.entry_username.pack()\n        self.entry_birthday.pack()\n        self.entry_password.pack()\n\n        #to know the class type of the widget use following command.\n        print(self.entry_username.winfo_class())\n        self.root.mainloop()\n\n    def onReturn(self, event):\n        print(&quot;Return pressed&quot;)\ndisplay = Display()\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\">Output<\/h2>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"908\" height=\"579\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/10\/Screenshot-624.png\" alt=\"Implementation\" class=\"wp-image-35860\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/10\/Screenshot-624.png 908w, https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/10\/Screenshot-624-300x191.png 300w, https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/10\/Screenshot-624-768x490.png 768w\" sizes=\"auto, (max-width: 908px) 100vw, 908px\" \/><figcaption>Implementation<\/figcaption><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Summary<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">In this article, we have understood that Bind is the function used in Tkinter to join or associate an event with a particular function called event handler for a widget. Also, Tkinter supports three levels of binding they are: instance-level binding, class-level binding, and application-level binding. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Reference<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">https:\/\/web.archive.org\/web\/20190514010732id_\/http:\/\/infohost.nmt.edu\/tcc\/help\/pubs\/tkinter\/web\/index.html<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this article let&#8217;s try to understand the concept of bind used in Tkinter. Before starting with this topic, we must remember that Python\u2019s Tkinter package is used to design GUI-based interfaces. Tkinter has a variety of built-in functionalities and extensions that can be used to enhance the functionality and performance of the application. What [&hellip;]<\/p>\n","protected":false},"author":41,"featured_media":35741,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[92],"tags":[],"class_list":["post-35273","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tkinter"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/35273","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/users\/41"}],"replies":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/comments?post=35273"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/35273\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media\/35741"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=35273"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=35273"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=35273"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}