{"id":633,"date":"2023-03-21T14:01:40","date_gmt":"2023-03-21T14:01:40","guid":{"rendered":"https:\/\/tinyytopic.com\/?p=633"},"modified":"2023-03-21T13:39:38","modified_gmt":"2023-03-21T13:39:38","slug":"how-to-get-a-number-as-input-in-tkinter","status":"publish","type":"post","link":"https:\/\/tinyytopic.com\/index.php\/2023\/03\/21\/how-to-get-a-number-as-input-in-tkinter\/","title":{"rendered":"How to get a number as Input in Tkinter?"},"content":{"rendered":"\n<div class=\"wp-block-uagb-advanced-heading uagb-block-51a67e10\"><h5 class=\"uagb-heading-text\"><br><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\">How to get a number as Input in Tkinter<\/mark> <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\">using Python ready-to-use function?<\/mark><\/h5><\/div>\n\n\n\n<p style=\"font-size:15px\">This Python function gets input from the user using the Tkinter input window. Ready-to-use Python function to get an Integer or Float number:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"atomic\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">def get_number_input(header, prompt, root, number_type):\n    \n    # Get an Integer number\n    if number_type == 'integer' or number_type == 'Integer' or number_type == 'int' or number_type == 'Int':\n        while True:\n            try:\n                number_str = simpledialog.askinteger(header, prompt, parent=root)\n                break\n            except ValueError:\n                print(\"That's not an integer. Please try again.\")\n                number_str = None\n        return number_str\n    \n    # Get a float number\n    elif number_type == 'float' or number_type == 'Float':\n        while True:\n            try:\n                number_str = simpledialog.askfloat(header, prompt, parent=root)\n                break\n            except ValueError:\n                print(\"That's not a float. Please try again.\")\n                number_str = None\n        return number_str<\/pre>\n\n\n\n<p style=\"font-size:15px\"><br>Write your main code as a sample below,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>header = 'Number Entry'\nprompt = 'Enter an Integer number: '\nnumber_type = 'Integer'\nnumber = get_number_input(header, prompt, root, number_type)\nprint(number)\n\nor\n\nheader = 'Number Entry'\nprompt = 'Enter a decimal number: '\nnumber_type = 'Float'\nnumber = get_number_input(header, prompt, root, number_type)\nprint(number)<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\" style=\"font-size:15px\"><code>A Sample Integer Output:\n150\n\nA Sample Decimal Output:\n12.35<\/code><\/pre>\n\n\n\n<div class=\"wp-block-uagb-advanced-heading uagb-block-ef556494\"><h5 class=\"uagb-heading-text\"><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\"><br>How does the function work?<\/mark><\/h5><\/div>\n\n\n\n<p style=\"font-size:15px\">This is a Python function called <code>get_number_input<\/code> that accepts four parameters: <code>header<\/code>, <code>prompt<\/code>, <code>root<\/code>, and <code>number_type<\/code>. Here&#8217;s how it works:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>The function starts by checking whether the <code>number_type<\/code> parameter is set to <code>'integer'<\/code>, <code>'Integer'<\/code>, <code>'int'<\/code>, or <code>'Int'<\/code>. If it is, the function uses a <code>while<\/code> loop to repeatedly ask the user for an integer input using the <code>simpledialog.askinteger<\/code> function. If the user enters a value that cannot be converted to an integer, a <code>ValueError<\/code> exception is raised, and the loop continues. Once the user enters a valid integer, the function returns that integer value.<\/li>\n\n\n\n<li>If the <code>number_type<\/code> parameter is set to <code>'float'<\/code> or <code>'Float'<\/code>, the function uses a similar <code>while<\/code> loop to ask the user for a float input using the <code>simpledialog.askfloat<\/code> function. Again, if the user enters a value that cannot be converted to a float, a <code>ValueError<\/code> exception is raised, and the loop continues. Once the user enters a valid float, the function returns that float value.<\/li>\n\n\n\n<li>If the <code>number_type<\/code> parameter is not set to <code>'integer'<\/code>, <code>'Integer'<\/code>, <code>'int'<\/code>, <code>'Int'<\/code>, <code>'float'<\/code>, or <code>'Float'<\/code>, the function does not do anything and simply returns <code>None<\/code>.<\/li>\n<\/ol>\n\n\n\n<p style=\"font-size:15px\">Overall, this function can be used to get either an integer or a float input from the user, depending on the value of the <code>number_type<\/code> parameter. If the user enters an invalid value, the function will continue to prompt them until they enter a valid one.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This Python function gets input from the user using the Tkinter input window. Ready-to-use Python function to get an Integer or Float number: Write your main code as a sample below, This is a Python function called get_number_input that accepts four parameters: header, prompt, root, and number_type. Here&#8217;s how it works: Overall, this function can [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_uag_custom_page_level_css":"","_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[12,17],"tags":[79,14,13,16,15,78],"class_list":["post-633","post","type-post","status-publish","format-standard","hentry","category-python","category-useful-function","tag-get-number","tag-programming-language","tag-python","tag-python-code","tag-python-sample-code","tag-tkinter"],"aioseo_notices":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.0 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>How to get a number as Input in Tkinter? - tinyytopic.com<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/tinyytopic.com\/index.php\/2023\/03\/21\/how-to-get-a-number-as-input-in-tkinter\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to get a number as Input in Tkinter? - tinyytopic.com\" \/>\n<meta property=\"og:description\" content=\"This Python function gets input from the user using the Tkinter input window. Ready-to-use Python function to get an Integer or Float number: Write your main code as a sample below, This is a Python function called get_number_input that accepts four parameters: header, prompt, root, and number_type. Here&#8217;s how it works: Overall, this function can [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/tinyytopic.com\/index.php\/2023\/03\/21\/how-to-get-a-number-as-input-in-tkinter\/\" \/>\n<meta property=\"og:site_name\" content=\"tinyytopic.com\" \/>\n<meta property=\"article:published_time\" content=\"2023-03-21T14:01:40+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-03-21T13:39:38+00:00\" \/>\n<meta name=\"author\" content=\"tinyytopic.com\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"tinyytopic.com\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/tinyytopic.com\/index.php\/2023\/03\/21\/how-to-get-a-number-as-input-in-tkinter\/\",\"url\":\"https:\/\/tinyytopic.com\/index.php\/2023\/03\/21\/how-to-get-a-number-as-input-in-tkinter\/\",\"name\":\"How to get a number as Input in Tkinter? - tinyytopic.com\",\"isPartOf\":{\"@id\":\"https:\/\/tinyytopic.com\/#website\"},\"datePublished\":\"2023-03-21T14:01:40+00:00\",\"dateModified\":\"2023-03-21T13:39:38+00:00\",\"author\":{\"@id\":\"https:\/\/tinyytopic.com\/#\/schema\/person\/56c840cea8539fb221a03c5fa2ef32eb\"},\"breadcrumb\":{\"@id\":\"https:\/\/tinyytopic.com\/index.php\/2023\/03\/21\/how-to-get-a-number-as-input-in-tkinter\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/tinyytopic.com\/index.php\/2023\/03\/21\/how-to-get-a-number-as-input-in-tkinter\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/tinyytopic.com\/index.php\/2023\/03\/21\/how-to-get-a-number-as-input-in-tkinter\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/tinyytopic.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How to get a number as Input in Tkinter?\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/tinyytopic.com\/#website\",\"url\":\"https:\/\/tinyytopic.com\/\",\"name\":\"tinyytopic.com\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/tinyytopic.com\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/tinyytopic.com\/#\/schema\/person\/56c840cea8539fb221a03c5fa2ef32eb\",\"name\":\"tinyytopic.com\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/tinyytopic.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/5f153681c8ca1e6d7287d858de51f968bb687221c89cf96d763ead4393881029?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/5f153681c8ca1e6d7287d858de51f968bb687221c89cf96d763ead4393881029?s=96&d=mm&r=g\",\"caption\":\"tinyytopic.com\"},\"sameAs\":[\"http:\/\/tinyytopic.com\"],\"url\":\"https:\/\/tinyytopic.com\/index.php\/author\/mmkmuthukumar21gmail-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to get a number as Input in Tkinter? - tinyytopic.com","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/tinyytopic.com\/index.php\/2023\/03\/21\/how-to-get-a-number-as-input-in-tkinter\/","og_locale":"en_US","og_type":"article","og_title":"How to get a number as Input in Tkinter? - tinyytopic.com","og_description":"This Python function gets input from the user using the Tkinter input window. Ready-to-use Python function to get an Integer or Float number: Write your main code as a sample below, This is a Python function called get_number_input that accepts four parameters: header, prompt, root, and number_type. Here&#8217;s how it works: Overall, this function can [&hellip;]","og_url":"https:\/\/tinyytopic.com\/index.php\/2023\/03\/21\/how-to-get-a-number-as-input-in-tkinter\/","og_site_name":"tinyytopic.com","article_published_time":"2023-03-21T14:01:40+00:00","article_modified_time":"2023-03-21T13:39:38+00:00","author":"tinyytopic.com","twitter_card":"summary_large_image","twitter_misc":{"Written by":"tinyytopic.com","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/tinyytopic.com\/index.php\/2023\/03\/21\/how-to-get-a-number-as-input-in-tkinter\/","url":"https:\/\/tinyytopic.com\/index.php\/2023\/03\/21\/how-to-get-a-number-as-input-in-tkinter\/","name":"How to get a number as Input in Tkinter? - tinyytopic.com","isPartOf":{"@id":"https:\/\/tinyytopic.com\/#website"},"datePublished":"2023-03-21T14:01:40+00:00","dateModified":"2023-03-21T13:39:38+00:00","author":{"@id":"https:\/\/tinyytopic.com\/#\/schema\/person\/56c840cea8539fb221a03c5fa2ef32eb"},"breadcrumb":{"@id":"https:\/\/tinyytopic.com\/index.php\/2023\/03\/21\/how-to-get-a-number-as-input-in-tkinter\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/tinyytopic.com\/index.php\/2023\/03\/21\/how-to-get-a-number-as-input-in-tkinter\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/tinyytopic.com\/index.php\/2023\/03\/21\/how-to-get-a-number-as-input-in-tkinter\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/tinyytopic.com\/"},{"@type":"ListItem","position":2,"name":"How to get a number as Input in Tkinter?"}]},{"@type":"WebSite","@id":"https:\/\/tinyytopic.com\/#website","url":"https:\/\/tinyytopic.com\/","name":"tinyytopic.com","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/tinyytopic.com\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/tinyytopic.com\/#\/schema\/person\/56c840cea8539fb221a03c5fa2ef32eb","name":"tinyytopic.com","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/tinyytopic.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/5f153681c8ca1e6d7287d858de51f968bb687221c89cf96d763ead4393881029?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/5f153681c8ca1e6d7287d858de51f968bb687221c89cf96d763ead4393881029?s=96&d=mm&r=g","caption":"tinyytopic.com"},"sameAs":["http:\/\/tinyytopic.com"],"url":"https:\/\/tinyytopic.com\/index.php\/author\/mmkmuthukumar21gmail-com\/"}]}},"uagb_featured_image_src":{"full":false,"thumbnail":false,"medium":false,"medium_large":false,"large":false,"1536x1536":false,"2048x2048":false},"uagb_author_info":{"display_name":"tinyytopic.com","author_link":"https:\/\/tinyytopic.com\/index.php\/author\/mmkmuthukumar21gmail-com\/"},"uagb_comment_info":2,"uagb_excerpt":"This Python function gets input from the user using the Tkinter input window. Ready-to-use Python function to get an Integer or Float number: Write your main code as a sample below, This is a Python function called get_number_input that accepts four parameters: header, prompt, root, and number_type. Here&#8217;s how it works: Overall, this function can&hellip;","_links":{"self":[{"href":"https:\/\/tinyytopic.com\/index.php\/wp-json\/wp\/v2\/posts\/633","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/tinyytopic.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/tinyytopic.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/tinyytopic.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/tinyytopic.com\/index.php\/wp-json\/wp\/v2\/comments?post=633"}],"version-history":[{"count":3,"href":"https:\/\/tinyytopic.com\/index.php\/wp-json\/wp\/v2\/posts\/633\/revisions"}],"predecessor-version":[{"id":636,"href":"https:\/\/tinyytopic.com\/index.php\/wp-json\/wp\/v2\/posts\/633\/revisions\/636"}],"wp:attachment":[{"href":"https:\/\/tinyytopic.com\/index.php\/wp-json\/wp\/v2\/media?parent=633"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/tinyytopic.com\/index.php\/wp-json\/wp\/v2\/categories?post=633"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/tinyytopic.com\/index.php\/wp-json\/wp\/v2\/tags?post=633"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}