Python Forum
set time & date by user in tkinter
Thread Rating:
  • 2 Vote(s) - 4.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
set time & date by user in tkinter
#1
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?
Reply
#2
Information 
(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 datetime
Then 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:
(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 datetime
Then 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 : - )
Reply
#3
(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 datetime
Then 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 datetime
Then 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 : - )

i want to set time & date myself(or the user that deals with the tkinter window). and then time & date change from the point that i have set it.now there are 2 issus...i use raspbian OS.
1- does this program do this really?
2-i think this program can not read this line
[if sys.platform=='linux2':
    print("linux")
    _linux_set_time(time_tuple)][/icode]
because it can not print "linux"
Reply
#4
1) Maybe. There's some text boxes, and they control the value of a different box.
2) idk what "linux2" is. Did you try changing it to just "linux"? Or, you know, you could print(sys.platform) to find out what your raspbian is calling itself.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to stop time counter in Tkinter LoneStar 1 4,298 Sep-11-2020, 08:56 PM
Last Post: Yoriz
  Convert combobox user input in to date with tkinter Ame 8 6,661 Jul-01-2020, 09:40 PM
Last Post: Yoriz
  How can a user send a message via Contact Form in tkinter karolp 0 2,319 Apr-08-2020, 08:00 PM
Last Post: karolp
  [Tkinter] How to adjust time - tkinter Ondrej 2 2,811 Jun-20-2019, 05:53 PM
Last Post: Yoriz

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020