简体   繁体   English

如何在 tkcalendar 中用空白日期初始化日历?

[英]How to initialise the calendar with blank date in tkcalendar?

I'm trying to create a date picker.我正在尝试创建一个日期选择器。 I found a code online that works for me.我在网上找到了一个对我有用的代码。 However, with this code, the program shows today's date as default when it starts like this:但是,使用此代码,程序在启动时默认显示今天的日期,如下所示:

在此处输入图片说明

Is there a way to set the default date to blank like this?有没有办法像这样将默认日期设置为空白?

在此处输入图片说明

I have been looking for an answer but no luck.我一直在寻找答案,但没有运气。

import tkinter as tk

from tkinter import ttk

from tkcalendar import DateEntry

from datetime import date

root = tk.Tk()

style = ttk.Style(root)

style.theme_use('clam')


class MyDateEntry(DateEntry):

    def __init__(self, master=None, **kw):

        DateEntry.__init__(self, master=None, **kw)

        self._top_cal.configure(bg='black', bd=1)


de = MyDateEntry(root,selectbackground='gray80',
                 selectforeground='black',
                 normalbackground='white',
                 normalforeground='black',
                 background='gray90',
                 foreground='black',
                 bordercolor='gray90',
                 othermonthforeground='gray50',
                 othermonthbackground='white',
                 othermonthweforeground='gray50',
                 othermonthwebackground='white',
                 weekendbackground='white',
                 weekendforeground='black',
                 headersbackground='white',
                 headersforeground='gray70')

de.pack()

root.mainloop()

DateEntry is inherited from Entry widget. DateEntry继承自Entry小部件。 So you can delete the content just like a normal Entry widget:因此,您可以像普通Entry小部件一样删除内容:

class MyDateEntry(DateEntry):

    def __init__(self, master=None, **kw):

        DateEntry.__init__(self, master=None, **kw)

        self._top_cal.configure(bg='black', bd=1)

        self.delete(0, "end")

I did not need to make a class.我不需要上课。 This solution works totally fine:此解决方案完全正常:

date_started = DateEntry(root, locale='en_US', date_pattern='yyyy-mm-dd')
date_started.delete(0, "end")  ## Only this line needed to be added to clear the field.
date_started.place(x=150, y=60, height=25, width=120)

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

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