{"id":1577,"date":"2020-12-10T02:03:18","date_gmt":"2020-12-10T02:03:18","guid":{"rendered":"https:\/\/www.pythontutorial.net\/?page_id=1577"},"modified":"2021-01-07T07:37:58","modified_gmt":"2021-01-07T07:37:58","slug":"tkinter-askokcancel","status":"publish","type":"page","link":"https:\/\/www.pythontutorial.net\/tkinter\/tkinter-askokcancel\/","title":{"rendered":"Tkinter askokcancel"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you&#8217;ll learn how to use the Tkinter <code>askokcancel()<\/code> function to show a confirmation dialog.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='introduction-to-the-tkinter-askokcancel-function'>Introduction to the Tkinter askokcancel() function <a href=\"#introduction-to-the-tkinter-askokcancel-function\" class=\"anchor\" id=\"introduction-to-the-tkinter-askokcancel-function\" title=\"Anchor for Introduction to the Tkinter askokcancel() function\">#<\/a><\/h2>\n\n\n\n<p>The <code>askokcancel()<\/code> function shows a confirmation dialog that has two buttons: <code>OK<\/code> and <code>Cancel<\/code>.<\/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 = askokcancel(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>If you click the <code>OK<\/code> button, the function returns <code>True<\/code>. However, if you click the <code>Cancel<\/code> button, the function returns <code>False<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='tkinter-askokcancel-example'>Tkinter askokcancel() example <a href=\"#tkinter-askokcancel-example\" class=\"anchor\" id=\"tkinter-askokcancel-example\" title=\"Anchor for Tkinter askokcancel() example\">#<\/a><\/h2>\n\n\n\n<p>The following program shows a <strong>Delete All<\/strong> button. If you click the button, the program will show a confirmation dialog that has two buttons: <code>OK<\/code> and <code>Cancel<\/code>.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"302\" height=\"182\" src=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2020\/12\/Tkinter-askokcancel-example.png\" alt=\"\" class=\"wp-image-1580\" srcset=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2020\/12\/Tkinter-askokcancel-example.png 302w, https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2020\/12\/Tkinter-askokcancel-example-300x181.png 300w\" sizes=\"auto, (max-width: 302px) 100vw, 302px\" \/><\/figure><\/div>\n\n\n\n<p>If you click the <code>OK<\/code> button, the program will show a message box indicating that all the data has been deleted successfully:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"261\" height=\"154\" src=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2020\/12\/Tkinter-askokcancel-dialog.png\" alt=\"\" class=\"wp-image-1581\"\/><\/figure><\/div>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"256\" height=\"152\" src=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2020\/12\/Tkinter-askokcancel-messagebox.png\" alt=\"\" class=\"wp-image-1582\"\/><\/figure><\/div>\n\n\n\n<p>The program:<\/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> askokcancel, showinfo, WARNING\n\n<span class=\"hljs-comment\"># create the root window<\/span>\nroot = tk.Tk()\nroot.title(<span class=\"hljs-string\">'Tkinter Ok\/Cancel Dialog'<\/span>)\nroot.geometry(<span class=\"hljs-string\">'300x150'<\/span>)\n\n<span class=\"hljs-comment\"># click event handler<\/span>\n\n\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 = askokcancel(\n        title=<span class=\"hljs-string\">'Confirmation'<\/span>,\n        message=<span class=\"hljs-string\">'Deleting will delete all the data.'<\/span>,\n        icon=WARNING)\n\n    <span class=\"hljs-keyword\">if<\/span> answer:\n        showinfo(\n            title=<span class=\"hljs-string\">'Deletion Status'<\/span>,\n            message=<span class=\"hljs-string\">'The data is deleted successfully'<\/span>)\n\n\nttk.Button(\n    root,\n    text=<span class=\"hljs-string\">'Delete All'<\/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()<\/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>The following program does the same as the program above but use the object-oriented programming approach:<\/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> askokcancel, showinfo, WARNING\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.title(<span class=\"hljs-string\">'Tkinter Ok\/Cancel Dialog'<\/span>)\n        self.geometry(<span class=\"hljs-string\">'300x150'<\/span>)\n\n        delete_button = ttk.Button(\n            self,\n            text=<span class=\"hljs-string\">'Delete All'<\/span>,\n            command=self.confirm)\n\n        delete_button.pack(expand=<span class=\"hljs-literal\">True<\/span>)\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">confirm<\/span><span class=\"hljs-params\">(self)<\/span>:<\/span>\n        answer = askokcancel(\n            title=<span class=\"hljs-string\">'Confirmation'<\/span>,\n            message=<span class=\"hljs-string\">'Deleting will delete all the data.'<\/span>,\n            icon=WARNING)\n\n        <span class=\"hljs-keyword\">if<\/span> answer:\n            showinfo(\n                title=<span class=\"hljs-string\">'Deletion Status'<\/span>,\n                message=<span class=\"hljs-string\">'The data is deleted successfully'<\/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-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<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>askokcancel()<\/code> function to display a confirmation dialog with two buttons <code>OK<\/code> and <code>Cancel<\/code>.<\/li><li>The <code>askokcancel()<\/code> function returns <code>True<\/code> if you click the <code>OK<\/code> button and <code>False<\/code> if you click the <code>Cancel<\/code> button.<\/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=\"1577\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/tkinter\/tkinter-askokcancel\/\"\n\t\t\t\tdata-post-title=\"Tkinter askokcancel\"\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=\"1577\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/tkinter\/tkinter-askokcancel\/\"\n\t\t\t\tdata-post-title=\"Tkinter askokcancel\"\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 askokcancel() function to show a confirmation dialog.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":1232,"menu_order":33,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-1577","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/1577","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=1577"}],"version-history":[{"count":0,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/1577\/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=1577"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}