0

I want to put this digital clock:

import sys    
from tkinter import *
import time

root = Tk()
time1 = ''
clock = Label(root, font=('times', 20, 'bold'), bg='green')
clock.pack(fill=BOTH, expand=1)

def tick():
    global time1
    # get the current local time from the PC
    time2 = time.strftime('%H:%M:%S')
    # if time string has changed, update it
    if time2 != time1:
        time1 = time2
        clock.config(text=time2)
        # calls itself every 200 milliseconds
        # to update the time display as needed
        # could use >200 ms, but display gets jerky
    clock.after(200, tick)

tick()
root.mainloop(  )

in this status bar:

status = Label(mGui, text="v1.0", bd=1, relief=SUNKEN, anchor=W)
status.pack(side=BOTTOM, fill=X)

Is there a way to do that? Thanks everyone who want to help, I appreciate it :)

TheQuiteStupidMan
  • 45
  • 2
  • 4
  • 10
  • possible duplicate of [How to create a timer using tkinter](http://stackoverflow.com/questions/2400262/how-to-create-a-timer-using-tkinter) – nbro Jan 17 '15 at 18:51

3 Answers3

2

What is with the if statement? It is unnecessary as the clock.after statement is calling tick() directly within the clock.after() function, to which updates your time string.

import sys    
from Tkinter import *
import time

def tick():
    # get the current local time from the PC
    time_string = time.strftime('%H:%M:%S')
    # if time string has changed, update it
    clock.config(text=time_string)
    clock.after(200, tick)

root = Tk()
clock = Label(root, font=('times', 20, 'bold'), bg='green')
clock.grid(row=0, column=1) 
tick()
root.mainloop()

Also, remember to use Tkinter (Capital T) for Python 2.7 and tkinter (lowercase t) for Python 3.0.

sk11
  • 1,779
  • 1
  • 17
  • 29
user2473494
  • 71
  • 1
  • 3
0

Tkinter noob here, but i don't think you can put the clock label inside the status label. However you can put them side by side:

import sys    
from tkinter import *
import time

def tick():
    global time1
    # get the current local time from the PC
    time2 = time.strftime('%H:%M:%S')
    # if time string has changed, update it
    if time2 != time1:
        time1 = time2
        clock.config(text=time2)
        # calls itself every 200 milliseconds
        # to update the time display as needed
        # could use >200 ms, but display gets jerky
    clock.after(200, tick)

root = Tk()
time1 = ''

status = Label(root, text="v1.0", bd=1, relief=SUNKEN, anchor=W)
status.grid(row=0, column=0)

clock = Label(root, font=('times', 20, 'bold'), bg='green')
clock.grid(row=0, column=1) 

tick()
root.mainloop()
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
  • Why do you choose to update every 200ms? It is an absolute certainty the time will change only once a second since you're only changing to the granularity of seconds. – Bryan Oakley Apr 14 '13 at 13:30
0

Normally, I make a statusbar out of a frame, and then pack whatever things I want to display in that frame. For example, your clock could be packed on the right side, and your status label could be packed on the left. Then you can put the whole statusbar frame at the bottom of your GUI.

Normally I prefer to give examples using an object-oriented style, but here's an example adapted from the code in your question:

import sys    
from tkinter import *
import time

root = Tk()

statusbar = Frame(root)
statusbar.pack(side="bottom", fill="x", expand=False)

time1 = ''
clock = Label(root, font=('times', 20, 'bold'), bg='green')

def tick():
    global time1
    # get the current local time from the PC
    time2 = time.strftime('%H:%M:%S')
    # if time string has changed, update it
    if time2 != time1:
        time1 = time2
        clock.config(text=time2)
        # calls itself every 200 milliseconds
        # to update the time display as needed
        # could use >200 ms, but display gets jerky
    clock.after(200, tick)

tick()

status = Label(root, text="v1.0", bd=1, relief=SUNKEN, anchor=W)
status.pack(in_=statusbar, side=LEFT, fill=BOTH, expand=True)
clock.pack(in_=statusbar, side=RIGHT, fill=Y, expand=False)

root.mainloop(  )
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685