{"id":1556,"date":"2020-12-09T08:30:40","date_gmt":"2020-12-09T08:30:40","guid":{"rendered":"https:\/\/www.pythontutorial.net\/?page_id=1556"},"modified":"2020-12-09T08:48:59","modified_gmt":"2020-12-09T08:48:59","slug":"tkinter-messagebox","status":"publish","type":"page","link":"https:\/\/www.pythontutorial.net\/tkinter\/tkinter-messagebox\/","title":{"rendered":"Tkinter messagebox"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you&#8217;ll learn how to show various message boxes using the <code>tkinter.messagebox<\/code> module.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='introduction-to-tkinter-messagebox-module'>Introduction to tkinter.messagebox module <a href=\"#introduction-to-tkinter-messagebox-module\" class=\"anchor\" id=\"introduction-to-tkinter-messagebox-module\" title=\"Anchor for Introduction to tkinter.messagebox module\">#<\/a><\/h2>\n\n\n\n<p>When developing a Tkinter application, you often want to notify users about the events that occurred.<\/p>\n\n\n\n<p>For example, when users click the save <a href=\"https:\/\/www.pythontutorial.net\/tkinter\/tkinter-button\/\">button<\/a>, you want to notify them that the record has been saved successfully.<\/p>\n\n\n\n<p>If an error occurred, for example, the database server is not reachable, you can notify users of the error.<\/p>\n\n\n\n<p>When the update has been completed but the record already exists, you may want to show a warning.<\/p>\n\n\n\n<p>To cover all of these scenarios, you can use various functions from the <code>tkinter.messagebox<\/code> module:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>showinfo()<\/code> &#8211; notify that an operation completed successfully.<\/li><li><code>showerror()<\/code> &#8211; notify that an operation hasn&#8217;t completed due to an error.<\/li><li><code>showwarrning()<\/code> &#8211; notify that an operation completed but something didn&#8217;t behave as expected.<\/li><\/ul>\n\n\n\n<p>All of these functions accept two arguments:<\/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\">showinfo(title, message)\nshowerror(title, message)\nshowwarrning(title, message)<\/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<ul class=\"wp-block-list\"><li>The <code>title<\/code> is displayed on the title bar of the dialog.<\/li><li>The <code>message<\/code> is shown on the dialog.<\/li><\/ul>\n\n\n\n<p>To span the message multiple lines, you can add the new line character <code>'\\n'<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='tkinter-messagebox-examples'>Tkinter messagebox examples <a href=\"#tkinter-messagebox-examples\" class=\"anchor\" id=\"tkinter-messagebox-examples\" title=\"Anchor for Tkinter messagebox examples\">#<\/a><\/h2>\n\n\n\n<p>The following program consists of three buttons. When you click a button, the corresponding message box will display.<\/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-messagebox-application.png\" alt=\"\" class=\"wp-image-1564\" srcset=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2020\/12\/Tkinter-messagebox-application.png 302w, https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2020\/12\/Tkinter-messagebox-application-300x181.png 300w\" sizes=\"auto, (max-width: 302px) 100vw, 302px\" \/><\/figure><\/div>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-9d6595d7 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<figure class=\"wp-block-image size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2020\/12\/Tkinter-messagebox-showerror.png\" alt=\"\" class=\"wp-image-1565\" width=\"220\" height=\"152\"\/><\/figure>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<figure class=\"wp-block-image size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2020\/12\/Tkinter-messagebox-showinfo.png\" alt=\"\" class=\"wp-image-1566\" width=\"256\" height=\"152\"\/><\/figure>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<figure class=\"wp-block-image size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2020\/12\/Tkinter-messagebox-showalert.png\" alt=\"\" class=\"wp-image-1567\" width=\"232\" height=\"152\"\/><\/figure>\n\n\n\n<p><\/p>\n<\/div>\n<\/div>\n\n\n\n<p><\/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> showerror, showwarning, showinfo\n\n<span class=\"hljs-comment\"># create the root window<\/span>\nroot = tk.Tk()\nroot.title(<span class=\"hljs-string\">'Tkinter MessageBox'<\/span>)\nroot.resizable(<span class=\"hljs-literal\">False<\/span>, <span class=\"hljs-literal\">False<\/span>)\nroot.geometry(<span class=\"hljs-string\">'300x150'<\/span>)\n\n<span class=\"hljs-comment\">#<\/span>\noptions = {<span class=\"hljs-string\">'fill'<\/span>: <span class=\"hljs-string\">'both'<\/span>, <span class=\"hljs-string\">'padx'<\/span>: <span class=\"hljs-number\">10<\/span>, <span class=\"hljs-string\">'pady'<\/span>: <span class=\"hljs-number\">10<\/span>, <span class=\"hljs-string\">'ipadx'<\/span>: <span class=\"hljs-number\">5<\/span>}\n\nttk.Button(\n    root,\n    text=<span class=\"hljs-string\">'Show an error message'<\/span>,\n    command=<span class=\"hljs-keyword\">lambda<\/span>: showerror(\n        title=<span class=\"hljs-string\">'Error'<\/span>,\n        message=<span class=\"hljs-string\">'This is an error message.'<\/span>)\n).pack(**options)\n\nttk.Button(\n    root,\n    text=<span class=\"hljs-string\">'Show an information message'<\/span>,\n    command=<span class=\"hljs-keyword\">lambda<\/span>: showinfo(\n        title=<span class=\"hljs-string\">'Information'<\/span>,\n        message=<span class=\"hljs-string\">'This is an information message.'<\/span>)\n).pack(**options)\n\n\nttk.Button(\n    root,\n    text=<span class=\"hljs-string\">'Show an warning message'<\/span>,\n    command=<span class=\"hljs-keyword\">lambda<\/span>: showwarning(\n        title=<span class=\"hljs-string\">'Warning'<\/span>,\n        message=<span class=\"hljs-string\">'This is a warning message.'<\/span>)\n).pack(**options)\n\n\n<span class=\"hljs-comment\"># run 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<p>How it works.<\/p>\n\n\n\n<p>First, import the <code>tkinter<\/code>, <code>tkinter.ttk<\/code>, and <code>tkinter.messagebox<\/code> modules:<\/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> showerror, showwarning, showinfo\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>Second, create the root window and initialize its properties:<\/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-comment\"># create the root window<\/span>\nroot = tk.Tk()\nroot.title(<span class=\"hljs-string\">'Tkinter MessageBox'<\/span>)\nroot.resizable(<span class=\"hljs-literal\">False<\/span>, <span class=\"hljs-literal\">False<\/span>)\nroot.geometry(<span class=\"hljs-string\">'300x150'<\/span>)\n<\/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>Third, create three buttons and assign a lambda expression to the <code>command<\/code> option of each button. Each <a href=\"https:\/\/www.pythontutorial.net\/python-basics\/python-lambda-expressions\/\">lambda expression<\/a> shows a corresponding message box.<\/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\">ttk.Button(\n    root,\n    text=<span class=\"hljs-string\">'Show an error message'<\/span>,\n    command=<span class=\"hljs-keyword\">lambda<\/span>: showerror(\n        title=<span class=\"hljs-string\">'Error'<\/span>,\n        message=<span class=\"hljs-string\">'This is an error message.'<\/span>)\n).pack(**options)\n\nttk.Button(\n    root,\n    text=<span class=\"hljs-string\">'Show an information message'<\/span>,\n    command=<span class=\"hljs-keyword\">lambda<\/span>: showinfo(\n        title=<span class=\"hljs-string\">'Information'<\/span>,\n        message=<span class=\"hljs-string\">'This is an information message.'<\/span>)\n).pack(**options)\n\n\nttk.Button(\n    root,\n    text=<span class=\"hljs-string\">'Show an warning message'<\/span>,\n    command=<span class=\"hljs-keyword\">lambda<\/span>: showwarning(\n        title=<span class=\"hljs-string\">'Warning'<\/span>,\n        message=<span class=\"hljs-string\">'This is a warning message.'<\/span>)\n).pack(**options)\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>Finally, display the root window.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">root.mainloop()    <\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><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 <code>showinfo()<\/code>, <code>showerror()<\/code>, <code>showwarrning()<\/code> functions from the <code>tkinter.messagebox<\/code> module to show an information, error, and warning message.<\/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=\"1556\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/tkinter\/tkinter-messagebox\/\"\n\t\t\t\tdata-post-title=\"Tkinter messagebox\"\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=\"1556\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/tkinter\/tkinter-messagebox\/\"\n\t\t\t\tdata-post-title=\"Tkinter messagebox\"\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 show various message boxes using the tkinter.messagebox module.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":1232,"menu_order":31,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-1556","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/1556","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=1556"}],"version-history":[{"count":0,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/1556\/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=1556"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}