{"id":1680,"date":"2020-12-15T08:43:17","date_gmt":"2020-12-15T08:43:17","guid":{"rendered":"https:\/\/www.pythontutorial.net\/?page_id=1680"},"modified":"2021-12-07T11:43:26","modified_gmt":"2021-12-07T11:43:26","slug":"tkinter-theme","status":"publish","type":"page","link":"https:\/\/www.pythontutorial.net\/tkinter\/tkinter-theme\/","title":{"rendered":"Tkinter Themes"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you&#8217;ll learn how to change the Tkinter theme from one to another.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='introduction-to-tkinter-ttk-themes'>Introduction to Tkinter ttk themes <a href=\"#introduction-to-tkinter-ttk-themes\" class=\"anchor\" id=\"introduction-to-tkinter-ttk-themes\" title=\"Anchor for Introduction to Tkinter ttk themes\">#<\/a><\/h2>\n\n\n\n<p>In Tkinter, a theme determines the &#8220;look &amp; feel&#8221; of all the widgets. It&#8217;s a collection of <a href=\"https:\/\/www.pythontutorial.net\/tkinter\/ttk-style\/\">styles<\/a> for all the <a href=\"https:\/\/www.pythontutorial.net\/tkinter\/tkinter-ttk\/\">ttk widgets<\/a>. <\/p>\n\n\n\n<p>A style specifies the appearance of a widget class e.g., a Button. Each theme comes with a set of styles. It&#8217;s possible to change the appearance of widgets by: <\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Modifying the built-in styles <\/li><li>or creatting new styles<\/li><\/ul>\n\n\n\n<p>Tkinter allows you to change the current theme to another. When you change the current theme to a new one, Tkinter will apply the styles of that theme to all the <a href=\"https:\/\/www.pythontutorial.net\/tkinter\/tkinter-ttk\/\">ttk widgets<\/a>.<\/p>\n\n\n\n<p>To get the available themes, you use the <code>theme_names()<\/code> method of the <code>ttk.Style<\/code> instance.<\/p>\n\n\n\n<p>First, create a new instance of the <code>ttk.Style<\/code> class:<\/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 = ttk.Style(root)<\/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>Second, get the available themes by calling the <code>theme_names()<\/code> method:<\/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\">style.theme_names()<\/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>To get the current theme, you use the <code>theme_use()<\/code> method:<\/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\">current_theme = style.theme_use()<\/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>Note that every operating system (OS) such as Windows, macOS, and Linux comes with its own predefined themes. If you use the <code>theme_names()<\/code> and <code>theme_use()<\/code> methods on different OS, you&#8217;ll get different results.<\/p>\n\n\n\n<p>To change the current theme to a new one, you pass the new theme name to the <code>theme_use()<\/code> method:<\/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\">style.theme_use(theme_name)<\/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>The following program shows all themes in your system and allows you to change one theme to another:<\/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\"><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        <span class=\"hljs-comment\"># root window<\/span>\n        self.title(<span class=\"hljs-string\">'Theme Demo'<\/span>)\n        self.geometry(<span class=\"hljs-string\">'400x300'<\/span>)\n        self.style = ttk.Style(self)\n\n        <span class=\"hljs-comment\"># label<\/span>\n        label = ttk.Label(self, text=<span class=\"hljs-string\">'Name:'<\/span>)\n        label.grid(column=<span class=\"hljs-number\">0<\/span>, row=<span class=\"hljs-number\">0<\/span>, padx=<span class=\"hljs-number\">10<\/span>, pady=<span class=\"hljs-number\">10<\/span>,  sticky=<span class=\"hljs-string\">'w'<\/span>)\n        <span class=\"hljs-comment\"># entry<\/span>\n        textbox = ttk.Entry(self)\n        textbox.grid(column=<span class=\"hljs-number\">1<\/span>, row=<span class=\"hljs-number\">0<\/span>, padx=<span class=\"hljs-number\">10<\/span>, pady=<span class=\"hljs-number\">10<\/span>,  sticky=<span class=\"hljs-string\">'w'<\/span>)\n        <span class=\"hljs-comment\"># button<\/span>\n        btn = ttk.Button(self, text=<span class=\"hljs-string\">'Show'<\/span>)\n        btn.grid(column=<span class=\"hljs-number\">2<\/span>, row=<span class=\"hljs-number\">0<\/span>, padx=<span class=\"hljs-number\">10<\/span>, pady=<span class=\"hljs-number\">10<\/span>,  sticky=<span class=\"hljs-string\">'w'<\/span>)\n\n        <span class=\"hljs-comment\"># radio button<\/span>\n        self.selected_theme = tk.StringVar()\n        theme_frame = ttk.LabelFrame(self, text=<span class=\"hljs-string\">'Themes'<\/span>)\n        theme_frame.grid(padx=<span class=\"hljs-number\">10<\/span>, pady=<span class=\"hljs-number\">10<\/span>, ipadx=<span class=\"hljs-number\">20<\/span>, ipady=<span class=\"hljs-number\">20<\/span>, sticky=<span class=\"hljs-string\">'w'<\/span>)\n\n        <span class=\"hljs-keyword\">for<\/span> theme_name <span class=\"hljs-keyword\">in<\/span> self.style.theme_names():\n            rb = ttk.Radiobutton(\n                theme_frame,\n                text=theme_name,\n                value=theme_name,\n                variable=self.selected_theme,\n                command=self.change_theme)\n            rb.pack(expand=<span class=\"hljs-literal\">True<\/span>, fill=<span class=\"hljs-string\">'both'<\/span>)\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">change_theme<\/span><span class=\"hljs-params\">(self)<\/span>:<\/span>\n        self.style.theme_use(self.selected_theme.get())\n\n\n<span class=\"hljs-keyword\">if<\/span> __name__ == <span class=\"hljs-string\">\"__main__\"<\/span>:\n    app = App()\n    app.mainloop()\n<\/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 example, when you select a theme from the <a href=\"https:\/\/www.pythontutorial.net\/tkinter\/tkinter-radio-button\/\">radio button<\/a> list, the <code>change_theme()<\/code> method will apply the selected theme.<\/p>\n\n\n\n<p>If you run the program on Windows 10, you&#8217;ll see the following window:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"402\" height=\"332\" src=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2020\/12\/Tkinter-Theme.png\" alt=\"Tkinter Theme\" class=\"wp-image-1681\" srcset=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2020\/12\/Tkinter-Theme.png 402w, https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2020\/12\/Tkinter-Theme-300x248.png 300w\" sizes=\"auto, (max-width: 402px) 100vw, 402px\" \/><\/figure>\n\n\n\n<p>If you change the theme to classic, you&#8217;ll see the style of the widgets (<a href=\"https:\/\/www.pythontutorial.net\/tkinter\/tkinter-label\/\">Label<\/a>, <a href=\"https:\/\/www.pythontutorial.net\/tkinter\/tkinter-entry\/\">Entry<\/a>, <a href=\"https:\/\/www.pythontutorial.net\/tkinter\/tkinter-button\/\">Button<\/a>, <a href=\"https:\/\/www.pythontutorial.net\/tkinter\/tkinter-labelframe\/\">LabelFrame<\/a>, and <a href=\"https:\/\/www.pythontutorial.net\/tkinter\/tkinter-radio-button\/\">Radio Button<\/a>) change to the following:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"402\" height=\"347\" src=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2020\/12\/Tkinter-Theme-change-ttk-theme.png\" alt=\"ttk Theme\" class=\"wp-image-1682\" srcset=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2020\/12\/Tkinter-Theme-change-ttk-theme.png 402w, https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2020\/12\/Tkinter-Theme-change-ttk-theme-300x259.png 300w\" sizes=\"auto, (max-width: 402px) 100vw, 402px\" \/><\/figure>\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>Create an instance of the <code>ttk.Style<\/code> class to access the style database.<\/li><li>Use the <code>style.theme_names()<\/code> method to get available themes from the Operating System on which the Tkinter application is running.<\/li><li>Use the <code>style.theme_use()<\/code> method to change the current theme to a new one.<\/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=\"1680\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/tkinter\/tkinter-theme\/\"\n\t\t\t\tdata-post-title=\"Tkinter Themes\"\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=\"1680\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/tkinter\/tkinter-theme\/\"\n\t\t\t\tdata-post-title=\"Tkinter Themes\"\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 change the Tkinter theme from one to another.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":1232,"menu_order":45,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-1680","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/1680","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=1680"}],"version-history":[{"count":0,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/1680\/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=1680"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}