0

I am starting with GUI in python with tkinter and I want to display current time as HH:MM:SS in the center of my windows (with big digits if possible). This is the app code: a basic full screen windows with a background picture.

import Tkinter as tk
from Tkinter import Tk, Frame, BOTH
import Tkinter
from PIL import Image, ImageTk
root = tk.Tk()
root.attributes('-fullscreen', 1)
im = Image.open('spring.png')
tkimage = ImageTk.PhotoImage(im)
myvar=Tkinter.Label(root,image = tkimage)
myvar.place(x=0, y=0, relwidth=1, relheight=1)
root.mainloop()

Edit: By current time I mean to keep updating the time as a clock.

martineau
  • 119,623
  • 25
  • 170
  • 301
user3833794
  • 15
  • 1
  • 2
  • 6

2 Answers2

0

When using place you can use relative coordinates. To place a widget in the center of another widget use relx and relx. You can tell it which widget to be relative to with the in_ parameter; by default it will be centered in its parent.

For example:

time = tk.Label(...)
time.place(in_=myvar, relx=.5, rely=.5)

Having the clock update has been asked many times on stackoverflow. Here's one example: How to create a timer using tkinter?

Community
  • 1
  • 1
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
  • hi Bryan thank you for the answer i went to see the example and as i said i'am just starting with GUI and Tkinter i don't know much about class also and i am wonrdering if there is any way to include the timer just with a function because i dont know how change the actual code to a class and include the timer.help me please. – user3833794 Jun 11 '16 at 19:33
-1

Add a compound parameter to your myvar object with value Tkinter.CENTER and also a textparameter with the time string. To create time string add

import time
time_string = time.strftime('%H:%M:%S')

Now your myvar will look like

myvar=Tkinter.Label(root,image = tkimage, text = time_string, compound = Tkinter.CENTER)

For further reference see: python time: https://docs.python.org/2/library/time.html#time.strftime

python tkinter label : http://effbot.org/tkinterbook/label.htm

Zeerorg
  • 39
  • 3
  • 1
    hi Zeerorg thanks you for help.i did what you say and it is working but it's not what i was expected.it printing the time when the code run..by "current time" i mean as a clock that update every second.python run mainloop() as and infinite loop.any idea to keep displaying and updating the current time? – user3833794 Jun 11 '16 at 14:58