繁体   English   中英

Python Tkinter从功能设置标签文本

[英]Python tkinter setting label text from function

我在tkinter窗口中有一个标签,并且有一个以西班牙语输出当前日期的函数。 我试图将标签文本设置为该函数给定的日期,但是它不起作用。

代码如下:

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()

谁能帮我这个?

提前致谢

您的代码有很多问题,包括缺少导入,缩进,样式,变量名称不唯一,混淆了哪些变量引用了哪些对象,拼写错误(是的,这些很重要,因为它们几乎使崩溃的bug困扰着用户),小部件放置不佳,字符串格式奇怪,无法调用已定义的函数,被丢弃而不是返回的函数结果,还有可能让我忘记的更多内容。

这是正常工作的代码的清理版本:

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()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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