{"id":1569,"date":"2020-12-10T01:01:57","date_gmt":"2020-12-10T01:01:57","guid":{"rendered":"https:\/\/www.pythontutorial.net\/?page_id=1569"},"modified":"2021-01-07T07:33:24","modified_gmt":"2021-01-07T07:33:24","slug":"tkinter-askyesno","status":"publish","type":"page","link":"https:\/\/www.pythontutorial.net\/tkinter\/tkinter-askyesno\/","title":{"rendered":"Tkinter askyesno"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you&#8217;ll learn how to use the Tkinter <code>askyesno()<\/code> function to show a dialog that asks for user confirmation.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='introduction-to-the-tkinter-askyesno-function'>Introduction to the Tkinter askyesno() function <a href=\"#introduction-to-the-tkinter-askyesno-function\" class=\"anchor\" id=\"introduction-to-the-tkinter-askyesno-function\" title=\"Anchor for Introduction to the Tkinter askyesno() function\">#<\/a><\/h2>\n\n\n\n<p>Sometimes, you need to ask for user confirmation. For example, if users click the quit button, you want to ask whether they really want to close the application. Or they just accidentally do so:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"277\" height=\"152\" src=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2020\/12\/Tkinter-askyesno-dialog.png\" alt=\"Tkinter askyesno\" class=\"wp-image-1570\"\/><\/figure><\/div>\n\n\n\n<p>To show a dialog that asks for user confirmation, you use the <code>askyesno()<\/code> function.<\/p>\n\n\n\n<p>The dialog will have a title, a message, and two buttons (yes and no).<\/p>\n\n\n\n<p>When you click the <code>yes<\/code> button, the function returns <code>True<\/code>. However, if you click the <code>no<\/code> button, it returns <code>False<\/code>.<\/p>\n\n\n\n<p>The following shows the syntax of the <code>askyesno()<\/code> function:<\/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\">answer = askyesno(title, message, **options)<\/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>Note that the <code>answer<\/code> is a Boolean value, either <code>True<\/code> or <code>False<\/code>.<\/p>\n\n\n\n<p>Tkinter also has another function called <code>askquestion()<\/code>, which is similar to the <code>askyesno()<\/code> function except that it returns a string with a value of <code>'yes'<\/code> or <code>'no'<\/code>:<\/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\">answer = askquestion(title, message, **options)<\/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<h2 class=\"wp-block-heading\" id='tkinter-askyesno-function-example'>Tkinter askyesno()  function example <a href=\"#tkinter-askyesno-function-example\" class=\"anchor\" id=\"tkinter-askyesno-function-example\" title=\"Anchor for Tkinter askyesno()  function example\">#<\/a><\/h2>\n\n\n\n<p>The following program illustrates how to use the Tkinter <code>askyesno()<\/code> function:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"304\" height=\"184\" src=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2020\/12\/Tkinter-askyesno.png\" alt=\"Tkinter askyesno example\" class=\"wp-image-1572\" srcset=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2020\/12\/Tkinter-askyesno.png 304w, https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2020\/12\/Tkinter-askyesno-300x182.png 300w\" sizes=\"auto, (max-width: 304px) 100vw, 304px\" \/><\/figure><\/div>\n\n\n\n<p>When you click the <code>Quit<\/code> button, it&#8217;ll show a confirmation dialog: <\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2020\/12\/Tkinter-askyesno-dialog.png\" alt=\"Tkinter askyesno function\" class=\"wp-image-1570\" width=\"277\" height=\"152\"\/><\/figure><\/div>\n\n\n\n<p>If you click the <code>yes<\/code> button, the application will be closed. Otherwise, it&#8217;ll stay.<\/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\"><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> askyesno\n\n<span class=\"hljs-comment\"># create the root window<\/span>\nroot = tk.Tk()\nroot.title(<span class=\"hljs-string\">'Tkinter Yes\/No Dialog'<\/span>)\nroot.geometry(<span class=\"hljs-string\">'300x150'<\/span>)\n\n<span class=\"hljs-comment\"># click event handler<\/span>\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">confirm<\/span><span class=\"hljs-params\">()<\/span>:<\/span>\n    answer = askyesno(title=<span class=\"hljs-string\">'confirmation'<\/span>,\n                    message=<span class=\"hljs-string\">'Are you sure that you want to quit?'<\/span>)\n    <span class=\"hljs-keyword\">if<\/span> answer:\n        root.destroy()\n\n\nttk.Button(\n    root,\n    text=<span class=\"hljs-string\">'Ask Yes\/No'<\/span>,\n    command=confirm).pack(expand=<span class=\"hljs-literal\">True<\/span>)\n\n\n<span class=\"hljs-comment\"># start the app<\/span>\nroot.mainloop()\n<\/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>The following is the same program but use the object-oriented programming approach:<\/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\"><span class=\"hljs-keyword\">import<\/span> tkinter <span class=\"hljs-keyword\">as<\/span> tk\r\n<span class=\"hljs-keyword\">from<\/span> tkinter <span class=\"hljs-keyword\">import<\/span> ttk\r\n<span class=\"hljs-keyword\">from<\/span> tkinter.messagebox <span class=\"hljs-keyword\">import<\/span> askyesno, askquestion\r\n\r\n\r\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>\r\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">__init__<\/span><span class=\"hljs-params\">(self)<\/span>:<\/span>\r\n        super().__init__()\r\n\r\n        self.title(<span class=\"hljs-string\">'Tkinter Yes\/No Dialog'<\/span>)\r\n        self.geometry(<span class=\"hljs-string\">'300x150'<\/span>)\r\n\r\n        <span class=\"hljs-comment\"># Quit button<\/span>\r\n        quit_button = ttk.Button(self, text=<span class=\"hljs-string\">'Quit'<\/span>, command=self.confirm)\r\n        quit_button.pack(expand=<span class=\"hljs-literal\">True<\/span>)\r\n\r\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">confirm<\/span><span class=\"hljs-params\">(self)<\/span>:<\/span>\r\n        answer = askyesno(title=<span class=\"hljs-string\">'Confirmation'<\/span>,\r\n                          message=<span class=\"hljs-string\">'Are you sure that you want to quit?'<\/span>)\r\n        <span class=\"hljs-keyword\">if<\/span> answer:\r\n            self.destroy()\r\n\r\n\r\n<span class=\"hljs-keyword\">if<\/span> __name__ == <span class=\"hljs-string\">\"__main__\"<\/span>:\r\n    app = App()\r\n    app.mainloop()\r<\/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<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 Tkinter <code>askyesno()<\/code> function to show a dialog that asks for user confirmation.<\/li><li>The <code>askyesno()<\/code> function returns <code>True<\/code> if you click the yes button, otherwise, it returns <code>False<\/code>.<\/li><li>The <code>askquestion()<\/code> function returns a string with a value of <code>'yes'<\/code> or <code>'no'<\/code> instead.<\/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=\"1569\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/tkinter\/tkinter-askyesno\/\"\n\t\t\t\tdata-post-title=\"Tkinter askyesno\"\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=\"1569\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/tkinter\/tkinter-askyesno\/\"\n\t\t\t\tdata-post-title=\"Tkinter askyesno\"\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 Tkinter askyesno() function to show a dialog that asks for user confirmation.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":1232,"menu_order":32,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-1569","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/1569","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=1569"}],"version-history":[{"count":0,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/1569\/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=1569"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}