0

我正在使用 Tkinter DateEntry 字段来获取 SQL 查询的“从”和“到”日期。我最近重做了我的项目,但复制了很多代码。在我的旧项目中,日期输入工作正常,但是在我的新项目中,每当我尝试为任何 DateEntry 字段选择一天时,它会自动填充并且不允许我使用下拉窗口。

此链接是它在我的工作项目中的外观示例: https ://www.plus2net.com/python/tkinter-DateEntry.php

这是工作代码:

date_joined_to = DateEntry(container, selectmode="day", year=datetime.now().year,
                           month=datetime.now().month, day=datetime.now().day,
                           date_pattern="yyyy-mm-dd", foreground="black",
                           headersforeground="black", selectforeground="black", 
                           width=10)
date_joined_to.grid(row=4, column=1, sticky="W")
date_joined_to.delete(0, "end")
date_joined_to._top_cal.overrideredirect(False)

这是我在新项目中出现问题的代码:

test_date_to = DateEntry(self.centersearch, selectmode="day", year=datetime.now().year,
                         month=datetime.now().month,
                         day=datetime.now().day, date_pattern="yyyy-mm-dd",
                         foreground="black", headersforeground="black",
                         selectforeground="black", width=10)
test_date_to.grid(row=9, column=1, sticky="E")
test_date_to.delete(0, "end")
test_date_to._top_cal.overrideredirect(False)

我尝试删除所有关键字参数以查看是否是它们引起的,但问题仍然存在。

从我的搜索来看,确实有很多人以前遇到过这种情况。

4

1 回答 1

0

我无法回答,因为我看到了同样的问题并来到这里寻找答案。这是显示它的代码。在我的情况下,工作的版本看起来相同,但不在单独的类中,而是在一个大脚本文件中。但是对于现实世界的问题,这是糟糕的编码实践。当我把它移到课堂上时,它不再起作用了。

这个版本有效

import tkinter as tk
from tkinter import ttk
from tkinter import messagebox
from tkcalendar import DateEntry

from datetime import datetime
current_day = datetime.now().day
current_month = datetime.now().month
current_year = datetime.now().year

root = tk.Tk()
root.title("Date Demo Works")
# root.minsize(750, 400)
root.geometry("1024x768+0+0")
root.grid()

demo_edit_screen = tk.Toplevel(root)
demo_edit_screen.geometry("1024x768+0+0")
demo_edit_screen.title("Demo Working")
demo_edit_screen.iconify()
demo_edit_screen.withdraw()

def DoNothing():
    tk.messagebox.showinfo(message='This is an info box that Does Nothing')
    print("testing do nothing dialog")

class DemoEdit():
    def __init__(self):
        leftsidebar = tk.Frame(demo_edit_screen, borderwidth=2, relief="raised", width=40)
        leftsidebar.grid(row=0, column=0, rowspan=11, sticky=("NSEW"))
        gohome = ttk.Button(leftsidebar, text="Home", command=DoNothing())
        gohome.grid(row=0, column=0)
        centersearch = tk.Frame(demo_edit_screen, width=850)
        centersearch.grid(row=0, column=1, sticky="W")
        test_date_label = tk.Label(centersearch, text='Test Date Range', anchor="e")
        test_date_label.grid(row=7, column=0, columnspan=2, padx=4)
        test_date_from_label = tk.Label(centersearch, text='From', anchor="w")
        test_date_from_label.grid(row=8, column=0, padx=4)
        test_date_to_label = tk.Label(centersearch, text='To', anchor="w")
        test_date_to_label.grid(row=8, column=1, padx=4)
        test_date_from = DateEntry(centersearch, selectmode="day"
                                   , year=datetime.now().year, month=datetime.now().month, day=datetime.now().day
                                   , date_pattern="yyyy-mm-dd"
                                   , foreground="black", headersforeground="black", selectforeground="black", width=10)
        test_date_from.grid(row=9, column=0, sticky="W")
        test_date_from.delete(0, "end")
        test_date_from._top_cal.overrideredirect(False)
        test_date_to = DateEntry(centersearch, selectmode="day"
                                 , year=datetime.now().year, month=datetime.now().month, day=datetime.now().day
                                 , date_pattern="yyyy-mm-dd"
                                 , foreground="black", headersforeground="black", selectforeground="black", width=10)
        test_date_to.grid(row=9, column=1, sticky="E")
        test_date_to.delete(0, "end")
        test_date_to._top_cal.overrideredirect(False)

    def show_demoedit(self):
        root.withdraw()
        demo_edit_screen.update()
        demo_edit_screen.deiconify()

leftsidebar = tk.Frame(root, borderwidth=2, relief="raised", height=768)
leftsidebar.grid(row=0, column=0, rowspan=8, sticky=("NSEW"))
show_screen = ttk.Button(leftsidebar, text="Show Calendar Screen", command=DemoEdit.show_demoedit(root))
show_screen.grid(row=3, column=0)
demo_edit = DemoEdit()

root.mainloop()

这个版本不

import tkinter as tk
from tkinter import ttk
from tkinter import messagebox

from tkcalendar import DateEntry
from datetime import datetime

def DoNothing():
    tk.messagebox.showinfo(message='This is an info box that Does Nothing')
    print("testing do nothing dialog")

class TopScreen(tk.Frame):
    def __init__(self, parent, controller, label_text="sample", *args, **kwargs):
        tk.Frame.__init__(self, parent, *args, **kwargs)
        self.controller = controller
        self.parent = parent
        #   Create the choose action sidebar that will go to specific screens
        self.leftsidebar = tk.Frame(self, borderwidth=2, relief="raised", height=768)
        self.leftsidebar.grid(row=0, column=0, rowspan=8, sticky=("NSEW"))
        self.home_label = tk.Label(self.leftsidebar, text=label_text)
        self.home_label.grid(row=0, column=0)


class CSVReportScreen(TopScreen):
    def __init__(self, parent, controller):
        super().__init__(parent, controller, label_text="CSV Report")
        self.controller = controller
        self.parent = parent
        create_csv_files_button = ttk.Button(self.leftsidebar, text="Create .CSV Files", command=DoNothing())
        create_csv_files_button.grid(row=3, column=0, sticky="NSEW")
        #   Top center search section
        self.centersearch = tk.Frame(self, width=850)
        self.centersearch.grid(row=0, column=1, sticky="W")

        test_date_label = tk.Label(self.centersearch, text='Test Date Range', anchor="e")
        test_date_label.grid(row=7, column=0, columnspan=2, padx=4)
        test_date_from_label = tk.Label(self.centersearch, text='From', anchor="w")
        test_date_from_label.grid(row=8, column=0, padx=4)
        test_date_to_label = tk.Label(self.centersearch, text='To', anchor="w")
        test_date_to_label.grid(row=8, column=1, padx=4)
        test_date_from = DateEntry(self.centersearch, selectmode="day"
                                 , year=datetime.now().year, month=datetime.now().month, day=datetime.now().day
                                 , date_pattern="yyyy-mm-dd"
                                 , foreground="black", headersforeground="black", selectforeground="black", width=10)
        test_date_from.grid(row=9, column=0, sticky="W")
        test_date_from.delete(0, "end")
        test_date_from._top_cal.overrideredirect(False)
        test_date_to = DateEntry(self.centersearch, selectmode="day"
                                 , year=datetime.now().year, month=datetime.now().month, day=datetime.now().day
                                 , date_pattern="yyyy-mm-dd"
                                 , foreground="black", headersforeground="black", selectforeground="black", width=10)
        test_date_to.grid(row=9, column=1, sticky="E")
        test_date_to.delete(0, "end")
        test_date_to._top_cal.overrideredirect(False)


class TestApp(tk.Tk):
    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)

        container = tk.Frame(self)
        container.pack(side="top", fill="both", expand=True)
        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)
        self.frames = dict()
        # Add each separate screen here. The name is the class for that screen
        trakker_classes = (CSVReportScreen,TopScreen)
        for F in trakker_classes:
            page_name = F.__name__
            frame = F(parent=container, controller=self)
            self.frames[page_name] = frame
            frame.grid(row=0, column=0, sticky="nsew")
        self.show_frame("CSVReportScreen")

    def show_frame(self, page_name):
        # Show a frame for the given page name
        frame = self.frames[page_name]
        frame.tkraise()

if __name__ == "__main__":
    app = TestApp()
    app.mainloop()

DateEntry 是 tkcalendar 的一部分。所以我有同样的问题。

于 2022-02-06T21:34:29.890 回答