Show UTC time in an Entry Widget

Asked

Viewed 54 times

0

I would like and was trying a way to show the time by updating myself, through an entry on Tkinter. I set up a function for this but I can’t get it to appear in Entry, instead it appears "Function Utclock at..". As can be seen in the image below.

inserir a descrição da imagem aqui

The code to generate the image above was this:

from tkinter import *
import tkinter as tk
from tkinter import ttk
from datetime import datetime
from pytz import timezone

root = tk.Tk()

def UTClock():
    hora_UTC = timezone('Greenwich')
    uTC_hora = datetime.now(hora_UTC)
    curr_utc = uTC_hora.strftime("%H:%M:%S")
    horaUtc.after(100, UTClock)

#Configurando a caixa do sites
caixadosite = tk.LabelFrame(root, text=' aaaaaaaaaaaaaaaa ', font=("Arial",14))
caixadosite.grid(column=3, row=1, padx=8, pady=4, sticky=N, columnspan=3)
#-------------------------------------------------------------------------------------------------------------------------------------------------



#Configurando a caixa do nascer do sol e sua entrada
ttk.Label(caixadosite, text="hora: ", font=("Arial",14)).grid(column=3, row=0, columnspan=3)
horaUtc = tk.StringVar()
horaUtcEntry = ttk.Entry(caixadosite, width=25, state='readonly', justify='center', textvariable=horaUtc, font=("Arial",14))
horaUtcEntry.grid(column=3, row=1, columnspan=3)


horaUtc.set(UTClock)


root.mainloop()

I would like the time to show up at Entry and update alone, but I’m banging my head with it for a while. Is it possible? I appreciate all the help.

  • Two things: 1. within its function UTClock has a variable (horaUtc) not initialized. Note that python works with variable scope 2. I believe you should call the function instead of referencing it => horaUtc.set(UTClock()) Even so, I still think we need to work the code a little more

1 answer

1

from tkinter import *
import tkinter as tk
from tkinter import ttk
from datetime import datetime
from pytz import timezone

root = tk.Tk()

def UTClock():
    hora_UTC = timezone('Greenwich')
    utc_hora = datetime.now(hora_UTC)
    curr_utc = utc_hora.strftime("%H:%M:%S")
    return curr_utc


#Configurando a caixa do sites
caixadosite = tk.LabelFrame(root, text=' aaaaaaaaaaaaaaaa ', font=("Arial",14))
caixadosite.grid(column=3, row=1, padx=8, pady=4, sticky=N, columnspan=3)
#-------------------------------------------------------------------------------------------------------------------------------------------------


#Configurando a caixa do nascer do sol e sua entrada
ttk.Label(caixadosite, text="hora: ", font=("Arial",14)).grid(column=3, row=0, columnspan=3)
y = tk.StringVar() 
horaUtcEntry = ttk.Entry(caixadosite, width=40, state='readonly',
justify='center', textvariable=y, font=("Arial",14))
horaUtcEntry.grid(column=3, row=1, columnspan=3)
y.set(UTClock()) #O que faltou no seu codigo

root.mainloop()

Browser other questions tagged

You are not signed in. Login or sign up in order to post.