{"id":45,"date":"2020-10-24T16:17:08","date_gmt":"2020-10-24T16:17:08","guid":{"rendered":"https:\/\/sharroj.co.uk\/?page_id=45"},"modified":"2020-10-26T18:25:31","modified_gmt":"2020-10-26T18:25:31","slug":"calculator-2","status":"publish","type":"page","link":"https:\/\/sharroj.co.uk\/?page_id=45","title":{"rendered":"Calculator 2"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">Adding functions to the buttons<\/h2>\n\n\n\n<p>Start by making  a copy of Calculator1 and name it Calculator2<\/p>\n\n\n\n<p>You should have what LOOKS like a calculator by now (but a very ugly one!  We&#8217;ll fix that later)<\/p>\n\n\n\n<p>What we need to do now is make it work!<\/p>\n\n\n\n<p>Doing calculations using tkinter windows and widgets is a bit complicated.  It uses its own special kind of variable to store the numbers, called IntVar()<\/p>\n\n\n\n<p>There are 3 numbers on our calculator &#8211; the two numbers we type in and the result of the calculation.<\/p>\n\n\n\n<p>So, AFTER you&#8217;ve set up the main_window but BEFORE you create all the widgets, you need to add lines like this:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>number_1 = IntVar()<\/li><li>number_2 = IntVar()<\/li><li>res = IntVar()<\/li><li># res is short for result (don&#8217;t need to write this line!!!)<\/li><\/ul>\n\n\n\n<p>Next, we need to link these variable names to the widgets on the calculator.  We already created <span class=\"has-inline-color has-accent-color\">ent_first_number<\/span> but you need to edit that line so that it looks like this<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>ent_first_number = Entry(main_window, <span class=\"has-inline-color has-accent-color\">textvariable=number_1<\/span>)<\/li><\/ul>\n\n\n\n<p>Now, whatever number is typed into that box will be known as number_1<\/p>\n\n\n\n<p>See if you can work out where to add textvariables for the other two IntVar() that we created.  If you get stuck, <a href=\"https:\/\/sharroj.co.uk\/?page_id=50\">click here<\/a><\/p>\n\n\n\n<p>Now that we have set those variables up, we can use them in the calculations.  We need one for each of the oeprations and we should add them near the start of the program, after the tkinter import.  This is what the add function should look like:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>def add_them():<ul><li>res.set(number_1.get() + number_2.get())<\/li><\/ul><\/li><\/ul>\n\n\n\n<p><span class=\"has-inline-color has-accent-color\">def<\/span> is short for <span class=\"has-inline-color has-accent-color\">define<\/span>.  When you type the colon (:) and press enter, the next line is set further in because this is the definition of the function.  When you&#8217;ve typed this line in, you need to press delete to move the cursor back to its normal position so the computer knows you&#8217;ve finished the function.<\/p>\n\n\n\n<p>basically we are saying &#8216;set the result to whatever number_1 is plus whatever number_2 is&#8217; but we have to use .set() and .get()  in tkinter<\/p>\n\n\n\n<p>You should be able to add similar functions for sub_them, multiply_them and share_them<\/p>\n\n\n\n<p>(multiply is &#8216;*&#8217; and share is &#8216;\/&#8217;)<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>Then, the last bit is easy.  We just need to add the name of the function to the button.  Here&#8217;s how to change btn_add<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li> btn_add = Button(main_window, <span class=\"has-inline-color has-accent-color\">command=add_them<\/span>)<\/li><\/ul>\n\n\n\n<p>You should be able to add commands to the other buttons too.  And &#8230; that&#8217;s it! You should now have a working calculator.  There are some things we still need to do to make it work properly but we&#8217;ll do that later.  For now, run the program and see if it works.  try to fix any errors yourself before calling on me!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Adding functions to the buttons Start by making a copy of Calculator1 and name it Calculator2 You should have what LOOKS like a calculator by now (but a very ugly one! We&#8217;ll fix that later) What we need to do now is make it work! Doing calculations using tkinter windows and widgets is a bit [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-45","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/sharroj.co.uk\/index.php?rest_route=\/wp\/v2\/pages\/45","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sharroj.co.uk\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/sharroj.co.uk\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/sharroj.co.uk\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/sharroj.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=45"}],"version-history":[{"count":6,"href":"https:\/\/sharroj.co.uk\/index.php?rest_route=\/wp\/v2\/pages\/45\/revisions"}],"predecessor-version":[{"id":64,"href":"https:\/\/sharroj.co.uk\/index.php?rest_route=\/wp\/v2\/pages\/45\/revisions\/64"}],"wp:attachment":[{"href":"https:\/\/sharroj.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=45"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}