Home > database >  clock/timer refresh times per second *tkinter
clock/timer refresh times per second *tkinter

Time:11-09

i'm making a clock but Click the time in seconds, for example, if the user clicks 2 hours, the timer will be like this:

2:00:00
1:59:59
but When it is refreshed, it replaces the previous label

that's mean:
2:00:00
refresh*
1:59:59
And the next feature is that if the user hits the minute

Start with minutes and give an error if it exceeds 60 minutes

And the same feature can be done per second

for example:
entry = 70 minutes
>> erorr! number > 60

Can anyone help? my codes:

mili_second_app = 60000
def jik():
    a = int(text.get())
    app.after(a * mili_second_app,show)
def show():
    Label(app,text="time is over").pack()
def jik_1():
    a = int(text.get())
    mili_second_app = 1000
    app.after(a * mili_second_app,show)
mili_second_app
app = Tk()
app.minsize(300,300)
app.maxsize(300,300)
text = Entry(app,font=20)
text.pack()
Button(app,text="محاسبه",command=jik).pack()
menu = Menu()
menu_select = Menu(menu,tearoff=0)
menu.add_cascade(label="تغییر زمان",menu=menu_select)
menu_select.add_command(label="برای ثانیه",command=jik_1)
app.config(menu=menu)
app.mainloop()

CodePudding user response:

You should create a Label in the global scope and assign a tk.StringVar() variable to your label's textvariable and use that to update the label periodically instead of creating a new label at each interval

time_var = tk.StringVar(app, '00:00:00')  # variable w/ default value
time = tk.Label(app, textvariable=time_var)  # create a label
time.pack()

Then put time_var.set('<time value as a string>') in a function to update the label. You can use after() to have the update function call itself repeatedly until the time is up.

  • Related