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