简体   繁体   中英

Python tkinter setting label text from function

I have a label in a tkinter window and a function which outputs the current date in spanish. I am trying to set the label text to the date given by the function but it doesnt work.

the code is the following:

root = Tk()
root.title("Tarea Programada 2")
root.geometry("800x700+350+100")
fecha = Label(root, text=" ", font=('times',10), fg= 'black', bg= 'white', width = 30, height = 1)
fecha.place(x=551,y=100)

def horayfecha():
   dia = time.strftime("%A")
   fecha = time.strftime("%m")
   mes = time.strftime("%B")
      año = time.strftime("%Y")
   if dia == "Monday":
       dia = "Lunes"
   if dia == "Tuesday":
       dia = "Martes"
   if dia == "Wednesday":
       dia = "Miercoles"
   if dia == "Thursday":
       dia = "Jueves"
   if dia == "Friday":
       dia = "Viernes"
   if dia == "Satuday":
       dia = "Sabado"
   if dia == "Sunday":
       dia = "Domingo"
   if mes == "January":
       mes = "enero"
   if mes == "February":
       mes = "febrero"
   if mes == "March":
       mes = "marzo"
   if mes == "April":
       mes = "abril"
   if mes == "May":
       mes = "mayo"
   if mes == "June":
       mes = "junio"
   if mes == "July":
       mes = "julio"
   if mes == "August":
       mes = "agosto"
   if mes == "September":
       mes = "stiembre"
   if mes == "October":
       mes = "octubre"
   if mes == "November":
       mes = "noviembre"
   if mes == "December":
       mes = "diciembre"
   comp = ("San José, Costa Rica " + dia + " " + fecha + " de " + mes + " de " + año)
   fecha.setLabel("{}".format(comp))

root.mainloop()

Can anyone help me with this?

Thanks in advance

Your code had many problems, including missing imports, indentation, style, non-unique variable names, mixing up which variables refer to which objects, spelling errors (yes, those count, because they bother the user almost as much as crashing bugs), poor widget placement, strange string formatting, failure to call defined functions, function results that are thrown away instead of returned, and possibly more that I'm forgetting.

Here is a cleaned-up version of your code that works properly:

from tkinter import *
import time

root = Tk()
root.title("Tarea Programada 2")
root.geometry("800x700+350+100")

fecha = Label(root, text=" ", font=('times',10), fg= 'black', bg= 'white', width = 50, height = 1)
fecha.place(x=350,y=100)

def horayfecha():
    día, fecha, mes, año = time.strftime("%A %m %B %Y").split()

    días = {'Monday':'Lunes', 'Tuesday':'Martes', 'Wednesday':'Miércoles',
            'Thursday':'Jueves', 'Friday':'Viernes', 'Saturday':'Sábado', 'Sunday':'Domingo'}

    meses = {'January':'Enero', 'February':'Febrero', 'March':'Marzo', 'April':'Abril', 'May':'Mayo',
             'June':'Junio', 'July':'Julio', 'August':'Agosto', 'September':'Setiembre', 'October':'Octubre',
             'November':'Noviembre', 'December':'Diciembre'}

    return ' '.join(["San José, Costa Rica", días.get(día), fecha, "de", meses.get(mes), "de", año])

fecha.config(text=horayfecha())
root.mainloop()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM