【问题标题】:How to initialise the calendar with blank date in tkcalendar?如何在 tkcalendar 中使用空白日期初始化日历?
【发布时间】:2019-06-06 11:39:02
【问题描述】:

我正在尝试创建一个日期选择器。我在网上找到了一个适合我的代码。但是,使用此代码,程序在启动时默认显示今天的日期:

有没有办法像这样将默认日期设置为空白?

我一直在寻找答案,但没有运气。

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()

【问题讨论】:

    标签: python tkinter tkcalendar


    【解决方案1】:

    DateEntry 继承自 Entry 小部件。所以你可以像普通的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")
    

    【讨论】:

    • 这就是我想要的。非常感谢:)
    • 如果有帮助,请考虑接受答案,以便关闭问题。
    【解决方案2】:

    我不需要上课。这个解决方案工作得很好:

    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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-07
      • 2021-10-30
      • 1970-01-01
      相关资源
      最近更新 更多