[英]How to display with a random word List the same random word on a specific date
我用 Python Tkinter 制作了一个日历应用程序,我已经找到了如何将用户输入保存到日期以及如何显示它。 所以我正在尝试制作一个每日报价按钮。 但为此它应该在一个日期始终显示相同的报价,因为它是当天的报价。 为此,我使用了与用户输入相同的方法。 但是现在,如果我在一天中第二次单击,它不会显示相同的报价,它会简单地显示:..Toplevel。 我真的没有找到答案。
这是我的代码的简化版本:
from tkinter import *
from tkcalendar import *
import datetime
import secrets
root = Tk()
Quotes_dict = {}
today = datetime.date.today()
cal = Calendar(root, selectmode="day", year=today.year, month=today.month, day=today.day)
Calendar.date.day
cal.place(x=0, y=0, height=600, width=1500)
list= ['Hi', 'HI2']
def random(quotes):
return secrets.choice(quotes)
def Quotes():
Quotes_fenster = Toplevel(root)
app_width = 1000
app_height = 100
Quotes_fenster.geometry(f'{app_width}x{app_height}+{125}+{10}')
Quotes_fenster.resizable(False, False)
Quotes_fenster.title("Zitate")
Quotes_zitat = Label(Quotes_fenster, text="", font=18)
Quotes_zitat.pack()
datum = str(cal.get_date())
try:
if event := Quotes_dict[datum]:
Quotes_zitat.config(text=f'{event}')
except Exception as e:
Quotes_oftheday = random(list)
Quotes_dict[datum] = Quotes_fenster
Quotes_zitat.config(text=f'{ Quotes_oftheday}')
button = Button(root,text=" Quotes", command=Quotes)
button.pack()
root.mainloop()
我希望你明白我在说什么
Quotes_dict[datum] = Quotes_fenster
您在这里所做的是将Tk.TopLevel
实例 Quotes_fenster object 保存到您的 Quotes_dict 中。
当您在同一天第二次单击该按钮时
if event:= Quotes_dict[datum]: Quotes_zitat.config(text=f'{event}')
这将从最后一次按下按钮中检索 Quotes_fenster object,转换为字符串,然后将其放入Tk.Label
的文本中。 在Tkinter中,对象的字符串表示是object的继承类型的句点分隔列表; 在这种情况下.!TopLevel
为 Quotes_fenster object。
你可能想要的是
Quotes_dict[datum] = Quotes_oftheday
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.