{"id":3925,"date":"2019-10-07T06:19:55","date_gmt":"2019-10-07T04:19:55","guid":{"rendered":"https:\/\/pythonprogramming.altervista.org\/?p=3925"},"modified":"2019-10-07T06:49:27","modified_gmt":"2019-10-07T04:49:27","slug":"tkinter-event-binding","status":"publish","type":"post","link":"https:\/\/pythonprogramming.altervista.org\/tkinter-event-binding\/","title":{"rendered":"Tkinter event binding: Enter aka <Return>"},"content":{"rendered":"<p>When you need to interact with the user in the window created with Python&#8217;s module tkinter, you need to use the bind method, if you are not using a button that has his own command method built in. Let&#8217;s explor the different keywords used in the method bind for the different event that the user may activate on a widget.<\/p>\n<h2>Enter event is &lt;Return&gt;<\/h2>\n<p>To call a function or a method when you press enter while a widget is focused, you will need to use the keyword &lt;Return&gt; into apostrophes, like in this example.<\/p>\n<p>After you imported tkinter like this:<\/p>\n<pre class=\"lang:default decode:true \">import tkinter as tk\r\n# for python 2: import Tkinter as tk<\/pre>\n<p>And you create a window<\/p>\n<pre class=\"lang:default decode:true \">root = tk.Tk()\r\n\r\nroot.mainloop()<\/pre>\n<p>Imagine you create an Entry widget like this (between root = tk.Tk() and root.mainloop():<\/p>\n<pre class=\"lang:default decode:true \">entry = tk.Entry(root)\r\nentry.pack()<\/pre>\n<p>To make something happen when you hit enter you write this and you&#8217;re done.<\/p>\n<pre class=\"lang:default decode:true \">self.entry.bind(\"&lt;Return&gt;\", print(\"Hello World\")<\/pre>\n<table border=1><td>event<\/td><td>keyword<\/td><tr><td>press Enter<\/td><td>&ltReturn><\/td><tr><\/table>\n","protected":false},"excerpt":{"rendered":"When you need to interact with the user in the window created with Python&#8217;s module tkinter, you need to use the bind method, \n<a class=\"moretag\" href=\"https:\/\/pythonprogramming.altervista.org\/tkinter-event-binding\/\"> [...]<\/a>","protected":false},"author":1,"featured_media":3927,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_crdt_document":"","footnotes":""},"categories":[1,122,49],"tags":[],"class_list":["post-3925","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-examples","category-gui","category-tkinter"],"avopt_banners_inside_post":true,"avopt_banners_on_page":true,"av_copy_from":"","av_sharing_message":"","av_sharing_allowed":false,"av_sharing_on":{"fb":[],"tw":[]},"av_allow_affiliate_banner":false,"av_allow_affiliate_multi_banner":false,"av_show_affiliation_buy_button":false,"av_post_rating":true,"av_have_post_rating_value":false,"av_is_artificial_intelligence_content":false,"_links":{"self":[{"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/posts\/3925","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/comments?post=3925"}],"version-history":[{"count":3,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/posts\/3925\/revisions"}],"predecessor-version":[{"id":3929,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/posts\/3925\/revisions\/3929"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/media\/3927"}],"wp:attachment":[{"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/media?parent=3925"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/categories?post=3925"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/tags?post=3925"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}