I want to create a python real-time clock using Tkinter. However, I do not know how to start.
Asked
Active
Viewed 885 times
1 Answers
1
This is how I made my clock, assuming you want digital:
# importing whole module
from tkinter import *
from tkinter.ttk import *
# importing strftime function to
# retrieve system's time
from time import strftime
# creating tkinter window
root = Tk()
root.title('Clock')
# This function is used to
# display time on the label
def time():
string = strftime('%H:%M:%S %p')
lbl.config(text = string)
lbl.after(1000, time)
# Styling the label widget so that clock
# will look more attractive
lbl = Label(root, font = ('calibri', 40, 'bold'),
background = 'purple',
foreground = 'white')
# Placing clock at the centre
# of the tkinter window
lbl.pack(anchor = 'center')
time()
mainloop()
If you wanted an analog clock, you could do this:
#list of imported modules
from tkinter import *
from math import cos, sin
from time import gmtime, sleep
#defining of secondary functions
def drawcircle(Alpha,Beta,Rayon,Couleur,can): #draw a circle base on center coord radius and color
x1,y1,x2,y2=Alpha-Rayon, Beta-Rayon, Alpha+Rayon, Beta+Rayon
can.create_oval(x1,y1,x2,y2,fill=Couleur)
def drawPetAig(CoordA, CoordZ, Taille, Omega, can): #function to drawn the second hand of the clock
Pi = 3.141592
Omega = ((Omega/60)+1)*30
can.create_line(CoordA + (Taille/3) * cos(Pi*(Omega/180)), CoordZ + (Taille/3) * sin(Pi*(Omega/180)), CoordA - (Taille/8) * cos(Pi*(Omega/180)), CoordZ - (Taille/8) * sin(Pi*(Omega/180)) )
def drawGrdAig(CoordA, CoordZ, Taille, Omega, can): #function to draw the minute hand, based on center coord and minutes.
Pi = 3.141592
Omega = (Omega-15)*6
can.create_line(CoordA + (Taille/1.5) * cos(Pi*(Omega/180)), CoordZ + (Taille/1.5) * sin(Pi*(Omega/180)), CoordA - (Taille/6) * cos(Pi*(Omega/180)), CoordZ - (Taille/6) * sin(Pi*(Omega/180)))
def drawSecAig(CoordA, CoordZ, Taille, Omega, can): #function to draw the hour hand
Pi = 3.141592
Omega = (Omega-15) *6
can.create_line(CoordA + (Taille/1.5) * cos(Pi*(Omega/180)), CoordZ + (Taille/1.5) * sin(Pi*(Omega/180)), CoordA - (Taille/6) * cos(Pi*(Omega/180)), CoordZ - (Taille/6) * sin(Pi*(Omega/180)), fill = "red")
def fondhorloge(CoordA, CoordZ, Taille, can1): #function drawing the backgroud of the clock
Pi = 3.141592
drawcircle(CoordA, CoordZ, Taille + (Taille/10), "grey6",can1) #drawing the surrounding of the clock
drawcircle(CoordA, CoordZ, Taille, "ivory3",can1)#backgroud
drawcircle(CoordA, CoordZ, Taille/80, "grey6",can1)#central point/needle articulation
can1.create_line(CoordA + (Taille - (Taille/15)), CoordZ, CoordA + (Taille - (Taille/5)), CoordZ) #drawing the N/S/E/W decorativ hour position
can1.create_line(CoordA, CoordZ + (Taille - (Taille/15)), CoordA, CoordZ + (Taille - (Taille/5)))
can1.create_line(CoordA - (Taille - (Taille/15)), CoordZ, CoordA - (Taille - (Taille/5)), CoordZ)
can1.create_line(CoordA, CoordZ - (Taille - (Taille/15)), CoordA, CoordZ - (Taille - (Taille/5)))
#here, this 4*2 line defined the position of the 8 intermediate line between the N/S/E/W decorativ line.
can1.create_line(CoordA + (Taille/1.05) * cos(Pi*(30/180)), CoordZ + (Taille/1.05) * sin(Pi*(30/180)), CoordA + (Taille/1.20) * cos(Pi*(30/180)), CoordZ + (Taille/1.20) * sin(Pi*(30/180)))
can1.create_line(CoordA + (Taille/1.05) * cos(Pi*(60/180)), CoordZ + (Taille/1.05) * sin(Pi*(60/180)), CoordA + (Taille/1.20) * cos(Pi*(60/180)), CoordZ + (Taille/1.20) * sin(Pi*(60/180)))
can1.create_line(CoordA - (Taille/1.05) * cos(Pi*(30/180)), CoordZ - (Taille/1.05) * sin(Pi*(30/180)), CoordA - (Taille/1.20) * cos(Pi*(30/180)), CoordZ - (Taille/1.20) * sin(Pi*(30/180)))
can1.create_line(CoordA - (Taille/1.05) * cos(Pi*(60/180)), CoordZ - (Taille/1.05) * sin(Pi*(60/180)), CoordA - (Taille/1.20) * cos(Pi*(60/180)), CoordZ - (Taille/1.20) * sin(Pi*(60/180)))
can1.create_line(CoordA + (Taille/1.05) * cos(Pi*(30/180)), CoordZ - (Taille/1.05) * sin(Pi*(30/180)), CoordA + (Taille/1.20) * cos(Pi*(30/180)), CoordZ - (Taille/1.20) * sin(Pi*(30/180)))
can1.create_line(CoordA + (Taille/1.05) * cos(Pi*(60/180)), CoordZ - (Taille/1.05) * sin(Pi*(60/180)), CoordA + (Taille/1.20) * cos(Pi*(60/180)), CoordZ - (Taille/1.20) * sin(Pi*(60/180)))
can1.create_line(CoordA - (Taille/1.05) * cos(Pi*(30/180)), CoordZ + (Taille/1.05) * sin(Pi*(30/180)), CoordA - (Taille/1.20) * cos(Pi*(30/180)), CoordZ + (Taille/1.20) * sin(Pi*(30/180)))
can1.create_line(CoordA - (Taille/1.05) * cos(Pi*(60/180)), CoordZ + (Taille/1.05) * sin(Pi*(60/180)), CoordA - (Taille/1.20) * cos(Pi*(60/180)), CoordZ + (Taille/1.20) * sin(Pi*(60/180)))
#PRINCIPLE FUNCTION (here the problem starts)
def HORLOGE1(Gamma, Pi, Epsylon):# draw a clock with the center position x/x = gamma/pi and the radius = epsylon
fondhorloge(Gamma, Pi, Epsylon, can1)# extracting time value
patate = gmtime()
heure = patate[3] + 1 # "+1" is changing time from english time to french time :P
minute = patate[4]
seconde = patate[5]
print(heure, minute, seconde) # a simple test to watch what the programm is doing (run it, and you'll see, that this test is done tausend time per second, that why I think the problem is here.)
drawPetAig(Gamma, Pi, Epsylon, minute, can1)
drawGrdAig(Gamma, Pi, Epsylon, minute, can1)
drawSecAig(Gamma, Pi, Epsylon, seconde, can1)
fen1.after(1000, lambda: HORLOGE1(250, 250, 200))
#execution of the main function
fen1 = Tk()
can1 = Canvas(fen1, bg="white", height=500, width=500)
can1.pack()
HORLOGE1(250, 250, 200)
fen1.mainloop()
fen1.destroy()
Nimantha
- 5,793
- 5
- 23
- 56
The Pilot Dude
- 1,798
- 2
- 4
- 22
-
Hello @The Pilot Dude, I have a couple questions. How could you make the digital and analog clock my time zone (Pacific Time) and also add tick marks for the seconds and label each hour? Help would be appreciated. Thanks! – Ender May 18 '22 at 01:22