如何在 tkcalendar (Python) 中获取 DateEntry 的选定日期? [英] How to get the selected date for DateEntry in tkcalendar (Python)?

查看:40
本文介绍了如何在 tkcalendar (Python) 中获取 DateEntry 的选定日期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 tkcalendar,它是日历、DateEntry 的预定义小部件,我正在尝试获取用户为 DateEntry 选择的日期.虽然可以使用 "selection_get()" 提取日历小部件的选定日期,但我找不到 DateEntry 的任何内容.

I have a tkcalendar and it's pre-defined widget for Calendar, DateEntry and am trying to get the User's selected date for DateEntry. Whereas there is provision to extract the selected date for the Calendar widget using "selection_get()" but nothing for DateEntry that I could find.

我尝试过 get_date()、get()、_date()、cget()、._selection() 等,但它们似乎没有返回/打印用户选择的日期.请帮忙,如果需要任何补充信息,请告诉我

I have tried get_date(),get(),_date(), cget(), ._selection() amongst many others but they don't seem to return/print the user-selected date. Please help, kindly let me know if any added information is needed

代码[选自一个简单的 tkcalendar 教程]:

Code [picked from a simple tkcalendar tutorial]:

import tkinter as tk
from tkinter import ttk
from tkcalendar import Calendar, DateEntry

def calendar_view():
    def print_sel():
        print(cal.selection_get())

    top = tk.Toplevel(root)

    cal = Calendar(top,
                   font="Arial 14", selectmode='day',
                   cursor="hand1", year=2018, month=2, day=5)
    cal.pack(fill="both", expand=True)
    ttk.Button(top, text="ok", command=print_sel).pack()

def dateentry_view():
    top = tk.Toplevel(root)

    ttk.Label(top, text='Choose date').pack(padx=10, pady=10)
    cal = DateEntry(top, width=12, background='darkblue',
                    foreground='white', borderwidth=2)
    cal.pack(padx=10, pady=10)
    print(cal.cget(DateEntry))

root = tk.Tk()
s = ttk.Style(root)
s.theme_use('clam')

ttk.Button(root, text='Calendar', command=calendar_view()).pack(padx=10, pady=10)
ttk.Button(root, text='DateEntry', command=dateentry_view()).pack(padx=10, pady=10)

root.mainloop()

推荐答案

您提到您已经尝试过 get_date() 并且没有奏效,但这实际上是正确的功能.

You mention you have tried get_date() and it didn't work, but that's actually the right function.

import tkinter as tk
from tkinter import ttk
from tkcalendar import Calendar, DateEntry

def calendar_view():
    def print_sel():
        print(cal.selection_get())

    top = tk.Toplevel(root)

    cal = Calendar(top,
                   font="Arial 14", selectmode='day',
                   cursor="hand1", year=2018, month=2, day=5)
    cal.pack(fill="both", expand=True)
    ttk.Button(top, text="ok", command=print_sel).pack()

def dateentry_view():
    def print_sel():
        print(cal.get_date())
    top = tk.Toplevel(root)

    ttk.Label(top, text='Choose date').pack(padx=10, pady=10)
    cal = DateEntry(top, width=12, background='darkblue',
                    foreground='white', borderwidth=2)
    cal.pack(padx=10, pady=10)
    ttk.Button(top, text="ok", command=print_sel).pack()

root = tk.Tk()
s = ttk.Style(root)
s.theme_use('clam')

ttk.Button(root, text='Calendar', command=calendar_view).pack(padx=10, pady=10)
ttk.Button(root, text='DateEntry', command=dateentry_view).pack(padx=10, pady=10)

root.mainloop()


如果您想在每次更改时获取日期,您可以使用事件绑定.来自文档:

  • 虚拟活动

每当用户用鼠标选择一天时,都会生成一个 <> 事件.

A <<CalendarSelected>> event is generated each time the user selects a day with the mouse.

所以你可以将获取日期的函数绑定到 <> 事件:

So you can bind the function that gets the date to the <<CalendarSelected>> event:

def dateentry_view():
    def print_sel(e):
        print(cal.get_date())
    top = tk.Toplevel(root)

    ttk.Label(top, text='Choose date').pack(padx=10, pady=10)
    cal = DateEntry(top, width=12, background='darkblue',
                    foreground='white', borderwidth=2)
    cal.pack(padx=10, pady=10)
    cal.bind("<<DateEntrySelected>>", print_sel)

这篇关于如何在 tkcalendar (Python) 中获取 DateEntry 的选定日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆