{"id":764,"date":"2017-10-14T13:33:26","date_gmt":"2017-10-14T18:33:26","guid":{"rendered":"http:\/\/bluegalaxy.info\/codewalk\/?p=764"},"modified":"2017-10-23T14:12:54","modified_gmt":"2017-10-23T19:12:54","slug":"python-how-to-create-gui-pop-up-windows-with-tkinter","status":"publish","type":"post","link":"https:\/\/bluegalaxy.info\/codewalk\/2017\/10\/14\/python-how-to-create-gui-pop-up-windows-with-tkinter\/","title":{"rendered":"Python: How to create GUI pop-up windows with Tkinter"},"content":{"rendered":"<p>When creating a program in Python, a useful thing to be able to do is to have a pop-up window appear on the screen with a special message. This can be used to give instant feedback to the user, alert the user to an error, or inform the user that the program completed successfully.<\/p>\n<p>In Python this can be done using the Tkinter module with the following code, which I have placed into an easy to use function:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">from Tkinter import *\r\n\r\ndef alert_popup(title, message, path):\r\n    \"\"\"Generate a pop-up window for special messages.\"\"\"\r\n    root = Tk()\r\n    root.title(title)\r\n\r\n    w = 400     # popup window width\r\n    h = 200     # popup window height\r\n\r\n    sw = root.winfo_screenwidth()\r\n    sh = root.winfo_screenheight()\r\n\r\n    x = (sw - w)\/2\r\n    y = (sh - h)\/2\r\n    root.geometry('%dx%d+%d+%d' % (w, h, x, y))\r\n\r\n    m = message\r\n    m += '\\n'\r\n    m += path\r\n    w = Label(root, text=m, width=120, height=10)\r\n    w.pack()\r\n    b = Button(root, text=\"OK\", command=root.destroy, width=10)\r\n    b.pack()\r\n    mainloop()\r\n\r\n\r\n# Examples\r\nalert_popup(\"Title goes here..\", \"Hello World!\", \"A path or second message can go here..\")\r\n\r\nalert_popup(\"Success!\", \"Processing completed. Your report was saved here:\", \"C:\/path\/to\/my_reports\/report1.xlsx\")<\/pre>\n<p>To use this function, you must import the Tkinter module. The width and height of the pop-up window can be defined inside the function. This function takes in the title you want for the alert, a main message, and a second message which can optionally be left empty.<\/p>\n<p>Here are what the two examples in the code look like:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-767 size-full\" src=\"http:\/\/bluegalaxy.info\/codewalk\/wp-content\/uploads\/2017\/10\/example_popup1.png\" alt=\"\" width=\"400\" height=\"226\" id=\"img_ds\" srcset=\"https:\/\/bluegalaxy.info\/codewalk\/wp-content\/uploads\/2017\/10\/example_popup1.png 400w, https:\/\/bluegalaxy.info\/codewalk\/wp-content\/uploads\/2017\/10\/example_popup1-300x170.png 300w\" sizes=\"auto, (max-width: 400px) 100vw, 400px\" \/><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-768 size-full\" src=\"http:\/\/bluegalaxy.info\/codewalk\/wp-content\/uploads\/2017\/10\/example_popup2.png\" alt=\"\" width=\"400\" height=\"226\" id=\"img_ds\" srcset=\"https:\/\/bluegalaxy.info\/codewalk\/wp-content\/uploads\/2017\/10\/example_popup2.png 400w, https:\/\/bluegalaxy.info\/codewalk\/wp-content\/uploads\/2017\/10\/example_popup2-300x170.png 300w\" sizes=\"auto, (max-width: 400px) 100vw, 400px\" \/><\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>When creating a program in Python, a useful thing to be able to do is to have a pop-up window appear on the screen with a special message. This can be used to give instant feedback to the user, alert the user to an error, or inform the user that the program completed successfully. In &hellip; <a href=\"https:\/\/bluegalaxy.info\/codewalk\/2017\/10\/14\/python-how-to-create-gui-pop-up-windows-with-tkinter\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Python: How to create GUI pop-up windows with Tkinter<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[22],"tags":[55,4,54],"class_list":["post-764","post","type-post","status-publish","format-standard","hentry","category-python-language","tag-pop-up","tag-python","tag-tkinter"],"_links":{"self":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts\/764","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/comments?post=764"}],"version-history":[{"count":5,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts\/764\/revisions"}],"predecessor-version":[{"id":803,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts\/764\/revisions\/803"}],"wp:attachment":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/media?parent=764"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/categories?post=764"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/tags?post=764"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}