{"id":3117,"date":"2019-08-23T08:14:24","date_gmt":"2019-08-23T06:14:24","guid":{"rendered":"https:\/\/pythonprogramming.altervista.org\/?p=3117"},"modified":"2019-08-23T08:41:17","modified_gmt":"2019-08-23T06:41:17","slug":"tkinter-callback-or-copycat-function-trace-stringvar","status":"publish","type":"post","link":"https:\/\/pythonprogramming.altervista.org\/tkinter-callback-or-copycat-function-trace-stringvar\/","title":{"rendered":"Tkinter: callback or copycat function&#8230; ( trace StringVar )"},"content":{"rendered":"<p>After having shown some little &#8216;apps&#8217; that does something useful lately, we want to go back revisiting some code about tkinter widgets that do something useful but not for a practical goal, so that we can use these knowledge for other more complex projects. Today we are going to take a look at the trace method to &#8216;follow&#8217; what the user is writing in an entry widget.<\/p>\n<p><a href=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/08\/trace.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-3119\" src=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/08\/trace.png\" alt=\"\" width=\"126\" height=\"72\" \/><\/a><\/p>\n<p>To trace the value of your input could be useful sometimes. That is what this code does. There is a function called trace that will be deprecated. In this code you can get what the user are inserting in an entry widget and, as he types, a label will copy that value and show it immediately in its text.<\/p>\n<div style=\"width: 124px;\" class=\"wp-video\"><video class=\"wp-video-shortcode\" id=\"video-3117-1\" width=\"124\" height=\"40\" preload=\"metadata\" controls=\"controls\"><source type=\"video\/mp4\" src=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/08\/trace2.mp4?_=1\" \/><a href=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/08\/trace2.mp4\">https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/08\/trace2.mp4<\/a><\/video><\/div>\n<pre class=\"lang:default decode:true\">from tkinter import *\r\n\r\ndef copycat(sv, lb):\r\n    lb['text'] = sv.get()\r\n\r\ndef labentry():\r\n\tsv = StringVar() # Create the StringVar\r\n\tsv.trace(\"w\", lambda name, index, mode, sv=sv: copycat(sv, lb))\r\n\t# copycat is usually called callback\r\n\te = Entry(root, textvariable=sv)\r\n\tlb = Label(root, text = \"\")\r\n\tlb.pack()\r\n\te.pack()\r\n\te.focus()\r\n\r\nroot = Tk()\r\nlabentry() # =&gt; create label and entry\r\nroot.mainloop()<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"How to get the entry value each time that it changes, so that we can use that value into our scripts, for example when the user inputs a text to be checked or searched among a list\n<a class=\"moretag\" href=\"https:\/\/pythonprogramming.altervista.org\/tkinter-callback-or-copycat-function-trace-stringvar\/\"> [...]<\/a>","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_crdt_document":"","footnotes":""},"categories":[1],"tags":[],"class_list":["post-3117","post","type-post","status-publish","format-standard","hentry","category-examples"],"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\/3117","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=3117"}],"version-history":[{"count":5,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/posts\/3117\/revisions"}],"predecessor-version":[{"id":3127,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/posts\/3117\/revisions\/3127"}],"wp:attachment":[{"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/media?parent=3117"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/categories?post=3117"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/tags?post=3117"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}