{"id":2062,"date":"2021-01-04T09:50:20","date_gmt":"2021-01-04T09:50:20","guid":{"rendered":"https:\/\/www.pythontutorial.net\/?page_id=2062"},"modified":"2021-01-05T01:52:26","modified_gmt":"2021-01-05T01:52:26","slug":"ttk-style-map","status":"publish","type":"page","link":"https:\/\/www.pythontutorial.net\/tkinter\/ttk-style-map\/","title":{"rendered":"How to Change the Appearances of Widgets Dynamically Using Ttk Style map() Method"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you&#8217;ll learn how to use the ttk Style <code>map()<\/code> method to dynamically change the appearance of a widget based on its specific state.<\/p>\n\n\n\n<p>Typically, a <a href=\"https:\/\/www.pythontutorial.net\/tkinter\/tkinter-ttk\/\">ttk widget<\/a> allows you to change its appearance based on a specific state.<\/p>\n\n\n\n<p>The following table shows a list of widget states and their meanings:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>State<\/th><th>Meaning<\/th><\/tr><\/thead><tbody><tr><td><code>active<\/code><\/td><td>The mouse is currently within the widget.<\/td><\/tr><tr><td><code>alternate<\/code><\/td><td>Ttk reserved this state for application use.<\/td><\/tr><tr><td><code>background<\/code><\/td><td>The widget is on a window that is not the foreground window. The foreground window is a window that is getting user inputs. This state is only relevant to Windows and macOS.<\/td><\/tr><tr><td><code>disabled<\/code><\/td><td>The widget won&#8217;t respond to any actions.<\/td><\/tr><tr><td><code>focus<\/code><\/td><td>The widget currently has&nbsp;focus.<\/td><\/tr><tr><td><code>invalid<\/code><\/td><td>The value of the widget is currently invalid.<\/td><\/tr><tr><td><code>pressed<\/code><\/td><td>The widget is currently being clicked or pressed e.g. when a Button widget is pressed.<\/td><\/tr><tr><td><code>readonly<\/code><\/td><td>The readonly widget prevents you from changing its current value e.g., a read-only&nbsp;<code>Entry<\/code>&nbsp;widget won&#8217;t allow you to change its text contents.<\/td><\/tr><tr><td><code>selected<\/code><\/td><td>The widget is selected e.g. when radio buttons are checked.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>To change the appearance of a widget dynamically, you can use the <code>map()<\/code> method of the Style object:<\/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\">style.map(style_name, query)<\/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>The <code>map()<\/code> method accepts the first argument as the name of the style e.g., <code>TButton<\/code> and <code>TLabel<\/code>.<\/p>\n\n\n\n<p>The argument query is a list of keyword arguments where each key is a style option and values are lists of tuples of <code>(state,value)<\/code>.<\/p>\n\n\n\n<p>For example, the following code dynamically changes the foreground color of a button widget:<\/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-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">App<\/span><span class=\"hljs-params\">(tk.Tk)<\/span>:<\/span>\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">__init__<\/span><span class=\"hljs-params\">(self)<\/span>:<\/span>\n        super().__init__()\n\n        self.geometry(<span class=\"hljs-string\">'300x100'<\/span>)\n\n        button = ttk.Button(self, text=<span class=\"hljs-string\">'Save'<\/span>)\n        button.pack(expand=<span class=\"hljs-literal\">True<\/span>)\n\n        style = ttk.Style(self)\n        style.configure(<span class=\"hljs-string\">'TButton'<\/span>, font=(<span class=\"hljs-string\">'Helvetica'<\/span>, <span class=\"hljs-number\">16<\/span>))\n        style.map(<span class=\"hljs-string\">'TButton'<\/span>,\n                foreground=&#91;(<span class=\"hljs-string\">'pressed'<\/span>, <span class=\"hljs-string\">'blue'<\/span>),\n                            (<span class=\"hljs-string\">'active'<\/span>, <span class=\"hljs-string\">'red'<\/span>)])\n\n        print(style.layout(<span class=\"hljs-string\">'TButton'<\/span>))\n\n\n<span class=\"hljs-keyword\">if<\/span> __name__ == <span class=\"hljs-string\">\"__main__\"<\/span>:\n    app = App()\n    app.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, when you move focus to the button, its text color changes to red. And when you click or press the button, its text color turns to blue.<\/p>\n\n\n\n<p>Output:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"355\" height=\"165\" src=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2021\/01\/ttk-style-map-example.gif\" alt=\"\" class=\"wp-image-2063\"\/><\/figure><\/div>\n\n\n\n<p><\/p>\n\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\"><li>Use the <code>style.map()<\/code> method to dynamically change the appearance of a widget based on its specific state.<\/li><\/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=\"2062\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/tkinter\/ttk-style-map\/\"\n\t\t\t\tdata-post-title=\"How to Change the Appearances of Widgets Dynamically Using Ttk Style map() Method\"\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=\"2062\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/tkinter\/ttk-style-map\/\"\n\t\t\t\tdata-post-title=\"How to Change the Appearances of Widgets Dynamically Using Ttk Style map() Method\"\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 how to use the ttk Style map() method to dynamically change the appearance of a widget based on its specific state.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":1232,"menu_order":52,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-2062","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/2062","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=2062"}],"version-history":[{"count":0,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/2062\/revisions"}],"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=2062"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}