2021年4月24日星期六

updating a label with a changing variable

I'm trying to make a clock app using tkinter, and i used the datetime module to get the current system time, and i made a function to update a label to the current time.

clock_label is a tk.Label and it will be updated to the current system time every 80 milliseconds, like this;

import tkinter  import datetime  from time import strftime    ...    window = tk.Tk()  clock_label = tk.Label(window)  clock_label.grid()      def update_label():       time = datetime.today().strftime("%I:%M:%S%p")   # current system time     config_label()       clock_label.config(text=time)       clock_label.after(80, lambda: update_clock())     update_label()  window.mainloop()  

And...

I made a function where the user can change the timezone,

with the help of pytz, tzlocal and timezone modules i was able to get the current time from any selected timezone,

for example if the user selected Europe/madrid the function will return the current time of that timezone; (i assigned it as a variable: new_time)

the problem i have is that i'm not sure how to update the clock_label with the new time;

 selected_timezone_name = "Europe/Madrid"      ...  # code for getting the time from a selected timezone     new_time = datetime.strftime(selected_time_object, "%H:%M:%S")   # update the clock_label with new time   

beacuse the new_time variable keeps changing i need to make a function which keeps updating the clock_label with the new_time,

I tried to make function which updates the clock_label with the new time similar to update label() but beacuse the update_label() function keeps updating the clock_label with the system time it creates confusion

https://stackoverflow.com/questions/67245369/updating-a-label-with-a-changing-variable April 25, 2021 at 01:04AM

没有评论:

发表评论