{"id":15131,"date":"2021-04-27T19:18:41","date_gmt":"2021-04-27T19:18:41","guid":{"rendered":"https:\/\/www.askpython.com\/?p=15131"},"modified":"2026-09-29T16:58:17","modified_gmt":"2026-09-29T16:58:17","slug":"gui-email-slicer","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python-modules\/tkinter\/gui-email-slicer","title":{"rendered":"Python Tkinter: A Simple Email Slicer"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Hello learner! Today we are going to build a simple Email Slicer using the <a href=\"https:\/\/www.askpython.com\/python-modules\/tkinter\/tkinter-buttons\" class=\"rank-math-link\">tkinter module in Python<\/a>. Before we begin the project let us know what an Email Slicer is.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What is an Email Slicer?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Email Slicer is a simple tool where the email address is provided as an input and as an output, the application returns the username and the domain of the email address given. It makes use of the <a href=\"https:\/\/www.askpython.com\/python\/array\/array-slicing-in-python\" class=\"rank-math-link\">slicing operations of Python<\/a>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Also Before we start writing the code let me show you how your final output screen is going to look like. Check out the image below.<\/p>\n\n\n\n<div class=\"wp-block-image is-style-default\"><figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"602\" height=\"590\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/04\/Initial_screen_Email_Slicer.png\" alt=\"Initial Screen Email Slicer\" class=\"wp-image-15146\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/04\/Initial_screen_Email_Slicer.png 602w, https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/04\/Initial_screen_Email_Slicer-300x294.png 300w\" sizes=\"auto, (max-width: 602px) 100vw, 602px\" \/><figcaption>Initial Screen Email Slicer<\/figcaption><\/figure><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">Looks pretty cool right? So let us begin!<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Building the Email Slicer in Python<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">In building the email slicer let us first design the screen and then declare functions for the functionalities of the buttons.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1. Designing the Screen<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">To design the screen let&#8217;s import the <code>Tkinter<\/code> module and then create a blank window to start with. At this step, we will also configure some changes such as background color and application name. The code below will help you do the same.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; gutter: true; title: ; notranslate\" title=\"\">\nimport tkinter as tk\nwindow = tk.Tk()\nwindow.geometry(&quot;480x440&quot;)\nwindow.config(bg=&quot;#BE361A&quot;)\nwindow.resizable(width=False,height=False)\nwindow.title(&#039;Simple Email Slicer&#039;)\n\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">Here we first imported the module. Then we created a window, set the window dimensions, and made the background color orangish (you can customize your screen according to your preference!). <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">We made sure the dimensions remain constant overall so we set the resizable attributes to &#8216;False&#8217;. We also set the title of the application as the name of the application. Now last but not least you need to view the window, don&#8217;t you? <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">So for that, we run the <code>mainloop<\/code> function. And the result should look something like the image shown below!<\/p>\n\n\n\n<div class=\"wp-block-image is-style-default\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"601\" height=\"590\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/04\/Screen1_Email_Slicer.png\" alt=\"Screen1 Email Slicer\" class=\"wp-image-15162\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/04\/Screen1_Email_Slicer.png 601w, https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/04\/Screen1_Email_Slicer-300x295.png 300w\" sizes=\"auto, (max-width: 601px) 100vw, 601px\" \/><figcaption>Screen1 Email Slicer<\/figcaption><\/figure><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">2. Adding necessary elements to our window<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Now an important part is adding all the necessary elements in our application. The elements that we require for the application are:<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li><a href=\"https:\/\/www.askpython.com\/python-modules\/tkinter\/tkinter-frame-and-label\" class=\"rank-math-link\">Labels<\/a><\/li><li><a href=\"https:\/\/www.askpython.com\/python-modules\/tkinter\/tkinter-entry-widget\" class=\"rank-math-link\">Entry Box<\/a> (To take the input)<\/li><li><a href=\"https:\/\/www.askpython.com\/python-modules\/tkinter\/tkinter-buttons\" class=\"rank-math-link\">Buttons<\/a><\/li><li>TextBox (To print the output)<\/li><\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">Now let&#8217;s add all the information about the application first. Now you can change it however you want. I will keep them as per my preference. The code to add the required labels is shown below.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; gutter: true; highlight: [8,9,10,11,12,14]; title: ; notranslate\" title=\"\">\nimport tkinter as tk\nwindow = tk.Tk()\nwindow.geometry(&quot;480x440&quot;)\nwindow.config(bg=&quot;#BE361A&quot;)\nwindow.resizable(width=False,height=False)\nwindow.title(&#039;Simple Email Slicer&#039;)\n\ngreeting = tk.Label(text=&quot;Welcome to A Simple Email Slicer!&quot;,font=(12),foreground=&quot;white&quot;,background=&quot;black&quot;)\nInfo = tk. Label(text=&quot;Enter your email id click the done button!\\n The application will extract your username and domain name.&quot;,foreground= &quot;white&quot;,background=&quot;#BE361A&quot;,font=(10))\n\ngreeting.pack()\nInfo.pack()\n\nwindow.mainloop()\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">The added lines have been highlighted for your reference. Obviously, the Label declaration and packing goes before we launch the window. If you have noticed, I have also made some changes such as the foreground and background and the font size. Again, you can choose all the changes according to your requirements. The new results are as follows: <\/p>\n\n\n\n<div class=\"wp-block-image is-style-default\"><figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"601\" height=\"590\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/04\/Screen2_Email_Slicer.png\" alt=\"Screen2 Email Slicer\" class=\"wp-image-15180\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/04\/Screen2_Email_Slicer.png 601w, https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/04\/Screen2_Email_Slicer-300x295.png 300w\" sizes=\"auto, (max-width: 601px) 100vw, 601px\" \/><figcaption>Screen2 Email Slicer<\/figcaption><\/figure><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">Just like we added the labels, we will be adding the buttons as well as the entry boxes. By now, I am guessing you are familiar with all that stuff. So I will be showing you the whole design code now! <\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; gutter: true; title: ; notranslate\" title=\"\">\nimport tkinter as tk\nwindow = tk.Tk()\nwindow.geometry(&quot;480x440&quot;)\nwindow.config(bg=&quot;#BE361A&quot;)\nwindow.resizable(width=False,height=False)\nwindow.title(&#039;Simple Email Slicer&#039;)\n\n# All the Labels\ngreeting = tk.Label(text=&quot;Welcome to A Simple Email Slicer!&quot;,font=(12),foreground=&quot;white&quot;,background=&quot;black&quot;)\nInfo = tk. Label(foreground= &quot;white&quot;,background=&quot;#BE361A&quot;,font=(10),text=&quot;Enter your email id click the done button!\\n The application will extract your username and domain name.&quot;)\nentry_label = tk.Label(foreground= &quot;white&quot;,font=(10),background=&quot;black&quot;,text=&quot;Enter Your Email Id: &quot;)\nresult_label = tk.Label(font=(10),foreground= &quot;white&quot;,background=&quot;black&quot;,text=&quot;The results are as follows:&quot;)\nempty_label0=tk.Label(background=&quot;#BE361A&quot;)\nempty_label1=tk.Label(background=&quot;#BE361A&quot;)\nempty_label2=tk.Label(background=&quot;#BE361A&quot;)\nempty_label3=tk.Label(background=&quot;#BE361A&quot;)\nempty_label4=tk.Label(background=&quot;#BE361A&quot;)\nempty_label5=tk.Label(background=&quot;#BE361A&quot;)\n\n#The Entry box\nentry = tk.Entry(font=(11),width=50,justify=&#039;center&#039;)\n\n#The two Buttons\nbutton = tk.Button(text=&quot;Done!&quot;,font=(11))\nreset=tk.Button(text=&quot;Reset!&quot;,font=(11))\n\n#Result\ntext_box = tk.Text(height=5,width=50)\n\n#Packing Everything Together\nempty_label0.pack()\ngreeting.pack()\nInfo.pack()\nempty_label1.pack()\nentry_label.pack()\nempty_label4.pack()\nentry.pack()\nempty_label2.pack()\nbutton.pack()\nempty_label5.pack()\nreset.pack()\nempty_label3.pack()\nresult_label.pack()\ntext_box.pack()\n\nwindow.mainloop()\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">Now you might have noticed some empty labels defined which seem to be pretty useless but they are helpful to add line breaks between various elements when we use <code>pack<\/code> to place the elements and not <code>place<\/code> (requires x and y coordinates). Now let me show you how our final screen looks!<\/p>\n\n\n\n<div class=\"wp-block-image is-style-default\"><figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"601\" height=\"590\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/04\/complete_design_Email_Slicer.png\" alt=\"Complete Design Email Slicer\" class=\"wp-image-15189\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/04\/complete_design_Email_Slicer.png 601w, https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/04\/complete_design_Email_Slicer-300x295.png 300w\" sizes=\"auto, (max-width: 601px) 100vw, 601px\" \/><figcaption>Complete Design Email Slicer<\/figcaption><\/figure><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">So now that we are done with the design currently the buttons are not doing anything at all. For that we will add some additional functions and variables to our code.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">3. Adding Functionalities to the Buttons<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Before jumping directly to the buttons we need a place to store our email address so we create a <strong>&#8216;String Variable&#8217; <\/strong>to store whatever is entered in the entry box. We make use of <code>StringVar<\/code> and add an attribute named <code>textvariable<\/code> to our Entry box created. I am only showing you the part that you need to change for the same.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; gutter: true; title: ; notranslate\" title=\"\">\ne1=tk.StringVar()\nentry = tk.Entry(font=(11),width=50,justify=&#039;center&#039;,textvariable=e1)\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">Function 1: Get Username and Domain<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Now that storing of the email address is done! We will create two functions for the two buttons as well. The first function will take the email address from the e1 the variable we created earlier and then use the <code>stripping<\/code> operations on the email address in order to extract the username and domain.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The code to do the same operations is shown below.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; gutter: true; title: ; notranslate\" title=\"\">\ndef result():\n    try:\n        email=entry.get()\n        inp_email = email.strip()\n        username = inp_email&#x5B;0:inp_email.index(&#039;@&#039;)]\n        domain = inp_email&#x5B;inp_email.index(&#039;@&#039;) + 1:]\n        text_box.delete(&#039;1.0&#039;, tk.END)\n        msg = &#039;Email entered was: {}\\nYour email username is {}\\nAnd your email domain server is {}&#039;\n        msg_formatted = msg.format(email,username,domain)\n        text_box.insert(tk.END,msg_formatted)\n        entry.delete(0, &#039;end&#039;)\n    except:\n        text_box.delete(&#039;1.0&#039;, tk.END)\n        text_box.insert(tk.END,&quot;ERROR!&quot;)\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">Now, the question is <strong>&#8216;why have I used the <a href=\"https:\/\/www.askpython.com\/python\/python-exception-handling\" class=\"rank-math-link\">try-except block<\/a> here?&#8217;<\/strong> <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The reason is that if a person doesn&#8217;t enter a valid email address the code might result into a series of unhandled errors and even the user won&#8217;t realize what&#8217;s wrong with the application. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Hence to avoid that situation we make sure that if the user enters an invalid email id then except block will print the <strong>&#8216;Error&#8217;<\/strong> message.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Now the operations are pretty simple. <code>get<\/code> email from the entry box variable, <code>strip<\/code> the string to remove all the whitespaces (if any). <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Now to get the username <code>slice<\/code> the string until we reach the <strong>&#8216;@&#8217;<\/strong> symbol and the rest (excluding the @ symbol) goes into the domain name.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Now the last operation is to make use of the <strong>textbox<\/strong> we have created. To be on a safer end, we remove everything that is inside the textbox (if any) and then insert a message which tells the user about the domain and username found. We will also make sure that the entry box is clean for the next input. In case any invalid entry is found, the <code>except<\/code> block is run and it prints the <strong>&#8216;Error&#8217;<\/strong> message.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Function 2: Reset the Application<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Now after the first function is successfully made the reset function becomes pretty simple. All the reset function does is that it cleans the text box and the entry box. The code for the same is shown below.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; gutter: true; title: ; notranslate\" title=\"\">\ndef reset_all():\n    text_box.delete(&#039;1.0&#039;, tk.END)\n    entry.delete(0, &#039;end&#039;)\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\"><strong>But the buttons won&#8217;t work yet! Why? <\/strong>Because we haven&#8217;t added the functions in the Buttons definitions. Basically, all you need to do is add an attribute of the name <code>command<\/code> to the Buttons definitions. The code below displays the same.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; gutter: true; title: ; notranslate\" title=\"\">\nbutton = tk.Button(text=&quot;Done!&quot;,command=result,font=(11))\nreset=tk.Button(text=&quot;Reset!&quot;,command=reset_all,font=(11))\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">Awesome! Now we are all set to run our application!<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">The final Code and Output<\/h2>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; gutter: true; title: ; notranslate\" title=\"\">\nimport tkinter as tk\nwindow = tk.Tk()\nwindow.geometry(&quot;480x440&quot;)\nwindow.config(bg=&quot;#BE361A&quot;)\nwindow.resizable(width=False,height=False)\nwindow.title(&#039;Simple Email Slicer&#039;)\n\ndef result():\n    try:\n        email=entry.get()\n        inp_email = email.strip()\n        username = inp_email&#x5B;0:inp_email.index(&#039;@&#039;)]\n        domain = inp_email&#x5B;inp_email.index(&#039;@&#039;) + 1:]\n        text_box.delete(&#039;1.0&#039;, tk.END)\n        msg = &#039;Email entered was: {}\\nYour email username is {}\\nAnd your email domain server is {}&#039;\n        msg_formatted = msg.format(email,username,domain)\n        text_box.insert(tk.END,msg_formatted)\n        entry.delete(0, &#039;end&#039;)\n    except:\n        text_box.delete(&#039;1.0&#039;, tk.END)\n        text_box.insert(tk.END,&quot;ERROR!&quot;)\n\ndef reset_all():\n    text_box.delete(&#039;1.0&#039;, tk.END)\n    entry.delete(0, &#039;end&#039;)\n\n# Labels\ngreeting = tk.Label(text=&quot;Welcome to A Simple Email Slicer!&quot;,font=(12),foreground=&quot;white&quot;,background=&quot;black&quot;)\nInfo = tk. Label(foreground= &quot;white&quot;,background=&quot;#BE361A&quot;,font=(10),text=&quot;Enter your email id click the done button!\\n The application will extract your username and domain name.&quot;)\nentry_label = tk.Label(foreground= &quot;white&quot;,font=(10),background=&quot;black&quot;,text=&quot;Enter Your Email Id: &quot;)\nresult_label = tk.Label(font=(10),foreground= &quot;white&quot;,background=&quot;black&quot;,text=&quot;The results are as follows:&quot;)\nempty_label0=tk.Label(background=&quot;#BE361A&quot;)\nempty_label1=tk.Label(background=&quot;#BE361A&quot;)\nempty_label2=tk.Label(background=&quot;#BE361A&quot;)\nempty_label3=tk.Label(background=&quot;#BE361A&quot;)\nempty_label4=tk.Label(background=&quot;#BE361A&quot;)\nempty_label5=tk.Label(background=&quot;#BE361A&quot;)\n\n#Entry\ne1=tk.StringVar()\nentry = tk.Entry(font=(11),width=50,justify=&#039;center&#039;,textvariable=e1)\n\n#Buttons\nbutton = tk.Button(text=&quot;Done!&quot;,command=result,font=(11))\nreset=tk.Button(text=&quot;Reset!&quot;,command=reset_all,font=(11))\n\n#Result\ntext_box = tk.Text(height=5,width=50)\n\n#Packing Everything Together\nempty_label0.pack()\ngreeting.pack()\nInfo.pack()\nempty_label1.pack()\nentry_label.pack()\nempty_label4.pack()\nentry.pack()\nempty_label2.pack()\nbutton.pack()\nempty_label5.pack()\nreset.pack()\nempty_label3.pack()\nresult_label.pack()\ntext_box.pack()\n\nwindow.mainloop()\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">The Image below shows the output of the code when run on a sample email address. Test it out with various email addresses yourself!<\/p>\n\n\n\n<div class=\"wp-block-image is-style-default\"><figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"602\" height=\"590\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/04\/Output_Email_slicer.png\" alt=\"Output Email Slicer\" class=\"wp-image-15230\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/04\/Output_Email_slicer.png 602w, https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/04\/Output_Email_slicer-300x294.png 300w\" sizes=\"auto, (max-width: 602px) 100vw, 602px\" \/><figcaption>Output Email Slicer<\/figcaption><\/figure><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Congratulations! You have successfully learned how to implement a simple Email Slicer all by yourself!  Hope you learned something! Happy Coding!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hello learner! Today we are going to build a simple Email Slicer using the tkinter module in Python. Before we begin the project let us know what an Email Slicer is. What is an Email Slicer? Email Slicer is a simple tool where the email address is provided as an input and as an output, [&hellip;]<\/p>\n","protected":false},"author":28,"featured_media":68659,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[92],"tags":[],"class_list":["post-15131","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tkinter"],"_links":{"self":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/15131","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/users\/28"}],"replies":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/comments?post=15131"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/15131\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media\/68659"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=15131"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=15131"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=15131"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}