{"id":393,"date":"2011-02-27T03:41:46","date_gmt":"2011-02-27T02:41:46","guid":{"rendered":"http:\/\/www.netquote.it\/nqmain\/?p=393"},"modified":"2011-02-27T03:51:53","modified_gmt":"2011-02-27T02:51:53","slug":"python-gui-calc","status":"publish","type":"post","link":"https:\/\/www.netquote.it\/nqmain\/2011\/02\/python-gui-calc\/","title":{"rendered":"Python GUI calc"},"content":{"rendered":"<p>Python GUI experiment. Simple calculator application made with Tk.<br \/>\nIt&#8217;s a first choice because it&#8217;s integrated into Python, simple to use and powerful enough for most cases. It&#8217;s also cross platform like an interpreter.<br \/>\nHowever, there are many other libraries for GUI:<br \/>\n<a href=\"http:\/\/wiki.python.org\/moin\/GuiProgramming\">http:\/\/wiki.python.org\/moin\/GuiProgramming<\/a><\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\n#!\/usr\/bin\/python\r\n\r\n# Python GUI calculator \r\n# (C) 2011 - netquote.it\r\n# Original code by Emanuele Chiabrera\r\n# Enhanced by Toropov Ivan \r\n\r\nfrom Tkinter import *\r\n\r\n# button text list\r\ncmdlst = &#x5B;'7', '8', '9', '+', '%',\r\n          '4', '5', '6', '-', '**',\r\n          '1', '2', '3', '*', '\/\/',\r\n          '.', '0', 'CL', '\/', '=']\r\n\r\nclass MyButton(Button):\r\n    \r\n    backref = None\r\n    \r\n    def Click(self):\r\n        # back reference\r\n        self.backref.BtnCmd(self&#x5B;&quot;text&quot;])\r\n\r\n\r\nclass CalcApp:\r\n\r\n    def __init__(self, master):\r\n        frame = Frame(master)\r\n        \r\n        self.textbox = Entry(width=30, takefocus=1)\r\n        self.textbox.pack(side=TOP)\r\n        self.textbox.focus_force()\r\n\r\n        self.buttons = &#x5B;]\r\n        for n, c in enumerate(cmdlst):\r\n            self.buttons.append(MyButton(frame, text=c, width=5))\r\n            self.buttons&#x5B;n]&#x5B;&quot;command&quot;] = self.buttons&#x5B;n].Click\r\n            self.buttons&#x5B;n].backref = self\r\n            self.buttons&#x5B;n].grid(row=n\/5, column=n%5)\r\n\r\n        frame.pack()\r\n\r\n    def BtnCmd(self, cmd):\r\n        if cmd == '=':\r\n            try:\r\n                res = eval(self.textbox.get())\r\n            except:\r\n                res = &quot;Error!&quot;\r\n            self.textbox.delete(0, END)\r\n            self.textbox.insert(0, str(res))\r\n        elif cmd == 'CL':\r\n            self.textbox.delete(0, END)\r\n        else:\r\n            self.textbox.insert(END, cmd)\r\n\r\nroot = Tk()\r\nroot.title(&quot;EWCalc&quot;)\r\n\r\ncalcapp = CalcApp(root)\r\nroot.mainloop()\r\n<\/pre>\n<p>What comes out:<\/p>\n<p><a href=\"http:\/\/www.netquote.it\/nqmain\/wp-content\/uploads\/PyCalc.jpg\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.netquote.it\/nqmain\/wp-content\/uploads\/PyCalc.jpg\" alt=\"\" title=\"PyCalc\" width=\"328\" height=\"264\" class=\"alignnone size-full wp-image-398\" srcset=\"https:\/\/www.netquote.it\/nqmain\/wp-content\/uploads\/PyCalc.jpg 328w, https:\/\/www.netquote.it\/nqmain\/wp-content\/uploads\/PyCalc-150x120.jpg 150w, https:\/\/www.netquote.it\/nqmain\/wp-content\/uploads\/PyCalc-300x241.jpg 300w\" sizes=\"auto, (max-width: 328px) 100vw, 328px\" \/><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python GUI experiment. Simple calculator application made with Tk. It&#8217;s a first choice because it&#8217;s integrated into Python, simple to<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-393","post","type-post","status-publish","format-standard","hentry","category-python"],"_links":{"self":[{"href":"https:\/\/www.netquote.it\/nqmain\/wp-json\/wp\/v2\/posts\/393","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.netquote.it\/nqmain\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.netquote.it\/nqmain\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.netquote.it\/nqmain\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.netquote.it\/nqmain\/wp-json\/wp\/v2\/comments?post=393"}],"version-history":[{"count":18,"href":"https:\/\/www.netquote.it\/nqmain\/wp-json\/wp\/v2\/posts\/393\/revisions"}],"predecessor-version":[{"id":410,"href":"https:\/\/www.netquote.it\/nqmain\/wp-json\/wp\/v2\/posts\/393\/revisions\/410"}],"wp:attachment":[{"href":"https:\/\/www.netquote.it\/nqmain\/wp-json\/wp\/v2\/media?parent=393"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.netquote.it\/nqmain\/wp-json\/wp\/v2\/categories?post=393"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.netquote.it\/nqmain\/wp-json\/wp\/v2\/tags?post=393"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}