Real time clock display in Tkinter

2024/7/8 6:11:11

I want to create real time clock using Tkinter and time library. I have created a class but somehow I am not able to figure out my problem.

My code

from tkinter import *import timeroot = Tk()class Clock:def __init__(self):self.time1 = ''self.time2 = time.strftime('%H:%M:%S')self.mFrame = Frame()self.mFrame.pack(side=TOP,expand=YES,fill=X)self.watch = Label (self.mFrame, text=self.time2, font=('times',12,'bold'))self.watch.pack()self.watch.after(200,self.time2)obj1 = Clock()
root.mainloop()
Answer

Second parameter of after() should be a function -when you are giving any- but you are giving a str object. Hence you are getting an error.

from tkinter import *    
import timeroot = Tk()class Clock:def __init__(self):self.time1 = ''self.time2 = time.strftime('%H:%M:%S')self.mFrame = Frame()self.mFrame.pack(side=TOP,expand=YES,fill=X)self.watch = Label(self.mFrame, text=self.time2, font=('times',12,'bold'))self.watch.pack()self.changeLabel() #first call it manuallydef changeLabel(self): self.time2 = time.strftime('%H:%M:%S')self.watch.configure(text=self.time2)self.mFrame.after(200, self.changeLabel) #it'll call itself continuouslyobj1 = Clock()
root.mainloop()

Also note that:

The callback is only called once for each call to this method. To keepcalling the callback, you need to reregister the callback insideitself.

https://en.xdnf.cn/q/120477.html

Related Q&A

cffi export python code to dll , how to read image object in @ffi.def_extern()

i am trying to convert my python code to dll using cffi so i can access this code in my c# apllication, and i am trying to send image my c# code to python function, below is my code to read the file an…

Python 2.7.5 and Python 3.6.5

I have installed Python 3.6.5 however when i type Python it shows Python 2.7.5. Id like to use Python 3.[aravind@aravind05 Python-3.6.5]$ python3 --version Python 3.6.5[aravind@aravind05 Python-3.6.5]$…

How use creating polynomial expression like function in Python?

Id like to write a program in Python where user define a deegre of polynomial and coefficients (a,b,c). When program create a polynomial expression with this data Id like to use it like function becaus…

how to merge two sublists sharing any number in common? [duplicate]

This question already has an answer here:Using sublists to create new lists where numbers dont repeat(1 answer)Closed 9 years ago.Given thatg=[[1,2,3,4],[4,5,6],[6,7],[10,11]]What code should I use to …

\n is treated as \ and n [duplicate]

This question already has an answer here:Compiler error "error: stray \ in program" in macro definition(1 answer)Closed 10 years ago.The following python codeenv.Command(versionFile, allSrcs …

How to locate an element and extract required text with Selenium and Python

I am using Selenium to enter data on a web page, but have run into an issue with one of the input fields. This is the HTML code causing my difficulty:<div class="form-group"> <label …

Determine type of disk on Windows with Python

I want to list all of my disk and can be able to know its type: "SATA" "NVME" "M.2" or "PCI" on Windows computer. I made some research with wmi and I get the int…

How to get a list the visible vertices and segments of a mesh

I work on pose estimation of a 3d objects. I am using CAD model of that object to generate all the possible hypothesis of its pose. I am using pyopengl to render the view of the object from a specific…

Python function calls the wrong method/target

The following program simulates a traffic light system with some buttons. The buttons appear correctly, but if Im trying to call the method to create/change the LEDs, it ends up in the wrong method. He…

How to make a triangle of xs in python?

How would I write a function that produces a triangle like this:xxxxxxxxxx xxxxxLets say the function is def triangle(n), the bottom row would have n amount of xsAll I know how to do is make a box:n = …