{"id":1584,"date":"2020-12-10T02:59:46","date_gmt":"2020-12-10T02:59:46","guid":{"rendered":"https:\/\/www.pythontutorial.net\/?page_id=1584"},"modified":"2020-12-11T00:55:03","modified_gmt":"2020-12-11T00:55:03","slug":"tkinter-askretrycancel","status":"publish","type":"page","link":"https:\/\/www.pythontutorial.net\/tkinter\/tkinter-askretrycancel\/","title":{"rendered":"Tkinter askretrycancel"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you&#8217;ll learn how to use the Tkinter <code>askretrycancel()<\/code> function to show a Retry\/Cancel confirmation dialog.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='introduction-to-the-tkinter-askretrycancel-function'>Introduction to the Tkinter askretrycancel() function <a href=\"#introduction-to-the-tkinter-askretrycancel-function\" class=\"anchor\" id=\"introduction-to-the-tkinter-askretrycancel-function\" title=\"Anchor for Introduction to the Tkinter askretrycancel() function\">#<\/a><\/h2>\n\n\n\n<p>Sometimes, the application performs a task but fails to do so because of an error.<\/p>\n\n\n\n<p>For example, you may want to connect to a database server. However, the database server is currently not reachable. It may be offline for a short period of time.<\/p>\n\n\n\n<p>In this case, you can display a confirmation dialog that allow users to reconnect to the database or just keep the application as is.<\/p>\n\n\n\n<p>To display the Retry\/Cancel dialog, you can use the <code>askretrycancel()<\/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 = askretrycancel(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>The <code>askretrycancel()<\/code> function returns <code>True<\/code> if the <code>Retry<\/code> button is clicked. If the <code>Cancel<\/code> button is clicked, it returns <code>False<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='tkinter-askretrycancel-function-example'>Tkinter askretrycancel() function example <a href=\"#tkinter-askretrycancel-function-example\" class=\"anchor\" id=\"tkinter-askretrycancel-function-example\" title=\"Anchor for Tkinter &lt;code&gt;askretrycancel()&lt;\/code&gt; function example\">#<\/a><\/h2>\n\n\n\n<p>The following program shows a button that simulates a bad database connection:<\/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-askretrycancel-example.png\" alt=\"\" class=\"wp-image-1585\" srcset=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2020\/12\/Tkinter-askretrycancel-example.png 302w, https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2020\/12\/Tkinter-askretrycancel-example-300x181.png 300w\" sizes=\"auto, (max-width: 302px) 100vw, 302px\" \/><\/figure><\/div>\n\n\n\n<p>If you click the button, it&#8217;ll show a Retry\/Cancel dialog saying that the database server is not reachable. It&#8217;ll also request you to reconnect to the database server:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"391\" height=\"152\" src=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2020\/12\/Tkinter-askretrycancel-dialog.png\" alt=\"\" class=\"wp-image-1586\" srcset=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2020\/12\/Tkinter-askretrycancel-dialog.png 391w, https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2020\/12\/Tkinter-askretrycancel-dialog-300x117.png 300w\" sizes=\"auto, (max-width: 391px) 100vw, 391px\" \/><\/figure><\/div>\n\n\n\n<p>If you click the Retry button, it&#8217;ll show a dialog indicating that the program is attempting to reconnect to the database server.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"313\" height=\"152\" src=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2020\/12\/Tkinter-askretrycancel-messagebox.png\" alt=\"\" class=\"wp-image-1587\" srcset=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2020\/12\/Tkinter-askretrycancel-messagebox.png 313w, https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2020\/12\/Tkinter-askretrycancel-messagebox-300x146.png 300w\" sizes=\"auto, (max-width: 313px) 100vw, 313px\" \/><\/figure><\/div>\n\n\n\n<p>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> askretrycancel, showinfo\n\n<span class=\"hljs-comment\"># create the root window<\/span>\nroot = tk.Tk()\nroot.title(<span class=\"hljs-string\">'Tkinter OK\/Retry 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 = askretrycancel(\n        title=<span class=\"hljs-string\">'Connection Issue'<\/span>,\n        message=<span class=\"hljs-string\">'The database server is unreachable. Do you want to retry?'<\/span>\n    )\n    <span class=\"hljs-keyword\">if<\/span> answer:\n        showinfo(\n            title=<span class=\"hljs-string\">'Information'<\/span>,\n            message=<span class=\"hljs-string\">'Attempt to connect to the database again.'<\/span>)\n\n\nttk.Button(\n    root,\n    text=<span class=\"hljs-string\">'Connect to the Database Server'<\/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-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='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>askretrycancel()<\/code> function to display a Retry\/Cancel dialog to confirm users to carry an operation again.<\/li><li>The <code>askretrycancel()<\/code> function returns <code>True<\/code> if the Retry button is clicked. If the Cancel button is clicked, it returns <code>False<\/code>.<\/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=\"1584\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/tkinter\/tkinter-askretrycancel\/\"\n\t\t\t\tdata-post-title=\"Tkinter askretrycancel\"\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=\"1584\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/tkinter\/tkinter-askretrycancel\/\"\n\t\t\t\tdata-post-title=\"Tkinter askretrycancel\"\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 askretrycancel() function to show a Retry\/Cancel confirmation dialog.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":1232,"menu_order":34,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-1584","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/1584","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=1584"}],"version-history":[{"count":0,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/1584\/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=1584"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}