Introduction
Basic Widgets
Toplevel Widgets
Geometry Management
Binding Functions
Working with Images in Tkinter
Tkinter Advance
Applications and Projects
Creating a digital clock using tkinter
involves setting up a continuous loop that updates the displayed time every second. Below is a step-by-step tutorial on how to create a digital clock using tkinter
:
1. Import Required Libraries:
import tkinter as tk from time import strftime
2. Create the Main Application Window:
root = tk.Tk() root.title("Digital Clock")
3. Define the Function to Display the Time:
def time(): current_time = strftime('%H:%M:%S %p') # Get the current local time in 12-hour clock format with AM/PM label.config(text=current_time) label.after(1000, time) # Update the time on the label every 1000 milliseconds (i.e., 1 second)
4. Create a Label to Display the Time: You can style this label to make it look more like a digital clock:
label = tk.Label(root, font=('calibri', 40, 'bold'), background='purple', foreground='white')
Here, we've used the 'calibri' font with a size of 40 and set it to bold. We've also chosen purple as the background color and white for the text color. You can adjust these settings to fit your desired aesthetic.
5. Pack the Label and Call the time
Function:
label.pack(anchor='center') time() # This initializes the clock's time when the application starts
6. Run the Main Loop:
root.mainloop()
Complete Code:
import tkinter as tk from time import strftime def time(): current_time = strftime('%H:%M:%S %p') # Get the current local time in 12-hour clock format with AM/PM label.config(text=current_time) label.after(1000, time) # Update the time on the label every 1000 milliseconds (i.e., 1 second) root = tk.Tk() root.title("Digital Clock") label = tk.Label(root, font=('calibri', 40, 'bold'), background='purple', foreground='white') label.pack(anchor='center') time() # Initialize the clock's time when the application starts root.mainloop()
When you run the complete code, you'll get a tkinter
window displaying the current time, which will update every second. The aesthetics of the clock can be further customized using different fonts, colors, and other tkinter
widget options.
Python Tkinter clock example:
import tkinter as tk from time import strftime def update_time(): current_time = strftime('%H:%M:%S %p') label_time.config(text=current_time) root.after(1000, update_time) # Update time every 1000 milliseconds (1 second) root = tk.Tk() root.title("Digital Clock") label_time = tk.Label(root, font=('calibri', 40, 'bold'), background='black', foreground='white') label_time.pack(anchor='center') update_time() root.mainloop()
Tkinter clock widget for digital time:
import tkinter as tk from time import strftime class DigitalClock(tk.Label): def __init__(self, parent=None, *args, **kwargs): super().__init__(parent, *args, **kwargs) self['font'] = ('calibri', 40, 'bold') self['background'] = 'black' self['foreground'] = 'white' self.pack(anchor='center') self.update_time() def update_time(self): current_time = strftime('%H:%M:%S %p') self['text'] = current_time self.after(1000, self.update_time) root = tk.Tk() root.title("Digital Clock") digital_clock = DigitalClock(root) root.mainloop()
Tkinter clock with hours, minutes, and seconds:
import tkinter as tk from time import strftime class DigitalClock(tk.Label): def __init__(self, parent=None, *args, **kwargs): super().__init__(parent, *args, **kwargs) self['font'] = ('calibri', 40, 'bold') self['background'] = 'black' self['foreground'] = 'white' self.pack(anchor='center') self.update_time() def update_time(self): current_time = strftime('%H:%M:%S %p') self['text'] = current_time self.after(1000, self.update_time) root = tk.Tk() root.title("Digital Clock") digital_clock = DigitalClock(root) root.mainloop()