如何在 tkcalendar (Python) 中保存日历中的日期?

How save date from Calendar in tkcalendar (Python)?

我想将日历中选定的日期保存到变量中。这是我找到的代码,但我不明白如何保存日期。

import tkinter as tk
from tkinter import ttk

from tkcalendar import Calendar

def example1():
    def print_sel():
        print(cal.selection_get())
    def quit1():
        top.destroy()

    top = tk.Toplevel(root)

    cal = Calendar(top,
                   font="Arial 14", selectmode='day',
                   cursor="hand1", year=2018, month=2, day=5)
    cal.pack(fill="both", expand=True)
    ttk.Button(top, text="ok", command=print_sel).pack()
    ttk.Button(top, text="exit", command=quit1).pack()

root = tk.Tk()
s = ttk.Style(root)
s.theme_use('clam')

ttk.Button(root, text='Last Date', command=example1).pack(padx=10, pady=10)
ttk.Button(root, text='Next Date', command=example1).pack(padx=10, pady=10)

root.mainloop()

此代码仅打印数据(例如),但我无法将其保存到 last_date 和 next_date:

2018-02-07
2018-02-28

日期应保存在内存中(例如)

last_date="2018-02-07"
next_date="2018-02-28"

你能帮我看看吗

我也试过这个,但仍然无法获得价值。它仍然只打印值,但不保存:

def print_sel():
    a=str( cal.selection_get())
    print(a)  
    return a

在我给你工作代码之前我想告诉你:

1) 在 tk 中(和 Python)

ttk.Button(root, text='Last Date', command=example1)

您将名称与函数连接起来 (command=example1),但是如果您更改

ttk.Button(root, text='Last Date', command=example1())

你会得到两个windows,因为你运行自动功能

2) 我不确定这是好的做法,但在这种情况下,您需要创建两个几乎相同但有一个不同的函数

print('next_date="{}"'.format(cal.selection_get()))

这里是完整的工作代码:

import tkinter as tk
from tkinter import ttk

from tkcalendar import Calendar

def example1():
    def print_sel():
        print('last_date="{}"'.format(cal.selection_get()))
    def quit1():
        top.destroy()

    top = tk.Toplevel(root)

    cal = Calendar(top,
                   font="Arial 14", selectmode='day',
                   cursor="hand1", year=2018, month=2, day=5)
    cal.pack(fill="both", expand=True)
    ttk.Button(top, text="ok", command=print_sel).pack()
    ttk.Button(top, text="exit", command=quit1).pack()

def example2():
    def print_sel():
        print('next_date="{}"'.format(cal.selection_get()))
    def quit1():
        top.destroy()

    top = tk.Toplevel(root)

    cal = Calendar(top,
                   font="Arial 14", selectmode='day',
                   cursor="hand1", year=2018, month=2, day=5)
    cal.pack(fill="both", expand=True)
    ttk.Button(top, text="ok", command=print_sel).pack()
    ttk.Button(top, text="exit", command=quit1).pack()

root = tk.Tk()
s = ttk.Style(root)
s.theme_use('clam')

ttk.Button(root, text='Last Date', command=example1).pack(padx=10, pady=10)
ttk.Button(root, text='Next Date', command=example2).pack(padx=10, pady=10)

root.mainloop()

如果使用 类 并获取值:

import tkinter as tk
from tkinter import ttk

from tkcalendar import Calendar

class t:
    def __init__(self):
        self.root = tk.Tk()
        self.s = ttk.Style(self.root)
        self.s.theme_use('clam')

        self.last_date = 'Last Date'
        self.next_date = 'Next Date'

        self.b1 = ttk.Button(self.root, text='Last Date', command=self.example1).pack(padx=10, pady=10)
        self.b2 = ttk.Button(self.root, text='Next Date', command=self.example2).pack(padx=10, pady=10)

        self.b3 = ttk.Button(self.root, text='show', command=self.my_print).pack(padx=10, pady=10)

        self.root.mainloop()

    def my_print(self):
        print ('{}\n{}'.format(self.last_date, self.next_date))

    def example1(self):
        def print_sel():
            print('"{}"'.format(cal.selection_get()))
            self.last_date += str(cal.selection_get())
        def quit1():
            top.destroy()

        top = tk.Toplevel(self.root)

        cal = Calendar(top,
                       font="Arial 14", selectmode='day',
                       cursor="hand1", year=2018, month=2, day=5)
        cal.pack(fill="both", expand=True)
        ttk.Button(top, text="ok", command=print_sel).pack()
        ttk.Button(top, text="exit", command=quit1).pack()

    def example2(self):
        def print_sel():
            print('"{}"'.format(cal.selection_get()))
            self.next_date += str(cal.selection_get())
        def quit1():
            top.destroy()

        top = tk.Toplevel(self.root)

        cal = Calendar(top,
                       font="Arial 14", selectmode='day',
                       cursor="hand1", year=2018, month=2, day=5)
        cal.pack(fill="both", expand=True)
        ttk.Button(top, text="ok", command=print_sel).pack()
        ttk.Button(top, text="exit", command=quit1).pack()    


tt = t()

因为无法从 tkinter 中的 Button 命令中 return 任何东西,处理此类事情的最简单方法是将其包装在 class 中并使用 class变量来存储生成的数据。我添加了 wait_windowgrab_set 命令,使根目录 window 等待不可点击,直到日历选择器 window 关闭。

import tkinter as tk
from tkinter import ttk

from tkcalendar import Calendar


class Example1():
    def __init__(self, root):
        self.top = tk.Toplevel(root)

        self.cal = Calendar(self.top, font="Arial 14", selectmode='day',
                            cursor="hand1", year=2018, month=2, day=5)
        self.cal.pack(fill="both", expand=True)
        ttk.Button(self.top, text="ok", command=self.print_sel).pack()
        ttk.Button(self.top, text="exit", command=self.quit1).pack()

        self.date = ''

        self.top.grab_set()

    def print_sel(self):
        self.date = self.cal.selection_get()

    def quit1(self):
        self.top.destroy()


class App():
    def __init__(self):
        self.root = tk.Tk()
        s = ttk.Style(self.root)
        s.theme_use('clam')

        ttk.Button(self.root, text='Last Date', command=self.last).pack(padx=10, pady=10)
        ttk.Button(self.root, text='Next Date', command=self.next).pack(padx=10, pady=10)

        self.last_date = ''
        self.next_date = ''

        self.root.mainloop()

    def last(self):
        cal = Example1(self.root)
        self.root.wait_window(cal.top)
        self.last_date = cal.date

    def next(self):
        cal = Example1(self.root)
        self.root.wait_window(cal.top)
        self.next_date = cal.date


app = App()
print('Last date: {}'.format(app.last_date))
print('Next date: {}'.format(app.next_date))

如果此代码对您来说没有意义,请参阅 this post 并尝试阅读 classes 及其工作原理。如您所见,我没有在我的示例中将继承合并到 tk.Frametk.Toplevel 中,因为我想这会使您的情况变得更加复杂,但我真的建议尝试彻底理解和使用此答案中的结构。它将帮助你在漫长的运行.