Get the App
SLTechnology News&Howtos  ›  Development  › 

How to draw a digital clock by Python+Tkinter

Shulou Source: shulou.com Published: 2022-06-02 01:09:32 09月29日 Update

This "Python+Tkinter how to draw a digital clock" article, the article sample code introduction is very detailed, with a certain reference value, interested friends must refer to, for "Python+Tkinter how to draw a digital clock", Xiaobian sorted out the following knowledge points, please follow the pace of the editor step by step slowly understand, then let us enter the topic.

Tkinter to achieve the above functions is not complex, as long as the use of relevant components of Tkinter and some simple logic processing, in the process of writing this case, we should do to review the past and know the new.

The program code is as follows:

From tkinter import * from time import strftimeroot = Tk () root.geometry ('500x350' 300') root.iconbitmap ('C:/Users/Administrator/Desktop/ C logo.ico') root.title ("C language Chinese website") # set text label lb = Label (root, font= ("Microsoft Yahue", 50, "bold"), bg='#87CEEB', fg= "# B452CD") lb.pack (anchor= "center", fill= "both") Expand=1) # define a mode flag mode = 'time'# function def showtime (): if mode =' time': # time formatting processing string = strftime ("% H:%M:%S% p") else: string = strftime ("% Y-%m-%d") lb.config (text=string) # execute the time function lb.after (1000) every 1 second Showtime) # define mouse handling events Click time to switch to date style display def mouseClick (event): global mode if mode = = 'time': # Click to switch mode style to date style mode =' date' else: mode = 'time'lb.bind (", mouseClick) # call showtime () function showtime () # display window mainloop ()

The running result of the program is as follows:

Figure 1: simple digital clock

Through the above code to achieve a simple digital clock, is not very simple.

Supplement

In addition to digital clocks, Tkinter can also draw a simple clock

The specific implementation code is as follows:

# coding:utf-8from tkinter import * import math,timedef points (): for i in range (1Magne13): X = 200 + 130*math.sin (2*math.pi*i/12) y = 200-130*math.cos (2*math.pi*i/12) canvas.create_text (xmemyscripti) def createline (radius,line_width,rad): global List global i List = [] x = 200+radius*math.sin (rad) y = 200-radius*math.cos (rad) i=canvas.create_line Width=line_width) List.append (I) root = Tk () root.resizable (0P0) canvas = Canvas (root,width=400,height=500,bd=0,highlightthickness=0) canvas.pack () canvas.create_oval (50Med 50350350) points () while 1: tm=time.localtime () t=time.asctime (tm) t_hour=0 if tm.tm_hour

Tags: Number clock code program language function function time style processing Chinese net content object date format source code knowledge procedure Chinese switch Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn Microsoft Xiaomi Shulou Technology macOS