簡體   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