Related Tutorial with source code
Stop watch GUI (for counting time): http://ygchan.blogspot.com/2012/05/python-stop-watch-timer-source-code.html
import Tkinter as tk import time def update_timeText(): # Get the current time, note you can change the format as you wish current = time.strftime("%H:%M:%S") # Update the timeText Label box with the current time timeText.configure(text=current) # Call the update_timeText() function after 1 second root.after(1000, update_timeText) root = tk.Tk() root.wm_title("Simple Clock Example") # Create a timeText Label (a text box) timeText = tk.Label(root, text="", font=("Helvetica", 150)) timeText.pack() update_timeText() root.mainloop()Reference: http://docs.python.org/library/time.html#time.strftime
Reference: http://docs.python.org/library/string.html#formatstrings
Can you explain how to add a time widget where u ask user to set a time . Basically grab a input from user where u can check for the system time and user input time to do a action..
ReplyDeleteTry to see if this work, it is sort of a hack.
Delete# For Python 2.x use "Tkinter"
# import Tkinter as tk
# For Python 3.x use "tkinter"
import tkinter as tk
import datetime
# Get the current time
myTime = datetime.datetime.now()
# Note it does not check if the time is valid!
def getText():
global myTime
myString = inputBox.get()
# Did not validate all the cases for the input
# The time needs to be correct to work
if (len(myString) == 6):
# Remove the message and the buttons
simpleTitle.pack_forget()
inputBox.pack_forget()
button.pack_forget()
# Setup the new time to the user defined time
hour = int(myString[0:2])
minute = int(myString[2:4])
second = int(myString[4:6])
myTime = datetime.datetime(2000, 1, 1, hour, minute, second)
def update_timeText():
global myTime
# Increment the time by 1 second every frame
myTime += datetime.timedelta(seconds=1)
current = myTime.strftime("%H:%M:%S")
# Update the timeText Label box with the current time
timeText.configure(text=current)
# Call the update_timeText() function after 1 second
root.after(1000, update_timeText)
root = tk.Tk()
root.wm_title("Simple Clock Example")
# The message title
simpleTitle =tk.Label(root)
simpleTitle['text'] = "Please Enter the time:\
\nExample: 06:30:00am, Enter 063000\
\nExample: 08:30:00pm, Enter 203000"
simpleTitle.pack()
# The entry box widget
inputBox = tk.Entry(root)
inputBox.pack()
# The submit box widget
button = tk.Button(root, text='Submit', command=getText)
button.pack()
# Create a timeText Label (a text box)
timeText = tk.Label(root, text="", font=("Helvetica", 150))
timeText.pack()
update_timeText()
root.mainloop()
Thanks for sharing such a great blog Keep posting..
ReplyDeletePython Training
Python Course