{"id":22375,"date":"2021-09-27T18:49:00","date_gmt":"2021-09-27T18:49:00","guid":{"rendered":"https:\/\/www.askpython.com\/?p=22375"},"modified":"2021-10-01T21:31:14","modified_gmt":"2021-10-01T21:31:14","slug":"python-convert-number-to-words","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python\/python-convert-number-to-words","title":{"rendered":"Python: Convert Number to Words"},"content":{"rendered":"\n<p>Hello, Everyone! In this article, we&#8217;ll look at how to create a Python GUI project that converts integral values to words.<\/p>\n\n\n\n<p>Let&#8217;s get started on the project straight away!<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Step 1: Importing Modules<\/h2>\n\n\n\n<p>We start off by importing all the necessary modules\/libraries into our program. <\/p>\n\n\n\n<p>We will be importing the <code>tkinter<\/code> module to create the GUI window. Along with this, we will be importing the <code>num2words<\/code> module in order to achieve the number to words functionality.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; gutter: true; title: ; notranslate\" title=\"\">\nimport num2words as n2w\nfrom tkinter import *\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Step 2: Create GUI Window<\/h2>\n\n\n\n<p>Now, we will be creating the basic <a href=\"https:\/\/www.askpython.com\/python-modules\/tkinter\/tkinter-canvas\" target=\"_blank\" data-type=\"URL\" data-id=\"https:\/\/www.askpython.com\/python-modules\/tkinter\/tkinter-canvas\" rel=\"noreferrer noopener\">Tkinter <\/a>window using the basic functions and then add some basic widgets onto the screen. Some of them include Labels, Entry boxes, and buttons.<\/p>\n\n\n\n<p>If you are unaware of Tkinter widgets, look at the tutorials mentioned below:<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li><a href=\"https:\/\/www.askpython.com\/python\/tkinter-gui-widgets\">Tkinter GUI Widgets \u2013 A Complete Reference<\/a><\/li><li><a href=\"https:\/\/www.askpython.com\/python-modules\/tkinter\/tkinter-entry-widget\">Tkinter Entry Widget<\/a><\/li><li><a href=\"https:\/\/www.askpython.com\/python-modules\/tkinter\/tkinter-text-widget-tkinter-scrollbar\">Tkinter Text Widget with Tkinter Scrollbar<\/a><\/li><li><a href=\"https:\/\/www.askpython.com\/python-modules\/tkinter\/tkinter-frame-and-label\">Tkinter Frame and Label: An easy reference<\/a><\/li><\/ol>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; gutter: true; title: ; notranslate\" title=\"\">\nroot = Tk()\nroot.title(&quot;Numbers tdo Words&quot;)\nroot.geometry(&quot;650x400&quot;)\nnum = StringVar()\ntitle = Label(root, text=&quot;Number to Words converter&quot;,fg=&quot;Blue&quot;, font=(&quot;Arial&quot;, 20, &#039;bold&#039;)).place(x=220, y=10)\nformats_lable = Label(root, text=&quot;Formats supported :  &quot;,fg=&quot;green&quot;, font=(&quot;Arial&quot;, 10, &#039;bold&#039;)).place(x=100, y=70)\npos_format_lable = Label(root, text=&quot;1. Positives :  &quot;,fg=&quot;green&quot;, font=(&quot;Arial&quot;, 10, &#039;bold&#039;)).place(x=200, y=90)\nneg_format_lable = Label(root, text=&quot;2. Negatives &quot;,fg=&quot;green&quot;, font=(&quot;Arial&quot;, 10, &#039;bold&#039;)).place(x=200, y=110)\nfloat_format_lable = Label(root, text=&quot;3. Zeros  &quot;,fg=&quot;green&quot;, font=(&quot;Arial&quot;, 10, &#039;bold&#039;)).place(x=200, y=130)\nzero_format_lable = Label(root, text=&quot;4. Floating points\/decimals\/fractions  &quot;,fg=&quot;green&quot;, font=(&quot;Arial&quot;, 10, &#039;bold&#039;)).place(x=200, y=150)\nnum_entry_lable = Label(root, text=&quot;Enter a number :&quot;,fg=&quot;Blue&quot;, font=(&quot;Arial&quot;, 15, &#039;bold&#039;)).place(x=50, y=200)\nnum_entry = Entry(root,textvariable=num,width=30).place(x=220, y=200)\nbtn = Button(master=root, text=&quot;calculate&quot;,fg=&quot;green&quot;,\nfont=(&quot;Arial&quot;, 10, &#039;bold&#039;),command=num_to_words).place(x=280,y=230)\ndisplay = Label(root, text=&quot;&quot;,fg=&quot;black&quot;, font=(&quot;Arial&quot;, 10, &#039;bold&#039;))\ndisplay.place(x=10, y=300)\nroot.mainloop()\n<\/pre><\/div>\n\n\n<p>You might notice that in <code>Line 21<\/code> we have the <code>command<\/code> attribute of the button. The attribute is set to <code>num_to_words<\/code> but we haven&#8217;t declared the function yer.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Step 3: The num_to_words function<\/h2>\n\n\n\n<p>In this function, we will be first reading the input given by the user and then convert the value read to words using the <code>num2words<\/code> function and finally changing the display value to the computed word form of the number.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; gutter: true; title: ; notranslate\" title=\"\">\ndef num_to_words():\n    given_num = float(num.get())\n    num_in_word = n2w.num2words(given_num)\n    display.config(text=str(num_in_word).capitalize())\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Convert numbers to words using Python Tkinter<\/h2>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; gutter: true; title: ; notranslate\" title=\"\">\nimport num2words as n2w\nfrom tkinter import *\n\ndef num_to_words():\n    given_num = float(num.get())\n    num_in_word = n2w.num2words(given_num)\n    display.config(text=str(num_in_word).capitalize())\n\nroot = Tk()\nroot.title(&quot;Numbers tdo Words&quot;)\nroot.geometry(&quot;650x400&quot;)\nnum = StringVar()\ntitle = Label(root, text=&quot;Number to Words converter&quot;,fg=&quot;Blue&quot;, font=(&quot;Arial&quot;, 20, &#039;bold&#039;)).place(x=220, y=10)\nformats_lable = Label(root, text=&quot;Formats supported :  &quot;,fg=&quot;green&quot;, font=(&quot;Arial&quot;, 10, &#039;bold&#039;)).place(x=100, y=70)\npos_format_lable = Label(root, text=&quot;1. Positives :  &quot;,fg=&quot;green&quot;, font=(&quot;Arial&quot;, 10, &#039;bold&#039;)).place(x=200, y=90)\nneg_format_lable = Label(root, text=&quot;2. Negatives &quot;,fg=&quot;green&quot;, font=(&quot;Arial&quot;, 10, &#039;bold&#039;)).place(x=200, y=110)\nfloat_format_lable = Label(root, text=&quot;3. Zeros  &quot;,fg=&quot;green&quot;, font=(&quot;Arial&quot;, 10, &#039;bold&#039;)).place(x=200, y=130)\nzero_format_lable = Label(root, text=&quot;4. Floating points\/decimals\/fractions  &quot;,fg=&quot;green&quot;, font=(&quot;Arial&quot;, 10, &#039;bold&#039;)).place(x=200, y=150)\nnum_entry_lable = Label(root, text=&quot;Enter a number :&quot;,fg=&quot;Blue&quot;, font=(&quot;Arial&quot;, 15, &#039;bold&#039;)).place(x=50, y=200)\nnum_entry = Entry(root,textvariable=num,width=30).place(x=220, y=200)\nbtn = Button(master=root, text=&quot;calculate&quot;,fg=&quot;green&quot;,\n    font=(&quot;Arial&quot;, 10, &#039;bold&#039;),command=num_to_words).place(x=280,y=230)\ndisplay = Label(root, text=&quot;&quot;,fg=&quot;black&quot;, font=(&quot;Arial&quot;, 10, &#039;bold&#039;))\ndisplay.place(x=10, y=300)\nroot.mainloop()\n<\/pre><\/div>\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"815\" height=\"540\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/09\/Initial_screen_Number2Words.png\" alt=\"Initial Screen Number2Words\" class=\"wp-image-22396\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/09\/Initial_screen_Number2Words.png 815w, https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/09\/Initial_screen_Number2Words-300x199.png 300w, https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/09\/Initial_screen_Number2Words-768x509.png 768w\" sizes=\"auto, (max-width: 815px) 100vw, 815px\" \/><figcaption>Initial Screen Number2Words<\/figcaption><\/figure><\/div>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Some Sample Outputs<\/h2>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"815\" height=\"540\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/09\/Sample-Output-1-_-Number-2-Words.png\" alt=\"Sample Output 1 Number 2 Words\" class=\"wp-image-22397\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/09\/Sample-Output-1-_-Number-2-Words.png 815w, https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/09\/Sample-Output-1-_-Number-2-Words-300x199.png 300w, https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/09\/Sample-Output-1-_-Number-2-Words-768x509.png 768w\" sizes=\"auto, (max-width: 815px) 100vw, 815px\" \/><figcaption>Sample Output 1 Number 2 Words<\/figcaption><\/figure><\/div>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"815\" height=\"540\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/09\/Sample-Output-2-_-Number-2-Words.png\" alt=\"Sample Output 2 Number 2 Words\" class=\"wp-image-22398\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/09\/Sample-Output-2-_-Number-2-Words.png 815w, https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/09\/Sample-Output-2-_-Number-2-Words-300x199.png 300w, https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/09\/Sample-Output-2-_-Number-2-Words-768x509.png 768w\" sizes=\"auto, (max-width: 815px) 100vw, 815px\" \/><figcaption>Sample Output 2 Number 2 Words<\/figcaption><\/figure><\/div>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"815\" height=\"540\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/09\/Sample-Output-3-_-Number-2-Words.png\" alt=\"Sample Output 3 Number 2 Words\" class=\"wp-image-22399\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/09\/Sample-Output-3-_-Number-2-Words.png 815w, https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/09\/Sample-Output-3-_-Number-2-Words-300x199.png 300w, https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/09\/Sample-Output-3-_-Number-2-Words-768x509.png 768w\" sizes=\"auto, (max-width: 815px) 100vw, 815px\" \/><figcaption>Sample Output 3 Number 2 Words<\/figcaption><\/figure><\/div>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>I hope you understood the concept very well and enjoyed the outputs as well. Thank you for taking out time to read the tutorial.<\/p>\n\n\n\n<p>Happy Learning! &#x1f607;<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Also Read:<\/h2>\n\n\n\n<ol class=\"wp-block-list\"><li><a href=\"https:\/\/www.askpython.com\/python-modules\/tkinter\/random-mobile-number-generator\">Python Tkinter Project: Random mobile number generator<\/a><\/li><li><a href=\"https:\/\/www.askpython.com\/python-modules\/tkinter\/youtube-video-downloader\">YouTube Video Downloader Using Python Tkinter<\/a><\/li><li><a href=\"https:\/\/www.askpython.com\/python-modules\/tkinter\/tkinter-alarm-clock\">Tkinter Alarm Clock \u2013 A Step-By-Step Guide<\/a><\/li><li><a href=\"https:\/\/www.askpython.com\/python-modules\/tkinter\/savings-calculator\">Python Tkinter: Simple Savings Calculator<\/a><\/li><\/ol>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n","protected":false},"excerpt":{"rendered":"<p>Hello, Everyone! In this article, we&#8217;ll look at how to create a Python GUI project that converts integral values to words. Let&#8217;s get started on the project straight away! Step 1: Importing Modules We start off by importing all the necessary modules\/libraries into our program. We will be importing the tkinter module to create the [&hellip;]<\/p>\n","protected":false},"author":28,"featured_media":22388,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-22375","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/22375","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=22375"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/22375\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media\/22388"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=22375"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=22375"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=22375"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}