如何使用与 ttkbootstrap.DateEntry 的绑定来更新具有所选日期的标签?

问题描述 投票:0回答:1

我使用 ttkbootstrap 框架主要是因为现代的

DateEntry
,它有一些有用的主题。我的目标是让
DateEntry
小部件自动使用从 DatePickerPopup 中选择的日期更新
Label
,但是使用
"<<DateEntrySelected>>"
.bind()
事件不像常规 tkinter 那样工作。我的解决方法是使用
Button
单击调用我的 update_label 函数。每次用户选择日期时生成的虚拟事件是什么?

这是我的代码片段:

import ttkbootstrap as ttk
from datetime import datetime


def update_label(event):
    selected_date = cal.entry.get()
    date_object = datetime.strptime(selected_date, "%m/%d/%Y")
    formated_date = date_object.strftime("%A, %b %d")
    label.config(text=f"{formated_date}")


root = ttk.Window()
root.geometry("600x400")

# Label
label = ttk.Label(root, text="Today")
label.pack(pady=5)

# DateEntry
cal = ttk.DateEntry(root, bootstyle="primary")
cal.pack(pady=5)
cal.bind("<<DateEntrySelected>>", update_label)  # FIX

root.mainloop()

ttkbootstrap 文档:https://ttkbootstrap.readthedocs.io/en/latest/api/widgets/dateentry/

在那里,它说...“日期输入小部件将组合框和按钮与附加到 get_date 函数的回调结合在一起。 按下后,将显示日期选择器弹出窗口。返回的值被插入到组合框中。 日期选择器弹出窗口将使用组合框中的日期作为焦点日期...

解决方法是使用按钮,就像这样......

btn = ttk.Button(root, text="Update", command=update_label)
btn.pack(pady=5)
python tkinter bind ttkbootstrap
1个回答
0
投票

我的解决方法是使用按钮调用我的 update_label 函数 单击即可。

问题可以解决。

删除

update_label
函数中的参数。

添加按钮小部件。

片段:

def update_label():
    selected_date = cal.entry.get()
    #date_object = datetime.strptime(selected_date, "%m/%d/%Y")
    #formated_date = date_object.strftime("%A, %b %d")
    label.config(text=f"{selected_date}")

...
...
...
btn = ttk.Button(root, text="Update", command=update_label)
btn.pack(pady=5)

截图:

© www.soinside.com 2019 - 2024. All rights reserved.