<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Annie Fernandez</title>
    <description>The latest articles on DEV Community by Annie Fernandez (@annie_fernandez).</description>
    <link>https://dev.to/annie_fernandez</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1647100%2F99d23772-1cea-4dfc-9a45-5273fa29d437.png</url>
      <title>DEV Community: Annie Fernandez</title>
      <link>https://dev.to/annie_fernandez</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/annie_fernandez"/>
    <language>en</language>
    <item>
      <title>Coding a Simple Python Timer with Tkinter</title>
      <dc:creator>Annie Fernandez</dc:creator>
      <pubDate>Mon, 16 Sep 2024 12:13:31 +0000</pubDate>
      <link>https://dev.to/annie_fernandez/coding-a-simple-python-timer-with-tkinter-3bn</link>
      <guid>https://dev.to/annie_fernandez/coding-a-simple-python-timer-with-tkinter-3bn</guid>
      <description>&lt;p&gt;When I talked to my friend, a professional Python developer, about different ways of coding apps with a GUI and mentioned &lt;code&gt;tkinter&lt;/code&gt;, he laughed at me, saying that this framework is too primitive. Sure, for specialists building Django, Flask, and APIs, apps made with &lt;code&gt;tkinter&lt;/code&gt; might seem like a school project. However, this is a great solution if you are not a full-stack snob but need a quick yet scalable way to code any GUI app in minutes.&lt;/p&gt;

&lt;p&gt;As a Python lover, I enjoy experimenting with coding different things, from statistical analytics to OCR programs that capture text from images. &lt;br&gt;
Recently, I encountered the concept of time tracking in my work and decided to build my own Python timer.&lt;/p&gt;

&lt;p&gt;The prototype &lt;a href="//traqq.com"&gt;employee time tracker&lt;/a&gt; I drew inspiration from is a powerful solution with servers, databases, multi-layer security, and, of course, a strong development team. So, let’s see what &lt;code&gt;tkinter&lt;/code&gt; can do about it.&lt;/p&gt;

&lt;h2&gt;
  
  
  How to make a timer in Python
&lt;/h2&gt;

&lt;p&gt;First, let's take a look at what the original app timeline looks like:&lt;br&gt;
&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fp3dwqt2hs3ha6kzljwwz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fp3dwqt2hs3ha6kzljwwz.png" alt="Image description" width="800" height="644"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now, see what Python timer I created:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F24mjdvcwq6na7u6ysf68.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F24mjdvcwq6na7u6ysf68.png" alt="Image description" width="800" height="514"&gt;&lt;/a&gt;&lt;br&gt;
There's no advanced functionality, but literally three options:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Launching time tracking&lt;/li&gt;
&lt;li&gt;Pausing &lt;/li&gt;
&lt;li&gt;Stop timer with log
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import time
import tkinter as tk
from tkinter import messagebox

class SimpleTimeTracker:
    def __init__(self, root):
        self.start_time = None
        self.paused_time = 0
        self.running = False
        self.paused = False
        self.update_interval = 1000  # Update every 1000 ms (1 second)
        self.root = root

        # Create GUI components
        self.root.title("Simple Time Tracker")

        self.start_button = tk.Button(self.root, text="Start", command=self.start)
        self.start_button.pack(pady=10)

        self.pause_button = tk.Button(self.root, text="Pause", command=self.pause)
        self.pause_button.pack(pady=10)

        self.stop_button = tk.Button(self.root, text="Stop", command=self.stop)
        self.stop_button.pack(pady=10)

        self.time_label = tk.Label(self.root, text="Elapsed Time: 0.00 seconds")
        self.time_label.pack(pady=10)

        self.timeline_label = tk.Label(self.root, text="Tracked Times:")
        self.timeline_label.pack(pady=10)

        self.timeline_listbox = tk.Listbox(self.root)
        self.timeline_listbox.pack(pady=10, fill=tk.BOTH, expand=True)

    def start(self):
        if not self.running:
            self.start_time = time.time() - self.paused_time
            self.running = True
            self.paused = False
            self.update_timer()
            self.time_label.config(text="Elapsed Time: 0.00 seconds")

    def pause(self):
        if not self.running:
            return

        if self.paused:
            self.start_time = time.time() - self.paused_time
            self.paused = False
            self.update_timer()
        else:
            self.paused_time = time.time() - self.start_time
            self.paused = True

    def stop(self):
        if not self.running:
            messagebox.showerror("Error", "Time tracking has not been started.")
            return

        if self.paused:
            elapsed_time = self.paused_time
        else:
            self.end_time = time.time()
            elapsed_time = self.end_time - self.start_time

        formatted_time = f"Start: {time.ctime(self.start_time)}, End: {time.ctime(time.time())}, Elapsed: {elapsed_time:.2f} seconds"
        self.timeline_listbox.insert(tk.END, formatted_time)

        self.running = False
        self.paused = False

        self.time_label.config(text=f"Elapsed Time: {elapsed_time:.2f} seconds")
        self.paused_time = 0
        self.reset()

    def update_timer(self):
        if self.running and not self.paused:
            elapsed_time = time.time() - self.start_time
            self.time_label.config(text=f"Elapsed Time: {elapsed_time:.2f} seconds")
            self.root.after(self.update_interval, self.update_timer)

    def reset(self):
        self.start_time = None
        self.paused_time = 0

if __name__ == "__main__":
    root = tk.Tk()
    root.geometry("400x400")
    tracker = SimpleTimeTracker(root)
    root.mainloop()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Try this out for yourself. I launched the tracker only in my Jupyter Lab to experiment with it. And yes, the timer I coded was built with the help of ChatGPT. It took me just four prompts to refine it to its current state.&lt;/p&gt;

&lt;p&gt;When I started coding about five years ago, I used to spend hours creating anything in &lt;code&gt;tkinter&lt;/code&gt;. I have built dozens of simple apps, and now I know how to interact with ChatGPT to get the results I want.&lt;/p&gt;

&lt;p&gt;Keep it easy, and never be afraid of experimenting - because Python has no limits!&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>python</category>
      <category>beginners</category>
      <category>programming</category>
    </item>
  </channel>
</rss>
