{"id":1806,"date":"2023-10-15T16:45:35","date_gmt":"2023-10-15T14:45:35","guid":{"rendered":"https:\/\/kindsonthegenius.com\/blog\/?p=1806"},"modified":"2023-10-16T21:44:07","modified_gmt":"2023-10-16T19:44:07","slug":"how-to-create-a-simple-calculator-in-python-using-tkinter-gui-step-by-step","status":"publish","type":"post","link":"https:\/\/kindsonthegenius.com\/blog\/how-to-create-a-simple-calculator-in-python-using-tkinter-gui-step-by-step\/","title":{"rendered":"How to Create a Simple Calculator in Python using Tkinter GUI &#8211; Step by Step"},"content":{"rendered":"<p>In this tutorial, you will learn how to create a calculator application in Python using Tkinter module.<\/p>\n<ol>\n<li><a href=\"#t1\">Plan Your Calculator Layout<\/a><\/li>\n<li><a href=\"#t2\">Code and Test the Layout (Grid Layout)<\/a><\/li>\n<li><a href=\"#t3\">Setup the Calculator Screen<\/a><\/li>\n<li><a href=\"#t4\">Setup the Keypad<\/a><\/li>\n<li><a href=\"#t5\">Changes the Font sizes<\/a><\/li>\n<li><a href=\"#t6\">Display Selected Number on Screen<\/a><\/li>\n<li><a href=\"#t7\">Coding the Assign Key<\/a><\/li>\n<li><a href=\"#t8\">Coding the Calculate Key<\/a><\/li>\n<li><a href=\"#t9\">Further Enhancements<\/a><\/li>\n<\/ol>\n<p>Related Tutorial &#8211; <a href=\"https:\/\/kindsonthegenius.com\/blog\/how-to-create-a-simple-calculator-in-python-try-it\/\" target=\"_blank\" rel=\"noopener\">How to Create a Command Line Calculator in Python<\/a><\/p>\n<h3 id=\"t1\">1. Plan Your Calculator Layout<\/h3>\n<p>We would adopt a grid layout for the calculator UI. This is shown below. Each of the frames is placed at a given location on the grid. For instance, the display_frame is placed on location (0,0) inside the root_frame. Similarly, the keypad frame is placed at location (1,0) inside the root_frame but in the second row.<\/p>\n<p>I have used the numbers in bold to show the size of the frames. Also,\u00a0 indentation is used to show the parent-child relationship between the frame.<\/p>\n<figure id=\"attachment_1812\" aria-describedby=\"caption-attachment-1812\" style=\"width: 1024px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/kindsonthegenius.com\/blog\/wp-content\/uploads\/2023\/10\/Calculator-Frames-Layout-1-scaled.jpg\"><img decoding=\"async\" loading=\"lazy\" class=\"size-large wp-image-1812\" src=\"https:\/\/kindsonthegenius.com\/blog\/wp-content\/uploads\/2023\/10\/Calculator-Frames-Layout-1-1024x376.jpg\" alt=\"Calculator Frames Layout\" width=\"1024\" height=\"376\" srcset=\"https:\/\/kindsonthegenius.com\/blog\/wp-content\/uploads\/2023\/10\/Calculator-Frames-Layout-1-1024x376.jpg 1024w, https:\/\/kindsonthegenius.com\/blog\/wp-content\/uploads\/2023\/10\/Calculator-Frames-Layout-1-300x110.jpg 300w, https:\/\/kindsonthegenius.com\/blog\/wp-content\/uploads\/2023\/10\/Calculator-Frames-Layout-1-768x282.jpg 768w, https:\/\/kindsonthegenius.com\/blog\/wp-content\/uploads\/2023\/10\/Calculator-Frames-Layout-1-1536x564.jpg 1536w, https:\/\/kindsonthegenius.com\/blog\/wp-content\/uploads\/2023\/10\/Calculator-Frames-Layout-1-2048x752.jpg 2048w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/a><figcaption id=\"caption-attachment-1812\" class=\"wp-caption-text\">Calculator Frames Layout<\/figcaption><\/figure>\n<p>&nbsp;<\/p>\n<h3 id=\"t2\">2. Code and Test the Layout<\/h3>\n<p>The code for the frames is quite easy to understand. It follows from the layout discussed above. The little addition is the padding of the frames. We add the attribute <strong>padx<\/strong> to specify the left and right padding. The attribute <strong>pady<\/strong> specifies the top and bottom padding.<\/p>\n<p>The code for the layout is given below:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008800; font-weight: bold;\">import<\/span> <span style=\"color: #0e84b5; font-weight: bold;\">tkinter<\/span> <span style=\"color: #008800; font-weight: bold;\">as<\/span> <span style=\"color: #0e84b5; font-weight: bold;\">tk<\/span>\r\n\r\nwindow <span style=\"color: #333333;\">=<\/span> tk<span style=\"color: #333333;\">.<\/span>Tk()\r\nwindow<span style=\"color: #333333;\">.<\/span>title(<span style=\"background-color: #fff0f0;\">'Simple Python GUI Calculator'<\/span>)\r\n\r\nroot_frame <span style=\"color: #333333;\">=<\/span> tk<span style=\"color: #333333;\">.<\/span>Frame(window)\r\nwindow<span style=\"color: #333333;\">.<\/span>geometry(<span style=\"background-color: #fff0f0;\">'650x600'<\/span>)\r\n\r\ndisplay_frame <span style=\"color: #333333;\">=<\/span> tk<span style=\"color: #333333;\">.<\/span>LabelFrame(root_frame, width<span style=\"color: #333333;\">=<\/span><span style=\"color: #0000dd; font-weight: bold;\">400<\/span>, height<span style=\"color: #333333;\">=<\/span><span style=\"color: #0000dd; font-weight: bold;\">80<\/span>)\r\ndisplay_frame<span style=\"color: #333333;\">.<\/span>grid(row<span style=\"color: #333333;\">=<\/span><span style=\"color: #0000dd; font-weight: bold;\">0<\/span>, column<span style=\"color: #333333;\">=<\/span><span style=\"color: #0000dd; font-weight: bold;\">0<\/span>, pady<span style=\"color: #333333;\">=<\/span>(<span style=\"color: #0000dd; font-weight: bold;\">10<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">10<\/span>))\r\ndisplay_frame<span style=\"color: #333333;\">.<\/span>grid_propagate(<span style=\"color: #0000dd; font-weight: bold;\">0<\/span>)\r\n\r\nkeypad_frame <span style=\"color: #333333;\">=<\/span> tk<span style=\"color: #333333;\">.<\/span>LabelFrame(root_frame, width<span style=\"color: #333333;\">=<\/span><span style=\"color: #0000dd; font-weight: bold;\">400<\/span>, height<span style=\"color: #333333;\">=<\/span><span style=\"color: #0000dd; font-weight: bold;\">450<\/span>)\r\nkeypad_frame<span style=\"color: #333333;\">.<\/span>grid(row<span style=\"color: #333333;\">=<\/span><span style=\"color: #0000dd; font-weight: bold;\">1<\/span>, column<span style=\"color: #333333;\">=<\/span><span style=\"color: #0000dd; font-weight: bold;\">0<\/span>, padx<span style=\"color: #333333;\">=<\/span><span style=\"color: #0000dd; font-weight: bold;\">10<\/span>, pady<span style=\"color: #333333;\">=<\/span>(<span style=\"color: #0000dd; font-weight: bold;\">10<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">10<\/span>))\r\nkeypad_frame<span style=\"color: #333333;\">.<\/span>grid_propagate(<span style=\"color: #0000dd; font-weight: bold;\">0<\/span>)\r\n\r\nnumbers_frame <span style=\"color: #333333;\">=<\/span> tk<span style=\"color: #333333;\">.<\/span>LabelFrame(keypad_frame, width<span style=\"color: #333333;\">=<\/span><span style=\"color: #0000dd; font-weight: bold;\">260<\/span>, height<span style=\"color: #333333;\">=<\/span><span style=\"color: #0000dd; font-weight: bold;\">400<\/span>)\r\nnumbers_frame<span style=\"color: #333333;\">.<\/span>grid(row<span style=\"color: #333333;\">=<\/span><span style=\"color: #0000dd; font-weight: bold;\">0<\/span>, column<span style=\"color: #333333;\">=<\/span><span style=\"color: #0000dd; font-weight: bold;\">0<\/span>, padx<span style=\"color: #333333;\">=<\/span>(<span style=\"color: #0000dd; font-weight: bold;\">10<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">20<\/span>), pady<span style=\"color: #333333;\">=<\/span>(<span style=\"color: #0000dd; font-weight: bold;\">10<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">10<\/span>))\r\nnumbers_frame<span style=\"color: #333333;\">.<\/span>grid_propagate(<span style=\"color: #0000dd; font-weight: bold;\">0<\/span>)\r\n\r\nsigns_frame <span style=\"color: #333333;\">=<\/span> tk<span style=\"color: #333333;\">.<\/span>LabelFrame(keypad_frame, width<span style=\"color: #333333;\">=<\/span><span style=\"color: #0000dd; font-weight: bold;\">100<\/span>, height<span style=\"color: #333333;\">=<\/span><span style=\"color: #0000dd; font-weight: bold;\">400<\/span>)\r\nsigns_frame<span style=\"color: #333333;\">.<\/span>grid(row<span style=\"color: #333333;\">=<\/span><span style=\"color: #0000dd; font-weight: bold;\">0<\/span>, column<span style=\"color: #333333;\">=<\/span><span style=\"color: #0000dd; font-weight: bold;\">1<\/span>, pady<span style=\"color: #333333;\">=<\/span>(<span style=\"color: #0000dd; font-weight: bold;\">10<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">10<\/span>))\r\nsigns_frame<span style=\"color: #333333;\">.<\/span>grid_propagate(<span style=\"color: #0000dd; font-weight: bold;\">0<\/span>)\r\n\r\nroot_frame<span style=\"color: #333333;\">.<\/span>pack()\r\n\r\nwindow<span style=\"color: #333333;\">.<\/span>mainloop()\r\n<\/pre>\n<p>&nbsp;<\/p>\n<h3 id=\"t3\">3. Setup the Calculator Screen<\/h3>\n<p>The calculator screen would be a textbox which is Text() in Tkinter. This would positioned at location (0,0) inside the display_frame.<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">display_box <span style=\"color: #333333;\">=<\/span> tk<span style=\"color: #333333;\">.<\/span>Text(display_frame, width<span style=\"color: #333333;\">=<\/span><span style=\"color: #0000dd; font-weight: bold;\">20<\/span>, height<span style=\"color: #333333;\">=<\/span><span style=\"color: #0000dd; font-weight: bold;\">1<\/span>)\r\ndisplay_box<span style=\"color: #333333;\">.<\/span>grid(row<span style=\"color: #333333;\">=<\/span><span style=\"color: #0000dd; font-weight: bold;\">0<\/span>, column<span style=\"color: #333333;\">=<\/span><span style=\"color: #0000dd; font-weight: bold;\">0<\/span>, padx<span style=\"color: #333333;\">=<\/span>(<span style=\"color: #0000dd; font-weight: bold;\">20<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">5<\/span>), pady<span style=\"color: #333333;\">=<\/span>(<span style=\"color: #0000dd; font-weight: bold;\">20<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">5<\/span>))\r\n<\/pre>\n<p>&nbsp;<\/p>\n<h3 id=\"t4\">4. Setup the Calculator Keypad<\/h3>\n<p>The calculator keypad consists of the numbers_frame and the signs_frame. These are implemented as buttons as shown in the code.<\/p>\n<p>The buttons for the numbers_keypad are placed inside the numbers_frame in 3 by 4 grid. The buttons for the signs_keypad are placed inside the signs_frame 1 by 4 grid.<\/p>\n<p>The code for button 1 and button 2 is given below:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">button_1 <span style=\"color: #333333;\">=<\/span> tk<span style=\"color: #333333;\">.<\/span>Button(numbers_frame, text<span style=\"color: #333333;\">=<\/span><span style=\"background-color: #fff0f0;\">'1'<\/span>, height<span style=\"color: #333333;\">=<\/span><span style=\"color: #0000dd; font-weight: bold;\">3<\/span>, width<span style=\"color: #333333;\">=<\/span><span style=\"color: #0000dd; font-weight: bold;\">3<\/span>)\r\nbutton_1<span style=\"color: #333333;\">.<\/span>grid(row<span style=\"color: #333333;\">=<\/span><span style=\"color: #0000dd; font-weight: bold;\">0<\/span>, column<span style=\"color: #333333;\">=<\/span><span style=\"color: #0000dd; font-weight: bold;\">0<\/span>, padx<span style=\"color: #333333;\">=<\/span><span style=\"color: #0000dd; font-weight: bold;\">10<\/span>, pady<span style=\"color: #333333;\">=<\/span><span style=\"color: #0000dd; font-weight: bold;\">10<\/span>)\r\n\r\nbutton_2 <span style=\"color: #333333;\">=<\/span> tk<span style=\"color: #333333;\">.<\/span>Button(numbers_frame, text<span style=\"color: #333333;\">=<\/span><span style=\"background-color: #fff0f0;\">'2'<\/span>, height<span style=\"color: #333333;\">=<\/span><span style=\"color: #0000dd; font-weight: bold;\">3<\/span>, width<span style=\"color: #333333;\">=<\/span><span style=\"color: #0000dd; font-weight: bold;\">3<\/span>)\r\nbutton_2<span style=\"color: #333333;\">.<\/span>grid(row<span style=\"color: #333333;\">=<\/span><span style=\"color: #0000dd; font-weight: bold;\">0<\/span>, column<span style=\"color: #333333;\">=<\/span><span style=\"color: #0000dd; font-weight: bold;\">1<\/span>)\r\n<\/pre>\n<p>You can can complete code for the rest of the buttons or you can get the code from Github here.<\/p>\n<p>Also the button for the addition sign(+) is given below<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">button_add <span style=\"color: #333333;\">=<\/span> tk<span style=\"color: #333333;\">.<\/span>Button(signs_frame, text<span style=\"color: #333333;\">=<\/span><span style=\"background-color: #fff0f0;\">'+'<\/span>, height<span style=\"color: #333333;\">=<\/span><span style=\"color: #0000dd; font-weight: bold;\">3<\/span>, width<span style=\"color: #333333;\">=<\/span><span style=\"color: #0000dd; font-weight: bold;\">3<\/span>)\r\nbutton_add<span style=\"color: #333333;\">.<\/span>grid(row<span style=\"color: #333333;\">=<\/span><span style=\"color: #0000dd; font-weight: bold;\">0<\/span>, column<span style=\"color: #333333;\">=<\/span><span style=\"color: #0000dd; font-weight: bold;\">0<\/span>, padx<span style=\"color: #333333;\">=<\/span>(<span style=\"color: #0000dd; font-weight: bold;\">10<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">10<\/span>), pady<span style=\"color: #333333;\">=<\/span><span style=\"color: #0000dd; font-weight: bold;\">10<\/span>)\r\n\r\nbutton_subtract <span style=\"color: #333333;\">=<\/span> tk<span style=\"color: #333333;\">.<\/span>Button(signs_frame, text<span style=\"color: #333333;\">=<\/span><span style=\"background-color: #fff0f0;\">'-'<\/span>, height<span style=\"color: #333333;\">=<\/span><span style=\"color: #0000dd; font-weight: bold;\">3<\/span>, width<span style=\"color: #333333;\">=<\/span><span style=\"color: #0000dd; font-weight: bold;\">3<\/span>)\r\nbutton_subtract<span style=\"color: #333333;\">.<\/span>grid(row<span style=\"color: #333333;\">=<\/span><span style=\"color: #0000dd; font-weight: bold;\">1<\/span>, column<span style=\"color: #333333;\">=<\/span><span style=\"color: #0000dd; font-weight: bold;\">0<\/span>)\r\n\r\nbutton_multiply <span style=\"color: #333333;\">=<\/span> tk<span style=\"color: #333333;\">.<\/span>Button(signs_frame, text<span style=\"color: #333333;\">=<\/span><span style=\"background-color: #fff0f0;\">'*'<\/span>, height<span style=\"color: #333333;\">=<\/span><span style=\"color: #0000dd; font-weight: bold;\">3<\/span>, width<span style=\"color: #333333;\">=<\/span><span style=\"color: #0000dd; font-weight: bold;\">3<\/span>)\r\nbutton_multiply<span style=\"color: #333333;\">.<\/span>grid(row<span style=\"color: #333333;\">=<\/span><span style=\"color: #0000dd; font-weight: bold;\">2<\/span>, column<span style=\"color: #333333;\">=<\/span><span style=\"color: #0000dd; font-weight: bold;\">0<\/span>, pady<span style=\"color: #333333;\">=<\/span><span style=\"color: #0000dd; font-weight: bold;\">10<\/span>)\r\n\r\nbutton_divide <span style=\"color: #333333;\">=<\/span> tk<span style=\"color: #333333;\">.<\/span>Button(signs_frame, text<span style=\"color: #333333;\">=<\/span><span style=\"background-color: #fff0f0;\">'\/'<\/span>, height<span style=\"color: #333333;\">=<\/span><span style=\"color: #0000dd; font-weight: bold;\">3<\/span>, width<span style=\"color: #333333;\">=<\/span><span style=\"color: #0000dd; font-weight: bold;\">3<\/span>)\r\nbutton_divide<span style=\"color: #333333;\">.<\/span>grid(row<span style=\"color: #333333;\">=<\/span><span style=\"color: #0000dd; font-weight: bold;\">3<\/span>, column<span style=\"color: #333333;\">=<\/span><span style=\"color: #0000dd; font-weight: bold;\">0<\/span>)\r\n<\/pre>\n<p>&nbsp;<\/p>\n<h3 id=\"t5\">5. Change the Font Sizes<\/h3>\n<p>Next, the font sizes would have to be increased both for the screen and for the keypad.<\/p>\n<p>For this screen, we want the font size to be 30px. Then it would be single-line display and a font type of &#8216;Helvetica&#8217;. This is achieved using the height and the font attribute of the Text object. The code is given below:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"> height<span style=\"color: #333333;\">=<\/span><span style=\"color: #0000dd; font-weight: bold;\">1<\/span>, font<span style=\"color: #333333;\">=<\/span>(<span style=\"background-color: #fff0f0;\">\"Helvetica\"<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">30<\/span>)\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>Next, to change the font styles for all the buttons, we can use two loop. For each of the two frames: numbers_frame and signs_frame, we would loop through all the buttons inside and apply the same style to each.<\/p>\n<p>The code is given below:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #888888;\"># Apply Styles to the buttons<\/span>\r\nbutton_font <span style=\"color: #333333;\">=<\/span> font<span style=\"color: #333333;\">.<\/span>Font(size<span style=\"color: #333333;\">=<\/span><span style=\"color: #0000dd; font-weight: bold;\">20<\/span>, weight<span style=\"color: #333333;\">=<\/span><span style=\"background-color: #fff0f0;\">'bold'<\/span>, family<span style=\"color: #333333;\">=<\/span><span style=\"background-color: #fff0f0;\">'Helvetica'<\/span>)\r\n<span style=\"color: #008800; font-weight: bold;\">for<\/span> button <span style=\"color: #000000; font-weight: bold;\">in<\/span> numbers_frame<span style=\"color: #333333;\">.<\/span>winfo_children():\r\n    button[<span style=\"background-color: #fff0f0;\">'font'<\/span>] <span style=\"color: #333333;\">=<\/span> button_font\r\n\r\n<span style=\"color: #008800; font-weight: bold;\">for<\/span> button <span style=\"color: #000000; font-weight: bold;\">in<\/span> signs_frame<span style=\"color: #333333;\">.<\/span>winfo_children():\r\n    button[<span style=\"background-color: #fff0f0;\">'font'<\/span>] <span style=\"color: #333333;\">=<\/span> button_font\r\n<\/pre>\n<p>&nbsp;<\/p>\n<h3 id=\"t6\">6. Display Selected Number on Screen<\/h3>\n<p>We need to write a function that executes when a button is clicked. In this case, when a number is clicked, we would take the clicked number and display it on the screen.\u00a0 The function is given below. It takes a parameter, and inserts the given argument into the display_box(screen). Note that the text is appended to existing text on the display.<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008800; font-weight: bold;\">def<\/span> <span style=\"color: #0066bb; font-weight: bold;\">show<\/span>(num):\r\n    display_box<span style=\"color: #333333;\">.<\/span>insert(END, num)\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>Next you will need to add this function to the number button using the command attribute.\u00a0 So the change to the button would be as shown:<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<pre style=\"margin: 0; line-height: 125%;\">command<span style=\"color: #333333;\">=<\/span><span style=\"color: #008800; font-weight: bold;\">lambda<\/span> num<span style=\"color: #333333;\">=<\/span><span style=\"background-color: #fff0f0;\">'1'<\/span>: show(<span style=\"background-color: #fff0f0;\">'1'<\/span>)\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>The Clear Entry (CE) button<\/strong><\/p>\n<p>You use this button the clear the content of the display. The code for it is shown below:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008800; font-weight: bold;\">def<\/span> <span style=\"color: #0066bb; font-weight: bold;\">clear<\/span>():\r\n    display_box<span style=\"color: #333333;\">.<\/span>delete(<span style=\"background-color: #fff0f0;\">'1.0'<\/span>, END)\r\n<\/pre>\n<p>And you also need to add to the command to the CE button as well:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">command<span style=\"color: #333333;\">=<\/span><span style=\"color: #008800; font-weight: bold;\">lambda<\/span>: clear()\r\n<\/pre>\n<p>You can then repeat this for all other numbers. When you are done, run the application an click on each of the number to see that it appears.<\/p>\n<p>&nbsp;<\/p>\n<h3 id=\"t7\">7. Coding the Assign Key<\/h3>\n<p>This are the keys for the operators: add, subtract, multiply and divide. This button is clicked after the user has entered the first number. When this key is clicked, two things happen:<\/p>\n<ol>\n<li>take number on the screen and assign it to a variable (num1)<\/li>\n<li>determine the sign that was clicked and assign it to the variable op (for operator)<\/li>\n<li>clear the display so the the user can enter the second operand<\/li>\n<\/ol>\n<p>Also note that these variables are global variables. So they be used later when the EQUALS (=) button is clicked. So we first declare them at the beginning of the program as shown here:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008800; font-weight: bold;\">global<\/span> num1\r\n<span style=\"color: #008800; font-weight: bold;\">global<\/span> num2\r\n<span style=\"color: #008800; font-weight: bold;\">global<\/span> op\r\n<\/pre>\n<p>The calculate function is given below:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008800; font-weight: bold;\">def<\/span> <span style=\"color: #0066bb; font-weight: bold;\">assign<\/span>(sign):\r\n    <span style=\"color: #008800; font-weight: bold;\">global<\/span> num1\r\n    <span style=\"color: #008800; font-weight: bold;\">global<\/span> op\r\n    op <span style=\"color: #333333;\">=<\/span> sign\r\n    num1 <span style=\"color: #333333;\">=<\/span> display_box<span style=\"color: #333333;\">.<\/span>get(<span style=\"background-color: #fff0f0;\">'1.0'<\/span>, <span style=\"background-color: #fff0f0;\">'end-1c'<\/span>)\r\n    clear()\r\n<\/pre>\n<p>&nbsp;<\/p>\n<h3 id=\"t8\">8. Coding the Calculate Key<\/h3>\n<p>The calculate key would perform the calculation and display the result on the screen.<\/p>\n<p>The calculate function performs the following actions:<\/p>\n<ol>\n<li>read the number on the screen into the second variable num2<\/li>\n<li>determine the operation to perform<\/li>\n<li>fetch the first operand from the global variable, num1<\/li>\n<li>perform the operation<\/li>\n<li>display the result on the screen<\/li>\n<\/ol>\n<p>The calculate function is given below:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008800; font-weight: bold;\">def<\/span> <span style=\"color: #0066bb; font-weight: bold;\">calculate<\/span>():\r\n    <span style=\"color: #008800; font-weight: bold;\">global<\/span> num2\r\n    <span style=\"color: #008800; font-weight: bold;\">global<\/span> op\r\n    num2 <span style=\"color: #333333;\">=<\/span> display_box<span style=\"color: #333333;\">.<\/span>get(<span style=\"background-color: #fff0f0;\">'1.0'<\/span>, <span style=\"background-color: #fff0f0;\">'end-1c'<\/span>)\r\n    result <span style=\"color: #333333;\">=<\/span> <span style=\"color: #0000dd; font-weight: bold;\">0<\/span>\r\n\r\n    <span style=\"color: #888888;\"># Switch the op to determine which operation to perform<\/span>\r\n    <span style=\"color: #008800; font-weight: bold;\">if<\/span> op <span style=\"color: #333333;\">==<\/span> <span style=\"background-color: #fff0f0;\">'+'<\/span>:\r\n        result <span style=\"color: #333333;\">=<\/span> <span style=\"color: #007020;\">float<\/span>(num1) <span style=\"color: #333333;\">+<\/span> <span style=\"color: #007020;\">float<\/span>(num2)\r\n    <span style=\"color: #008800; font-weight: bold;\">elif<\/span> op <span style=\"color: #333333;\">==<\/span> <span style=\"background-color: #fff0f0;\">'-'<\/span>:\r\n        result <span style=\"color: #333333;\">=<\/span> <span style=\"color: #007020;\">float<\/span>(num1) <span style=\"color: #333333;\">-<\/span> <span style=\"color: #007020;\">float<\/span>(num2)\r\n    <span style=\"color: #008800; font-weight: bold;\">elif<\/span> op <span style=\"color: #333333;\">==<\/span> <span style=\"background-color: #fff0f0;\">'*'<\/span>:\r\n        result <span style=\"color: #333333;\">=<\/span> <span style=\"color: #007020;\">float<\/span>(num1) <span style=\"color: #333333;\">*<\/span> <span style=\"color: #007020;\">float<\/span>(num2)\r\n    <span style=\"color: #008800; font-weight: bold;\">elif<\/span> op <span style=\"color: #333333;\">==<\/span> <span style=\"background-color: #fff0f0;\">'\/'<\/span>:\r\n        result <span style=\"color: #333333;\">=<\/span> <span style=\"color: #007020;\">float<\/span>(num1) <span style=\"color: #333333;\">\/<\/span> <span style=\"color: #007020;\">float<\/span>(num2)\r\n    clear()\r\n    display_box<span style=\"color: #333333;\">.<\/span>insert(END, result)\r\n<\/pre>\n<p>&nbsp;<\/p>\n<h3 id=\"t9\">9. Further Enhancements<\/h3>\n<ul>\n<li>Erasing figures from displayed values<\/li>\n<li>Handling decimal numbers<\/li>\n<li>Displaying both the operands and operators on screen with the result<\/li>\n<li>Validate inputs<\/li>\n<\/ul>\n<p>These enhancements and more would be covered in the next part. Then we would move on to Scientific Calculator!<\/p>\n<p><a href=\"https:\/\/youtu.be\/NY0gxvtOY8o?si=MFsf-_jtfFl0JkVC\" target=\"_blank\" rel=\"noopener\">Video Tutorial &#8211; How to Build a GUI Calculator in Python<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, you will learn how to create a calculator application in Python using Tkinter module. Plan Your Calculator Layout Code and Test the &hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_mi_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0},"categories":[7],"tags":[],"_links":{"self":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/1806"}],"collection":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/comments?post=1806"}],"version-history":[{"count":8,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/1806\/revisions"}],"predecessor-version":[{"id":1816,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/1806\/revisions\/1816"}],"wp:attachment":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/media?parent=1806"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/categories?post=1806"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/tags?post=1806"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}