Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for Build a Time Tracking App with Tkinter and Pygame
Florian Zeba
Florian Zeba

Posted on • Originally published atfzeba.com

Build a Time Tracking App with Tkinter and Pygame

Building a Time Tracking App with Tkinter and Pygame in Python

Creating a graphical user interface (GUI) for a time tracking app in Python can be accomplished using various libraries. Two popular choices are Tkinter, which is widely used for standard applications, and Pygame, which is generally utilized for game development but can also be adapted for other types of interactive applications. This article provides a comprehensive guide on how to use both to create a functional time tracking application.

Using Tkinter to Create a Time Tracking App

Tkinter is the standard GUI toolkit for Python. It provides a powerful object-oriented interface and is simple to learn and use.

Step 1: Setup

Before starting, make sure Python and Tkinter are installed. Tkinter is included with Python by default in most installations.

Step 2: Create the Main Window

Begin by importing Tkinter and setting up the main application window:

importtkinterastkfromtkinterimportttkroot=tk.Tk()root.title("Time Tracker")
Enter fullscreen modeExit fullscreen mode

Step 3: Design the GUI

Add essential GUI components. For a time tracker, you need a display for the time and buttons to control it:

timer_label=ttk.Label(root,text="00:00:00",font=("Segoe UI",30))timer_label.pack(pady=20)start_button=ttk.Button(root,text="Start",command=lambda:start_timer())start_button.pack(side=tk.LEFT,padx=20)stop_button=ttk.Button(root,text="Stop",command=lambda:stop_timer())stop_button.pack(side=tk.RIGHT,padx=20)
Enter fullscreen modeExit fullscreen mode

Step 4: Add Functionality

Define the functions that will start, stop, and update the timer:

running=Falsestart_time=0defupdate_timer():ifrunning:elapsed_time=time.time()-start_timeminutes,seconds=divmod(int(elapsed_time),60)hours,minutes=divmod(minutes,60)timer_label.config(text=f"{hours:02}:{minutes:02}:{seconds:02}")root.after(1000,update_timer)defstart_timer():globalrunning,start_timeifnotrunning:running=Truestart_time=time.time()update_timer()defstop_timer():globalrunning,start_timerunning=Falsestart_time=0timer_label.config(text="00:00:00")
Enter fullscreen modeExit fullscreen mode

Step 5: Main Loop

Keep the window open and responsive:

root.mainloop()
Enter fullscreen modeExit fullscreen mode

Using Pygame as an Alternative

Pygame offers more flexibility in terms of graphics and animations, making it a viable alternative for building a time tracking app, especially if you want custom visuals.

Step 1: Install and Import Pygame

Ensure Pygame is installed using pip (pip install pygame\) and then import it:

importpygameimportsysfrompygame.localsimport*importtime
Enter fullscreen modeExit fullscreen mode

Step 2: Initialize Pygame

Set up the main display and clock:

pygame.init()fps_clock=pygame.time.Clock()width,height=400,200screen=pygame.display.set_mode((width,height))pygame.display.set_caption('Time Tracker')
Enter fullscreen modeExit fullscreen mode

Step 3: Setup Display and Fonts

Define your display parameters and fonts:

black=(0,0,0)white=(255,255,255)font=pygame.font.Font(None,36)
Enter fullscreen modeExit fullscreen mode

Step 4: Timer Logic

Initialize timer variables and define the timer functions similar to Tkinter:

start_time=Noneelapsed_time=0running=Falsedefstart_timer():globalrunning,start_timeifnotrunning:running=Truestart_time=time.time()-elapsed_timedefstop_timer():globalrunning,elapsed_timeifrunning:running=Falseelapsed_time=time.time()-start_time
Enter fullscreen modeExit fullscreen mode

Step 5: Main Event Loop

Create the loop to handle events and update the display:

whileTrue:screen.fill(black)foreventinpygame.event.get():ifevent.type==QUIT:pygame.quit()sys.exit()elifevent.type==KEYDOWN:ifevent.key==K_s:start_timer()elifevent.key==K_p:stop_timer()ifrunning:current_time=time.time()elapsed_time=current_time-start_timemins,secs=divmod(elapsed_time,60)hrs,mins=divmod(mins,60)time_string="%d:%02d:%02d"%(hrs,mins,secs)else:mins,secs=divmod(elapsed_time,60)hrs,mins=divmod(mins,60)time_string="%d:%02d:%02d"%(hrs,mins,secs)time_text=font.render(time_string,True,white)time_rect=time_text.get_rect()time_rect.center=(width//2,height//2)screen.blit(time_text,time_rect)pygame.display.update()fps_clock.tick(fps)
Enter fullscreen modeExit fullscreen mode

Conclusion

In summary, both Tkinter and Pygame provide robust frameworks for developing a time tracking application with graphical user interfaces in Python. While Tkinter is more straightforward and suitable for typical application interfaces, Pygame offers enhanced control over graphic elements and animations, which can be particularly beneficial for applications requiring dynamic visual displays. The selection between these two should be based on the specific requirements and objectives of the project, as well as the developer's familiarity with the libraries.

Readthis article and more onfzeba.com.

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Worked and studied in the fields of management consulting, finance, marketing, IT, mechatronics and law. Lead two web-agencys. Niched in on tax-technology.
  • Location
    Austria
  • Education
    MSc Computer Science
  • Joined

More fromFlorian Zeba

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp