{"id":58,"date":"2023-03-30T04:56:55","date_gmt":"2023-03-30T04:56:55","guid":{"rendered":"http:\/\/www.programminginpython.com\/2023\/03\/30\/a-simple-python-gui-calculator-using-tkinter\/"},"modified":"2023-04-16T11:55:56","modified_gmt":"2023-04-16T11:55:56","slug":"python-gui-calculator-using-tkinter","status":"publish","type":"post","link":"https:\/\/www.programminginpython.com\/python-gui-calculator-using-tkinter\/","title":{"rendered":"A simple python GUI calculator using TKInter"},"content":{"rendered":"<p>Hello everybody, Welcome back to programminginpython.com! Now am going to show you how to make a simple Python GUI application, basically, the app is a simple calculator which adds two numbers. I used TKInter which is a standard GUI package for Python. In the <a href=\"http:\/\/programminginpython.com\/intro-python-gui-labels-frames-buttons\/\" target=\"_blank\" rel=\"noopener noreferrer\">previous post<\/a>, I covered the basic<a href=\"http:\/\/programminginpython.com\/intro-python-gui-labels-frames-buttons\/\" target=\"_blank\" rel=\"noopener noreferrer\"> introduction to GUI programming in Python<\/a>, here I will use those and also extend some other things to build this simple app. So get ready for a simple python GUI calculator using TKInter.<\/p>\n<p><iframe title=\"How to create a simple python GUI calculator using TKInter || Programming In Python\" width=\"640\" height=\"360\" data-src=\"https:\/\/www.youtube.com\/embed\/bMicaNoVTfM?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" data-load-mode=\"1\"><\/iframe><\/p>\n<p style=\"text-align: center;\"><strong>You can also watch the video on YouTube <\/strong><a href=\"https:\/\/www.youtube.com\/watch?v=bMicaNoVTfM\" target=\"_blank\" rel=\"noopener noreferrer\"><strong>here<\/strong><\/a><\/p>\n<p class=\"extra_btn_para\"><span class=\"wdg\"> <a href=\"https:\/\/github.com\/avinashn\/programminginpython.com\/blob\/master\/simple_calci.py\" target=\"_blank\" rel=\"noopener noreferrer\"> <i class=\"fa fa-github fa-lg\"><\/i> Program on Github<\/a><\/span><\/p>\n<p>Below are the screenshots, how what our basic app looks like,<\/p>\n<figure id=\"attachment_387\" class=\"wp-caption aligncenter\" style=\"width: 531px;\" aria-describedby=\"caption-attachment-387\">\n<p><figure id=\"attachment_469\" aria-describedby=\"caption-attachment-469\" style=\"width: 531px\" class=\"wp-caption aligncenter\"><img decoding=\"async\" class=\"size-full wp-image-469 lazyload\" data-src=\"https:\/\/www.programminginpython.com\/wp-content\/uploads\/2023\/03\/python_GUI_calci1.png\" alt=\"A simple python GUI calculator using TKInter\" width=\"531\" height=\"468\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 531px; --smush-placeholder-aspect-ratio: 531\/468;\" \/><figcaption id=\"caption-attachment-469\" class=\"wp-caption-text\">A simple Python GUI calculator using TKInter<\/figcaption><\/figure><figcaption id=\"caption-attachment-387\" class=\"wp-caption-text\"><\/figcaption><\/figure>\n<figure id=\"attachment_388\" class=\"wp-caption aligncenter\" style=\"width: 533px;\" aria-describedby=\"caption-attachment-388\">\n<p><figure id=\"attachment_470\" aria-describedby=\"caption-attachment-470\" style=\"width: 533px\" class=\"wp-caption aligncenter\"><img decoding=\"async\" class=\"size-full wp-image-470 lazyload\" data-src=\"https:\/\/www.programminginpython.com\/wp-content\/uploads\/2023\/03\/python_GUI_calciEER1.png\" alt=\"A simple python GUI calculator using TKInter\" width=\"533\" height=\"399\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 533px; --smush-placeholder-aspect-ratio: 533\/399;\" \/><figcaption id=\"caption-attachment-470\" class=\"wp-caption-text\">A simple Python GUI calculator using TKInter<\/figcaption><\/figure><\/figure>\n<p>&nbsp;<\/p>\n<p>Here I used, a grid layout for designing the layout, in the <a href=\"http:\/\/programminginpython.com\/intro-python-gui-labels-frames-buttons\/\" target=\"_blank\" rel=\"noopener noreferrer\">previous post<\/a>, I told about pack() which sets the widgets automatically based on available space in the window, but here we can set the widgets where we want to place them.<\/p>\n<p>Firstly import and initialize TKInter,<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">import tkinter as tk\r\n..\r\n..\r\nroot = tk.Tk()<\/pre>\n<p>I also set the size of the app and its title,<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">root.geometry('400x200+100+200')\r\nroot.title('Simple Calculator')<\/pre>\n<p>I also added some labels and input fields to read the entered numbers,<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">labelTitle = tk.Label(root, text=\"Simple Calculator\").grid(row=0, column=2)\r\nlabelNum1 = tk.Label(root, text=\"Enter a number\").grid(row=1, column=0)\r\nlabelNum2 = tk.Label(root, text=\"Enter another number\").grid(row=2, column=0)\r\nlabelResult = tk.Label(root)\r\nlabelResult.grid(row=7, column=2)\r\n\r\nentryNum1 = tk.Entry(root,textvariable=number1).grid(row=1, column=2)\r\nentryNum2 = tk.Entry(root,textvariable=number2).grid(row=2, column=2)<\/pre>\n<p>Then I add a button, when clicked, calculates the result and displays it in the label <code>labelResult<\/code> I initialized the above.<\/p>\n<p class=\"extra_btn_para\"><span class=\"wdg\"> <a href=\"https:\/\/github.com\/avinashn\/programminginpython.com\/blob\/master\/simple_calci.py\" target=\"_blank\" rel=\"noopener noreferrer\"> <i class=\"fa fa-github fa-lg\"><\/i> Program on Github<\/a><\/span><\/p>\n<p>So when the button is clicked, a function is called which calculates the sum of both the numbers.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">call_result = partial(call_result, labelResult, number1, number2) \r\nbuttonCal = tk.Button(root, text=\"Calculate\", command=call_result).grid(row=3, column=0)<\/pre>\n<p>The <code>command<\/code> parameter to <code>Button<\/code>\u00a0function calls the <code>call_result<\/code>function, which does the simple logic.<\/p>\n<p>The <code>call_result<\/code> function.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">def call_result(label_result, n1, n2):\r\n    num1 = (n1.get())\r\n    num2 = (n2.get())\r\n    result = int(num1)+int(num2)\r\n    label_result.config(text=\"Result is %d\" % result)\r\n    return<\/pre>\n<p class=\"extra_btn_para\"><span class=\"wdg\"> <a href=\"https:\/\/github.com\/avinashn\/programminginpython.com\/blob\/master\/simple_calci.py\" target=\"_blank\" rel=\"noopener noreferrer\"> <i class=\"fa fa-github fa-lg\"><\/i> Program on GitHub<\/a><\/span><\/p>\n<p>Feel free to look at my previous posts on <a href=\"http:\/\/programminginpython.com\/category\/gui-programs\/\" target=\"_blank\" rel=\"noopener noreferrer\">Python GUI programming<\/a>.<\/p>\n<h4>Program<\/h4>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">__author__ = 'Avinash'\r\n\r\nimport tkinter as tk\r\nfrom functools import partial\r\n\r\n\r\ndef call_result(label_result, n1, n2):\r\n    num1 = (n1.get())\r\n    num2 = (n2.get())\r\n    result = int(num1)+int(num2)\r\n    label_result.config(text=\"Result is %d\" % result)\r\n    return\r\n\r\nroot = tk.Tk()\r\nroot.geometry('400x200+100+200')\r\nroot.title('Simple Calculator')\r\n\r\nnumber1 = tk.StringVar()\r\nnumber2 = tk.StringVar()\r\n\r\nlabelTitle = tk.Label(root, text=\"Simple Calculator\").grid(row=0, column=2)\r\nlabelNum1 = tk.Label(root, text=\"Enter a number\").grid(row=1, column=0)\r\nlabelNum2 = tk.Label(root, text=\"Enter another number\").grid(row=2, column=0)\r\nlabelResult = tk.Label(root)\r\nlabelResult.grid(row=7, column=2)\r\n\r\nentryNum1 = tk.Entry(root, textvariable=number1).grid(row=1, column=2)\r\nentryNum2 = tk.Entry(root, textvariable=number2).grid(row=2, column=2)\r\ncall_result = partial(call_result, labelResult, number1, number2)\r\nbuttonCal = tk.Button(root, text=\"Calculate\", command=call_result).grid(row=3, column=0)\r\nroot.mainloop()<\/pre>\n<p>That\u2019s it for this post guys, also feel free to look at other Python tutorials on <a href=\"https:\/\/www.programminginpython.com\/category\/gui-programs\/\" target=\"_blank\" rel=\"noopener noreferrer\">GUI Programming<\/a>.<\/p>\n<div id=\"984867187\"><script type=\"text\/javascript\">\n        try {\n            window._mNHandle.queue.push(function (){\n                window._mNDetails.loadTag(\"984867187\", \"970x250\", \"984867187\");\n            });\n        }\n        catch (error) {}\n    <\/script><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Hello everybody, Welcome back to programminginpython.com! Now am going to show you how to make a simple python GUI application, basically, the app is a simple calculator which adds two numbers. I used\u00a0TKInter which is &hellip;<\/p>\n","protected":false},"author":1,"featured_media":471,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[17],"tags":[60,160,59,52,58,51],"class_list":["post-58","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python-gui","tag-buttons","tag-calculator","tag-frames","tag-gui","tag-labels","tag-python-gui"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.4 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>A simple python GUI calculator using TKInter - Programming In Python<\/title>\n<meta name=\"description\" content=\"A simple python GUI calculator using TKInter. Here I use elements from Python GUI package TKInter to create a simple calculator.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.programminginpython.com\/python-gui-calculator-using-tkinter\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"A simple python GUI calculator using TKInter - Programming In Python\" \/>\n<meta property=\"og:description\" content=\"A simple python GUI calculator using TKInter. Here I use elements from Python GUI package TKInter to create a simple calculator.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.programminginpython.com\/python-gui-calculator-using-tkinter\/\" \/>\n<meta property=\"og:site_name\" content=\"Programming In Python\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/programminginpython\" \/>\n<meta property=\"article:published_time\" content=\"2023-03-30T04:56:55+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-04-16T11:55:56+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.programminginpython.com\/wp-content\/uploads\/2023\/03\/GUI_Calculator-1900x4201-1.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1900\" \/>\n\t<meta property=\"og:image:height\" content=\"420\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"AVINASH NETHALA\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@python_pip\" \/>\n<meta name=\"twitter:site\" content=\"@python_pip\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"AVINASH NETHALA\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"A simple python GUI calculator using TKInter - Programming In Python","description":"A simple python GUI calculator using TKInter. Here I use elements from Python GUI package TKInter to create a simple calculator.","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:\/\/www.programminginpython.com\/python-gui-calculator-using-tkinter\/","og_locale":"en_US","og_type":"article","og_title":"A simple python GUI calculator using TKInter - Programming In Python","og_description":"A simple python GUI calculator using TKInter. Here I use elements from Python GUI package TKInter to create a simple calculator.","og_url":"https:\/\/www.programminginpython.com\/python-gui-calculator-using-tkinter\/","og_site_name":"Programming In Python","article_publisher":"https:\/\/www.facebook.com\/programminginpython","article_published_time":"2023-03-30T04:56:55+00:00","article_modified_time":"2023-04-16T11:55:56+00:00","og_image":[{"width":1900,"height":420,"url":"https:\/\/www.programminginpython.com\/wp-content\/uploads\/2023\/03\/GUI_Calculator-1900x4201-1.png","type":"image\/png"}],"author":"AVINASH NETHALA","twitter_card":"summary_large_image","twitter_creator":"@python_pip","twitter_site":"@python_pip","twitter_misc":{"Written by":"AVINASH NETHALA","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.programminginpython.com\/python-gui-calculator-using-tkinter\/#article","isPartOf":{"@id":"https:\/\/www.programminginpython.com\/python-gui-calculator-using-tkinter\/"},"author":{"name":"AVINASH NETHALA","@id":"https:\/\/www.programminginpython.com\/#\/schema\/person\/9a3c14fe46d422ebf783ee61de1e788c"},"headline":"A simple python GUI calculator using TKInter","datePublished":"2023-03-30T04:56:55+00:00","dateModified":"2023-04-16T11:55:56+00:00","mainEntityOfPage":{"@id":"https:\/\/www.programminginpython.com\/python-gui-calculator-using-tkinter\/"},"wordCount":312,"commentCount":0,"publisher":{"@id":"https:\/\/www.programminginpython.com\/#organization"},"image":{"@id":"https:\/\/www.programminginpython.com\/python-gui-calculator-using-tkinter\/#primaryimage"},"thumbnailUrl":"https:\/\/www.programminginpython.com\/wp-content\/uploads\/2023\/03\/GUI_Calculator-1900x4201-1.png","keywords":["Buttons","calculator","Frames","GUI","Labels","python GUI"],"articleSection":["Python GUI"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.programminginpython.com\/python-gui-calculator-using-tkinter\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.programminginpython.com\/python-gui-calculator-using-tkinter\/","url":"https:\/\/www.programminginpython.com\/python-gui-calculator-using-tkinter\/","name":"A simple python GUI calculator using TKInter - Programming In Python","isPartOf":{"@id":"https:\/\/www.programminginpython.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.programminginpython.com\/python-gui-calculator-using-tkinter\/#primaryimage"},"image":{"@id":"https:\/\/www.programminginpython.com\/python-gui-calculator-using-tkinter\/#primaryimage"},"thumbnailUrl":"https:\/\/www.programminginpython.com\/wp-content\/uploads\/2023\/03\/GUI_Calculator-1900x4201-1.png","datePublished":"2023-03-30T04:56:55+00:00","dateModified":"2023-04-16T11:55:56+00:00","description":"A simple python GUI calculator using TKInter. Here I use elements from Python GUI package TKInter to create a simple calculator.","breadcrumb":{"@id":"https:\/\/www.programminginpython.com\/python-gui-calculator-using-tkinter\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.programminginpython.com\/python-gui-calculator-using-tkinter\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.programminginpython.com\/python-gui-calculator-using-tkinter\/#primaryimage","url":"https:\/\/www.programminginpython.com\/wp-content\/uploads\/2023\/03\/GUI_Calculator-1900x4201-1.png","contentUrl":"https:\/\/www.programminginpython.com\/wp-content\/uploads\/2023\/03\/GUI_Calculator-1900x4201-1.png","width":1900,"height":420,"caption":"A simple python GUI calculator using TKInter"},{"@type":"BreadcrumbList","@id":"https:\/\/www.programminginpython.com\/python-gui-calculator-using-tkinter\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.programminginpython.com\/"},{"@type":"ListItem","position":2,"name":"A simple python GUI calculator using TKInter"}]},{"@type":"WebSite","@id":"https:\/\/www.programminginpython.com\/#website","url":"https:\/\/www.programminginpython.com\/","name":"Programming In Python","description":"All About Python","publisher":{"@id":"https:\/\/www.programminginpython.com\/#organization"},"alternateName":"pip","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.programminginpython.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.programminginpython.com\/#organization","name":"Programming In Python","alternateName":"PIP","url":"https:\/\/www.programminginpython.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.programminginpython.com\/#\/schema\/logo\/image\/","url":"https:\/\/www.programminginpython.com\/wp-content\/uploads\/2023\/04\/pip_logo_500_500.png","contentUrl":"https:\/\/www.programminginpython.com\/wp-content\/uploads\/2023\/04\/pip_logo_500_500.png","width":500,"height":500,"caption":"Programming In Python"},"image":{"@id":"https:\/\/www.programminginpython.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/programminginpython","https:\/\/x.com\/python_pip","https:\/\/www.youtube.com\/programminginpython","https:\/\/github.com\/avinashn\/programminginpython.com"]},{"@type":"Person","@id":"https:\/\/www.programminginpython.com\/#\/schema\/person\/9a3c14fe46d422ebf783ee61de1e788c","name":"AVINASH NETHALA","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.programminginpython.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/ed52e7670d7db94820c7430d324103ccdecb16d86611d5b29064aa9ce25a958b?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/ed52e7670d7db94820c7430d324103ccdecb16d86611d5b29064aa9ce25a958b?s=96&d=mm&r=g","caption":"AVINASH NETHALA"},"sameAs":["https:\/\/www.programminginpython.com\/"],"url":"https:\/\/www.programminginpython.com\/author\/avinash\/"}]}},"jetpack_featured_media_url":"https:\/\/www.programminginpython.com\/wp-content\/uploads\/2023\/03\/GUI_Calculator-1900x4201-1.png","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.programminginpython.com\/wp-json\/wp\/v2\/posts\/58","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.programminginpython.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.programminginpython.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.programminginpython.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.programminginpython.com\/wp-json\/wp\/v2\/comments?post=58"}],"version-history":[{"count":3,"href":"https:\/\/www.programminginpython.com\/wp-json\/wp\/v2\/posts\/58\/revisions"}],"predecessor-version":[{"id":472,"href":"https:\/\/www.programminginpython.com\/wp-json\/wp\/v2\/posts\/58\/revisions\/472"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.programminginpython.com\/wp-json\/wp\/v2\/media\/471"}],"wp:attachment":[{"href":"https:\/\/www.programminginpython.com\/wp-json\/wp\/v2\/media?parent=58"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.programminginpython.com\/wp-json\/wp\/v2\/categories?post=58"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.programminginpython.com\/wp-json\/wp\/v2\/tags?post=58"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}