{"id":1273,"date":"2020-11-26T05:59:38","date_gmt":"2020-11-26T05:59:38","guid":{"rendered":"https:\/\/www.pythontutorial.net\/?page_id=1273"},"modified":"2025-04-01T09:28:58","modified_gmt":"2025-04-01T09:28:58","slug":"tkinter-checkbox","status":"publish","type":"page","link":"https:\/\/www.pythontutorial.net\/tkinter\/tkinter-checkbox\/","title":{"rendered":"Tkinter Checkbox"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you&#8217;ll learn about the Tkinter Checkbox widget and how to use it effectively.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='introduction-to-the-tkinter-checkbox-widget'>Introduction to the Tkinter checkbox widget <a href=\"#introduction-to-the-tkinter-checkbox-widget\" class=\"anchor\" id=\"introduction-to-the-tkinter-checkbox-widget\" title=\"Anchor for Introduction to the Tkinter checkbox widget\">#<\/a><\/h2>\n\n\n\n<p>A checkbox is a widget that allows you to check and uncheck. A checkbox can hold a value and invoke a function automatically when its state changes.<\/p>\n\n\n\n<p>Typically, you use a checkbox when you want to ask users to choose between two values.<\/p>\n\n\n\n<p>To create a checkbox, you use the <code>ttk.Checkbutton<\/code> constructor:<\/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\">checkbox = ttk.Checkbutton(\n    master,\n    text=<span class=\"hljs-string\">'&lt;checkbox label&gt;'<\/span>,\n    command=callback,\n    variable=variable,\n    onvalue=<span class=\"hljs-string\">'&lt;value_when_checked&gt;'<\/span>,\n    offvalue=<span class=\"hljs-string\">'&lt;value_when_unchecked&gt;'<\/span>\n)<\/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>In this syntax:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The <code>master<\/code> argument specifies the master widget that you want to place the checkbox.<\/li>\n\n\n\n<li>The <code>text<\/code> argument specifies the label for the checkbox.<\/li>\n\n\n\n<li>The <code>command<\/code> is a callable that will be called once the checkbox is checked or unchecked.<\/li>\n\n\n\n<li>The <code>variable<\/code> holds the current value of the checkbox. Typically, you use a <a href=\"https:\/\/www.pythontutorial.net\/tkinter\/tkinter-booleanvar\/\">BooleanVar<\/a> object to track whether the checkbox is checked or not. If you want other values than <code>True<\/code> and <code>False<\/code>, you can specify them in the <code>onvalue<\/code> and <code>offvalue<\/code> options.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id='tkinter-checkbox-example'>Tkinter checkbox example <a href=\"#tkinter-checkbox-example\" class=\"anchor\" id=\"tkinter-checkbox-example\" title=\"Anchor for Tkinter checkbox example\">#<\/a><\/h2>\n\n\n\n<p>The following program illustrates how to use a checkbox widget. Once you check or uncheck the checkbox, a message box will show the on value and the off value accordingly:<\/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<span class=\"hljs-keyword\">from<\/span> tkinter.messagebox <span class=\"hljs-keyword\">import<\/span> showinfo\n\nroot = tk.Tk()\nroot.geometry(<span class=\"hljs-string\">'300x200'<\/span>)\nroot.title(<span class=\"hljs-string\">'Checkbox Demo'<\/span>)\n\n\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">show_message<\/span><span class=\"hljs-params\">()<\/span>:<\/span>\n    showinfo(\n        title=<span class=\"hljs-string\">'Result'<\/span>,\n        message=<span class=\"hljs-string\">'You agreed.'<\/span> <span class=\"hljs-keyword\">if<\/span> agreement_var.get() <span class=\"hljs-keyword\">else<\/span> <span class=\"hljs-string\">'You did not agree.'<\/span>\n    )\n\n\nagreement_var = tk.BooleanVar()\n\ncheckbox = ttk.Checkbutton(\n    root,\n    text=<span class=\"hljs-string\">'I agree'<\/span>,\n    command=show_message,\n    variable=agreement_var\n)\n\ncheckbox.pack()\n\n\nroot.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>Output:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"431\" height=\"299\" src=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2025\/04\/Tkinter-Checkbox-Demo.png\" alt=\"Tkinter Checkbox Demo\" class=\"wp-image-7359\" srcset=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2025\/04\/Tkinter-Checkbox-Demo.png 431w, https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2025\/04\/Tkinter-Checkbox-Demo-300x208.png 300w\" sizes=\"auto, (max-width: 431px) 100vw, 431px\" \/><\/figure>\n\n\n\n<p>How it works.<\/p>\n\n\n\n<p>First, create a <a href=\"https:\/\/www.pythontutorial.net\/tkinter\/tkinter-stringvar\/\">BooleanVar<\/a> object that will hold the the state of the checkbox:<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">agreement_var = tk.BooleanVar()<\/code><\/span><\/pre>\n\n\n<p>Second, <a href=\"https:\/\/www.pythontutorial.net\/python-basics\/python-functions\/\">define a function<\/a> that will execute when the state of the checkbox changes. The function shows a message whether you check or uncheck the checkbox:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">def show_message():\n    showinfo(\n        title=<span class=\"hljs-string\">'Result'<\/span>,\n        message=<span class=\"hljs-string\">'You agreed.'<\/span> <span class=\"hljs-keyword\">if<\/span> agreement_var.get() <span class=\"hljs-keyword\">else<\/span> <span class=\"hljs-string\">'You did not agree.'<\/span>\n    )<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Third, create a checkbox widget and set its options accordingly:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">checkbox = ttk.Checkbutton(\n    root,\n    text=<span class=\"hljs-string\">'I agree'<\/span>,\n    command=show_message,\n    variable=agreement_var\n)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\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\">\n<li>Use <code>Checkbutton()<\/code> to create a checkbox.<\/li>\n\n\n\n<li>Use <code>command<\/code> argument to specify a function that executes when the button is checked or unchecked.<\/li>\n\n\n\n<li>Use the <code>onvalue<\/code> and <code>offvalue<\/code> to determine what value the <code>variable<\/code> will take.<\/li>\n<\/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=\"1273\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/tkinter\/tkinter-checkbox\/\"\n\t\t\t\tdata-post-title=\"Tkinter Checkbox\"\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=\"1273\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/tkinter\/tkinter-checkbox\/\"\n\t\t\t\tdata-post-title=\"Tkinter Checkbox\"\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 about the Tkinter checkbox widget and how to use it effectively.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":1232,"menu_order":18,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-1273","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/1273","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=1273"}],"version-history":[{"count":4,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/1273\/revisions"}],"predecessor-version":[{"id":7360,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/1273\/revisions\/7360"}],"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=1273"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}