Mar-07-2017, 01:10 PM
how can a user set time & date in a tkinter window...please give me a link...i searched but i didn't find a suitable content... i am looking for a general code regardless what is my code?
(Mar-07-2017, 01:10 PM)gray Wrote: [ -> ]how can a user set time & date in a tkinter window...please give me a link...i searched but i didn't find a suitable content... i am looking for a general code regardless what is my code?
import datetime datetime.datetime.now() datetime(2009, 1, 6, 15, 8, 24, 78915)And just the time:
datetime.datetime.time(datetime.datetime.now()) datetime.time(15, 8, 24, 78915)The same but slightly more compact:
datetime.datetime.now().time()See the documentation for more info.
from datetime import datetimeThen remove the leading datetime.
(Mar-10-2017, 09:51 PM)dudeisbrendan03 Wrote: [ -> ](Mar-07-2017, 01:10 PM)gray Wrote: [ -> ]how can a user set time & date in a tkinter window...please give me a link...i searched but i didn't find a suitable content... i am looking for a general code regardless what is my code?
import datetime datetime.datetime.now() datetime(2009, 1, 6, 15, 8, 24, 78915)And just the time:
datetime.datetime.time(datetime.datetime.now()) datetime.time(15, 8, 24, 78915)The same but slightly more compact:
datetime.datetime.now().time()See the documentation for more info.
To save typing, you can import the datetime object from the datetime module:
from datetime import datetimeThen remove the leading datetime.
from all the above.
Not very sure how this could be implemented into tkinter. I do have a mild idea, should work. Ill type it up and test it. Reply with the code IF it works dont get your hopes up. Hopefully you can beat me to it.
from Tkinter import * import datetime import sys master = Tk() e = Entry(master) e.pack() e.focus_set() def callback(): print e.get() b = Button(master, text="get", width=10, command=callback) b.pack() mainloop() e = Entry(master, width=50) e.pack() text = e.get() def makeentry(parent, caption, width=None, **options): Label(parent, text=caption).pack(side=LEFT) entry = Entry(parent, **options) if width: entry.config(width=width) entry.pack(side=LEFT) return entry hour = makeentry(parent, "Hour", 10) minute = makeentry(parent, "Minute", 10) second = makeentry(parent, "Second", 10) day = makeentry(parent, "Day", 10) month = makeentry(parent, "Month", 10) year = makeentry(parent, "Year", 10) millisecond = makeentry(parent, "Millisecond", 10) content = StringVar() entry = Entry(parent, text=caption, textvariable=content) text = content.get() content.set(text) #datetime.datetime.now() #datetime(year, 1, 6, 15, 8, 24, 78915) time_tuple = ( year, # Year month, # Month day, # Day hour, # Hour minute, # Minute second, # Second millisecond, # Millisecond ) def _win_set_time(time_tuple): import pywin32 # http://timgolden.me.uk/pywin32-docs/win32api__SetSystemTime_meth.html # pywin32.SetSystemTime(year, month , dayOfWeek , day , hour , minute , second , millseconds ) dayOfWeek = datetime.datetime(time_tuple).isocalendar()[2] pywin32.SetSystemTime( time_tuple[:2] + (dayOfWeek,) + time_tuple[2:]) def _linux_set_time(time_tuple): import ctypes import ctypes.util import time # /usr/include/linux/time.h: # # define CLOCK_REALTIME 0 CLOCK_REALTIME = 0 # /usr/include/time.h # # struct timespec # { # __time_t tv_sec; /* Seconds. */ # long int tv_nsec; /* Nanoseconds. */ # }; class timespec(ctypes.Structure): _fields_ = [("tv_sec", ctypes.c_long), ("tv_nsec", ctypes.c_long)] librt = ctypes.CDLL(ctypes.util.find_library("rt")) ts = timespec() ts.tv_sec = int( time.mktime( datetime.datetime( *time_tuple[:6]).timetuple() ) ) ts.tv_nsec = time_tuple[6] * 1000000 # Millisecond to nanosecond # http://linux.die.net/man/3/clock_settime librt.clock_settime(CLOCK_REALTIME, ctypes.byref(ts)) if sys.platform=='linux2': _linux_set_time(time_tuple) elif sys.platform=='win32': _win_set_time(time_tuple)
(Mar-10-2017, 10:42 PM)dudeisbrendan03 Wrote: [ -> ](Mar-07-2017, 01:10 PM)gray Wrote: [ -> ]how can a user set time & date in a tkinter window...please give me a link...i searched but i didn't find a suitable content... i am looking for a general code regardless what is my code?
import datetime datetime.datetime.now() datetime(2009, 1, 6, 15, 8, 24, 78915)And just the time:
datetime.datetime.time(datetime.datetime.now()) datetime.time(15, 8, 24, 78915)The same but slightly more compact:
datetime.datetime.now().time()See the documentation for more info.
To save typing, you can import the datetime object from the datetime module:
from datetime import datetimeThen remove the leading datetime.
from all the above.
Not very sure how this could be implemented into tkinter. I do have a mild idea, should work. Ill type it up and test it. Reply with the code IF it works dont get your hopes up. Hopefully you can beat me to it.
(Mar-10-2017, 09:51 PM)dudeisbrendan03 Wrote: [ -> ]import datetime datetime.datetime.now() datetime(2009, 1, 6, 15, 8, 24, 78915)And just the time:
datetime.datetime.time(datetime.datetime.now()) datetime.time(15, 8, 24, 78915)The same but slightly more compact:
datetime.datetime.now().time()See the documentation for more info.
To save typing, you can import the datetime object from the datetime module:
from datetime import datetimeThen remove the leading datetime.
from all the above.
Not very sure how this could be implemented into tkinter. I do have a mild idea, should work. Ill type it up and test it. Reply with the code IF it works dont get your hopes up. Hopefully you can beat me to it.
Originally I was going to just use datetime(valuea, valueb, valuec, valued)
But then I found a little snippet online I did some tkintering with (get it tinkering/tkintering. Its probably the worst joke you have ever heard) and I got this:
from Tkinter import * import datetime import sys master = Tk() e = Entry(master) e.pack() e.focus_set() def callback(): print e.get() b = Button(master, text="get", width=10, command=callback) b.pack() mainloop() e = Entry(master, width=50) e.pack() text = e.get() def makeentry(parent, caption, width=None, **options): Label(parent, text=caption).pack(side=LEFT) entry = Entry(parent, **options) if width: entry.config(width=width) entry.pack(side=LEFT) return entry hour = makeentry(parent, "Hour", 10) minute = makeentry(parent, "Minute", 10) second = makeentry(parent, "Second", 10) day = makeentry(parent, "Day", 10) month = makeentry(parent, "Month", 10) year = makeentry(parent, "Year", 10) millisecond = makeentry(parent, "Millisecond", 10) content = StringVar() entry = Entry(parent, text=caption, textvariable=content) text = content.get() content.set(text) #datetime.datetime.now() #datetime(year, 1, 6, 15, 8, 24, 78915) time_tuple = ( year, # Year month, # Month day, # Day hour, # Hour minute, # Minute second, # Second millisecond, # Millisecond ) def _win_set_time(time_tuple): import pywin32 # http://timgolden.me.uk/pywin32-docs/win32api__SetSystemTime_meth.html # pywin32.SetSystemTime(year, month , dayOfWeek , day , hour , minute , second , millseconds ) dayOfWeek = datetime.datetime(time_tuple).isocalendar()[2] pywin32.SetSystemTime( time_tuple[:2] + (dayOfWeek,) + time_tuple[2:]) def _linux_set_time(time_tuple): import ctypes import ctypes.util import time # /usr/include/linux/time.h: # # define CLOCK_REALTIME 0 CLOCK_REALTIME = 0 # /usr/include/time.h # # struct timespec # { # __time_t tv_sec; /* Seconds. */ # long int tv_nsec; /* Nanoseconds. */ # }; class timespec(ctypes.Structure): _fields_ = [("tv_sec", ctypes.c_long), ("tv_nsec", ctypes.c_long)] librt = ctypes.CDLL(ctypes.util.find_library("rt")) ts = timespec() ts.tv_sec = int( time.mktime( datetime.datetime( *time_tuple[:6]).timetuple() ) ) ts.tv_nsec = time_tuple[6] * 1000000 # Millisecond to nanosecond # http://linux.die.net/man/3/clock_settime librt.clock_settime(CLOCK_REALTIME, ctypes.byref(ts)) if sys.platform=='linux2': _linux_set_time(time_tuple) elif sys.platform=='win32': _win_set_time(time_tuple)
Oh and it requires pywin32.
If you dont have it:
https://sourceforge.net/projects/pywin32/
^Its a great tool
PythonWin is best interactive place for Windows that I know.
If you dont wont to use PythonWin I can just make a new script using datetime : - )
print(sys.platform)
to find out what your raspbian is calling itself.