{"id":219,"date":"2025-05-25T18:44:51","date_gmt":"2025-05-25T18:44:51","guid":{"rendered":"https:\/\/r0b3rt.com\/andr-web\/?p=219"},"modified":"2025-05-25T18:56:00","modified_gmt":"2025-05-25T18:56:00","slug":"python-stopwatch","status":"publish","type":"post","link":"http:\/\/r0b3rt.com\/andr-web\/python-stopwatch\/","title":{"rendered":"Python &#8211; Stopwatch"},"content":{"rendered":"<p><a href=\"https:\/\/www.essentialwidgets.com\/programs\/stopwatch.html\">HTML STOPWATCH<\/a><\/p>\n<p><pre><span style=\"font-size: 10pt;\">\r\nimport tkinter as tk\r\nfrom tkinter import simpledialog\r\n\r\nclass TimerApp:\r\n    def __init__(self, root):\r\n        self.root = root\r\n        self.root.title(\"Timer\")\r\n\r\n        self.count = 0.0  # Use float to accommodate tenths of a second\r\n        self.is_running = False\r\n        self.is_counting_up = True\r\n\r\n        self.label = tk.Label(root, text=str(self.count), font=(\"Helvetica\", 48))  # Increased font size to 48\r\n        self.label.pack(pady=20)\r\n\r\n        self.up_button = tk.Button(root, text=\"Count Up\", command=self.start_counting_up, font=(\"Helvetica\", 18))\r\n        self.up_button.pack(side=tk.LEFT, padx=20)\r\n\r\n        self.down_button = tk.Button(root, text=\"Count Down\", command=self.start_counting_down, font=(\"Helvetica\", 18))\r\n        self.down_button.pack(side=tk.RIGHT, padx=20)\r\n\r\n        self.stop_button = tk.Button(root, text=\"Stop\", command=self.stop_timer, font=(\"Helvetica\", 18))\r\n        self.stop_button.pack(pady=20)\r\n\r\n        self.update_timer()\r\n\r\n    def start_counting_up(self):\r\n        self.is_counting_up = True\r\n        self.is_running = True\r\n        self.count = 0.0  # Reset count\r\n        self.label.config(text=str(self.count))\r\n\r\n    def start_counting_down(self):\r\n        self.is_counting_up = False\r\n        self.is_running = True\r\n        # Ask user for countdown time in seconds\r\n        countdown_time = simpledialog.askfloat(\"Input\", \"Set countdown time in seconds:\", minvalue=0.1)\r\n        if countdown_time is not None:\r\n            self.count = countdown_time  # Set countdown start\r\n            self.label.config(text=str(self.count))\r\n\r\n    def stop_timer(self):\r\n        self.is_running = False\r\n\r\n    def update_timer(self):\r\n        if self.is_running:\r\n            if self.is_counting_up:\r\n                self.count += 0.1  # Increment by tenths of a second\r\n            else:\r\n                self.count -= 0.1  # Decrement by tenths of a second\r\n            \r\n            # Ensure count doesn't go below 0 for countdown\r\n            if not self.is_counting_up and self.count &lt; 0:\r\n                self.count = 0\r\n            \r\n            self.label.config(text=f\"{self.count:.1f}\")  # Format to one decimal place\r\n        \r\n        self.root.after(100, self.update_timer)  # Update every 100 milliseconds\r\n\r\nif __name__ == \"__main__\":\r\n    root = tk.Tk()\r\n    app = TimerApp(root)\r\n    root.mainloop()\r\n\r\n<\/span><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>HTML STOPWATCH import tkinter as tk from tkinter import simpledialog class TimerApp: def __init__(self, root): self.root = root self.root.title(&#8220;Timer&#8221;) self.count = 0.0 # Use float to accommodate tenths of a second self.is_running = False self.is_counting_up = True self.label = tk.Label(root, text=str(self.count), font=(&#8220;Helvetica&#8221;, 48)) # Increased font size to 48 self.label.pack(pady=20) self.up_button = tk.Button(root, text=&#8221;Count Up&#8221;, &hellip; <a href=\"http:\/\/r0b3rt.com\/andr-web\/python-stopwatch\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Python &#8211; Stopwatch&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[],"class_list":["post-219","post","type-post","status-publish","format-standard","hentry","category-writing-code"],"_links":{"self":[{"href":"http:\/\/r0b3rt.com\/andr-web\/wp-json\/wp\/v2\/posts\/219","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/r0b3rt.com\/andr-web\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/r0b3rt.com\/andr-web\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/r0b3rt.com\/andr-web\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/r0b3rt.com\/andr-web\/wp-json\/wp\/v2\/comments?post=219"}],"version-history":[{"count":4,"href":"http:\/\/r0b3rt.com\/andr-web\/wp-json\/wp\/v2\/posts\/219\/revisions"}],"predecessor-version":[{"id":227,"href":"http:\/\/r0b3rt.com\/andr-web\/wp-json\/wp\/v2\/posts\/219\/revisions\/227"}],"wp:attachment":[{"href":"http:\/\/r0b3rt.com\/andr-web\/wp-json\/wp\/v2\/media?parent=219"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/r0b3rt.com\/andr-web\/wp-json\/wp\/v2\/categories?post=219"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/r0b3rt.com\/andr-web\/wp-json\/wp\/v2\/tags?post=219"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}