1

我创建了一个 GUI,用户可以使用 tkcalendar DateEntry 小部件从下拉列表中选择一个日期。我想允许用户选择不选择日期并将此小部件留空。但是,即使没有选择日期,小部件也会返回当前日期。

如果用户没有选择日期,有没有办法将 DateEntry 配置为不允许选择而不是默认为当前日期?

下面是我的代码的一个子集:

import pandas as pd
from tkinter import *
from tkinter.ttk import *
import tkinter as tk
from tkcalendar import DateEntry  

class Window(Frame):
    def __init__(self, master):
        Frame.__init__(self,master)
        master.title('Solar Master Project Tracking')

        # create canvas for scrollable window
        canvas = Canvas(root)
        canvas.grid(row=1,column=0, columnspan=2)

        # create vertical scrollbar and connect it to the canvas
        scrollBar = tk.Scrollbar(root, orient='vertical', command = canvas.yview)
        scrollBar.grid(row=1, column=2, sticky = 'ns')
        canvas.configure(yscrollcommand=scrollBar.set)

        def update_scroll_region(event):
            canvas.configure(scrollregion=canvas.bbox("all"))

        def _on_mousewheel(event):
            canvas.yview_scroll(int(-1*(event.delta/120)), "units")

        # create a frame for the widgets in the scrollable canvas
        scroll_frame = Frame(canvas)
        scroll_frame.bind("<Configure>", update_scroll_region)
        canvas.create_window(0,0, anchor='nw', window = scroll_frame)
        canvas.bind_all("<MouseWheel>", _on_mousewheel)

        # Proposal Date
        self.L18 = Label(scroll_frame, text="Proposal Date:",font=('TKDefaultFont', 8, 'bold'))
        self.L18.grid(row=21, column=0, sticky=W)
        self.prop_date_selection = DateEntry(scroll_frame, width = 25, background = 'LightCyan3',
                                             foreground ='white',borderwidth=2)
        self.prop_date_selection.grid(row=21, column=1,sticky=E)
        self.prop_date_selection.delete(0,"end")


        # SUBMIT INFORMATION
        self.button = tk.Button(root, text="Insert / Update Project",font=('TKDefaultFont', 10, 'bold'),
                                relief=RAISED, command = self.store_user_inputs, bg = "gray80")
        self.button.grid(row=25, column = 0, columnspan=8, sticky = 'EW')

    # STORE USER INPUT
    def store_user_inputs(self):
        prop_date_selection = self.prop_date_selection.get_date()

        global params
        params = [prop_date_selection]
        root.destroy()

if __name__ == "__main__":
    root = Tk()
    Window(root)
    root.mainloop()  
4

2 回答 2

0

您可以创建一个继承自tkcalendar.DateEntry并修改DateEntry 为空时get_date()返回的方法的类:None

import tkcalendar

class DateEntry(tkcalendar.DateEntry):
    def get_date(self):
        if not self.get():
            return None
        self._validate_date()
        return self.parse_date(self.get())
于 2020-06-10T11:14:40.153 回答
0

对于那些找到这篇文章的人。

在我的情况下,修改 get_date() 并没有改变任何东西。但以下做到了:

class MyDateEntry(tkcalendar.DateEntry):
    def _validate_date(self):
        if not self.get():
            return True # IMPORTANT!!! Validation must return True/False otherwise it is turned off by tkinter engine
        
        return super()._validate_date()
于 2021-09-05T21:13:47.323 回答