{"id":24,"date":"2025-12-05T14:38:28","date_gmt":"2025-12-05T14:38:28","guid":{"rendered":"https:\/\/theroyalscode.com\/students\/e_henderson\/?p=24"},"modified":"2025-12-05T14:38:28","modified_gmt":"2025-12-05T14:38:28","slug":"free-friday-12-5-25","status":"publish","type":"post","link":"https:\/\/theroyalscode.com\/students\/e_henderson\/2025\/12\/05\/free-friday-12-5-25\/","title":{"rendered":"Free friday 12\/5\/25"},"content":{"rendered":"\n<p>So, for this free Friday, i decided to work on a calculator like I wanted for a while now.<br><\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"454\" height=\"231\" src=\"https:\/\/theroyalscode.com\/students\/e_henderson\/wp-content\/uploads\/2025\/12\/image.png\" alt=\"\" class=\"wp-image-25\" srcset=\"https:\/\/theroyalscode.com\/students\/e_henderson\/wp-content\/uploads\/2025\/12\/image.png 454w, https:\/\/theroyalscode.com\/students\/e_henderson\/wp-content\/uploads\/2025\/12\/image-300x153.png 300w\" sizes=\"auto, (max-width: 454px) 100vw, 454px\" \/><\/figure>\n\n\n\n<p>I tried something new following the video call tkinter<br>tkinter is a python GUI, very helpful for creating things that require a Gui<\/p>\n\n\n\n<figure class=\"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<div class=\"ast-oembed-container \" style=\"height: 100%;\"><iframe loading=\"lazy\" title=\"Calculator with GUI Using Python Tkinter (Less Than 200 lines of Code)\" width=\"500\" height=\"281\" src=\"https:\/\/www.youtube.com\/embed\/QZPv1y2znZo?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe><\/div>\n<\/div><\/figure>\n\n\n\n<pre class=\"wp-block-code\"><code>class Calculator:\n    def __init__(self):\n        self.window = tk.Tk()\n        self.window.geometry(\"375x667\")\n        self.window.resizable(0, 0)\n        self.window.title(\"Calculator\")\n\n        self.total_expression = \"\"\n        self.current_expression = \"\"\n        self.display_frame = self.create_display_frame()\n\n        self.total_label, self.label = self.create_display_labels()\n\n        self.digits = {\n            7: (1, 1), 8: (1, 2), 9: (1, 3),\n            4: (2, 1), 5: (2, 2), 6: (2, 3),\n            1: (3, 1), 2: (3, 2), 3: (3, 3),\n            0: (4, 2), '.': (4, 1)\n        }\n        self.operations = {\"\/\": \"\\u00F7\", \"*\": \"\\u00D7\", \"-\": \"-\", \"+\": \"+\"}\n        self.buttons_frame = self.create_buttons_frame()\n\n        self.buttons_frame.rowconfigure(0, weight=1)\n        for x in range(1, 5):\n            self.buttons_frame.rowconfigure(x, weight=1)\n            self.buttons_frame.columnconfigure(x, weight=1)\n        self.create_digit_buttons()\n        self.create_operator_buttons()\n        self.create_special_buttons()\n        self.bind_keys()\n\n    def bind_keys(self):\n        self.window.bind(\"&lt;Return>\", lambda event: self.evaluate())\n        for key in self.digits:\n            self.window.bind(str(key), lambda event, digit=key: self.add_to_expression(digit))\n\n        for key in self.operations:\n            self.window.bind(key, lambda event, operator=key: self.append_operator(operator))\n\n    def create_special_buttons(self):\n        self.create_clear_button()\n        self.create_equals_button()\n        self.create_square_button()\n        self.create_sqrt_button()\n\n    def create_display_labels(self):\n        total_label = tk.Label(self.display_frame, text=self.total_expression, anchor=tk.E, bg=LIGHT_GRAY,\n                               fg=LABEL_COLOR, padx=24, font=SMALL_FONT_STYLE)\n        total_label.pack(expand=True, fill='both')\n\n        label = tk.Label(self.display_frame, text=self.current_expression, anchor=tk.E, bg=LIGHT_GRAY,\n                         fg=LABEL_COLOR, padx=24, font=LARGE_FONT_STYLE)\n        label.pack(expand=True, fill='both')\n\n        return total_label, label\n\n    def create_display_frame(self):\n        frame = tk.Frame(self.window, height=221, bg=LIGHT_GRAY)\n        frame.pack(expand=True, fill=\"both\")\n        return frame\n\n    def add_to_expression(self, value):\n        self.current_expression += str(value)\n        self.update_label()\n\n    def create_digit_buttons(self):\n        for digit, grid_value in self.digits.items():\n            button = tk.Button(self.buttons_frame, text=str(digit), bg=WHITE, fg=LABEL_COLOR, font=DIGITS_FONT_STYLE,\n                               borderwidth=0, command=lambda x=digit: self.add_to_expression(x))\n            button.grid(row=grid_value&#91;0], column=grid_value&#91;1], sticky=tk.NSEW)\n\n    def append_operator(self, operator):\n        self.current_expression += operator\n        self.total_expression += self.current_expression\n        self.current_expression = \"\"\n        self.update_total_label()\n        self.update_label()\n\n    def create_operator_buttons(self):\n        i = 0\n        for operator, symbol in self.operations.items():\n            button = tk.Button(self.buttons_frame, text=symbol, bg=OFF_WHITE, fg=LABEL_COLOR, font=DEFAULT_FONT_STYLE,\n                               borderwidth=0, command=lambda x=operator: self.append_operator(x))\n            button.grid(row=i, column=4, sticky=tk.NSEW)\n            i += 1\n\n    def clear(self):\n        self.current_expression = \"\"\n        self.total_expression = \"\"\n        self.update_label()\n        self.update_total_label()\n\n    def create_clear_button(self):\n        button = tk.Button(self.buttons_frame, text=\"C\", bg=OFF_WHITE, fg=LABEL_COLOR, font=DEFAULT_FONT_STYLE,\n                           borderwidth=0, command=self.clear)\n        button.grid(row=0, column=1, sticky=tk.NSEW)\n\n    def square(self):\n        self.current_expression = str(eval(f\"{self.current_expression}**2\"))\n        self.update_label()\n\n    def create_square_button(self):\n        button = tk.Button(self.buttons_frame, text=\"x\\u00b2\", bg=OFF_WHITE, fg=LABEL_COLOR, font=DEFAULT_FONT_STYLE,\n                           borderwidth=0, command=self.square)\n        button.grid(row=0, column=2, sticky=tk.NSEW)\n\n    def sqrt(self):\n        self.current_expression = str(eval(f\"{self.current_expression}**0.5\"))\n        self.update_label()\n\n    def create_sqrt_button(self):\n        button = tk.Button(self.buttons_frame, text=\"\\u221ax\", bg=OFF_WHITE, fg=LABEL_COLOR, font=DEFAULT_FONT_STYLE,\n                           borderwidth=0, command=self.sqrt)\n        button.grid(row=0, column=3, sticky=tk.NSEW)\n\n    def evaluate(self):\n        self.total_expression += self.current_expression\n        self.update_total_label()\n        try:\n            self.current_expression = str(eval(self.total_expression))\n\n            self.total_expression = \"\"\n        except Exception as e:\n            self.current_expression = \"Error\"\n        finally:\n            self.update_label()\n\n    def create_equals_button(self):\n        button = tk.Button(self.buttons_frame, text=\"=\", bg=LIGHT_BLUE, fg=LABEL_COLOR, font=DEFAULT_FONT_STYLE,\n                           borderwidth=0, command=self.evaluate)\n        button.grid(row=4, column=3, columnspan=2, sticky=tk.NSEW)\n\n    def create_buttons_frame(self):\n        frame = tk.Frame(self.window)\n        frame.pack(expand=True, fill=\"both\")\n        return frame\n\n    def update_total_label(self):\n        expression = self.total_expression\n        for operator, symbol in self.operations.items():\n            expression = expression.replace(operator, f' {symbol} ')\n        self.total_label.config(text=expression)\n\n    def update_label(self):\n        self.label.config(text=self.current_expression&#91;:11])\n\n    def run(self):\n        self.window.mainloop()\n<\/code><\/pre>\n\n\n\n<p>This is the code for the calculator itself.<br>I followed the video, it showed me the functions of a calculator, and how it&#8217;s supposed to update itself once you put 2 function (or equations) in.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"404\" height=\"702\" src=\"https:\/\/theroyalscode.com\/students\/e_henderson\/wp-content\/uploads\/2025\/12\/image-1.png\" alt=\"\" class=\"wp-image-26\" srcset=\"https:\/\/theroyalscode.com\/students\/e_henderson\/wp-content\/uploads\/2025\/12\/image-1.png 404w, https:\/\/theroyalscode.com\/students\/e_henderson\/wp-content\/uploads\/2025\/12\/image-1-173x300.png 173w\" sizes=\"auto, (max-width: 404px) 100vw, 404px\" \/><\/figure>\n\n\n\n<p>This is the calculator interface. (very cool and interesting)<\/p>\n\n\n\n<p>but yeah, simple.<br>Calculator \ud83d\ude0e<\/p>\n","protected":false},"excerpt":{"rendered":"<p>So, for this free Friday, i decided to work on a calculator like I wanted for a while now. I [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[1],"tags":[],"class_list":["post-24","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/theroyalscode.com\/students\/e_henderson\/wp-json\/wp\/v2\/posts\/24","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/theroyalscode.com\/students\/e_henderson\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/theroyalscode.com\/students\/e_henderson\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/theroyalscode.com\/students\/e_henderson\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/theroyalscode.com\/students\/e_henderson\/wp-json\/wp\/v2\/comments?post=24"}],"version-history":[{"count":1,"href":"https:\/\/theroyalscode.com\/students\/e_henderson\/wp-json\/wp\/v2\/posts\/24\/revisions"}],"predecessor-version":[{"id":27,"href":"https:\/\/theroyalscode.com\/students\/e_henderson\/wp-json\/wp\/v2\/posts\/24\/revisions\/27"}],"wp:attachment":[{"href":"https:\/\/theroyalscode.com\/students\/e_henderson\/wp-json\/wp\/v2\/media?parent=24"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/theroyalscode.com\/students\/e_henderson\/wp-json\/wp\/v2\/categories?post=24"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/theroyalscode.com\/students\/e_henderson\/wp-json\/wp\/v2\/tags?post=24"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}