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

时间:2018-06-12 10:10:13

标签: python tkinter

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

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"

enter image description here 能帮帮我吗。

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

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

2 个答案:

答案 0 :(得分:1)

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

1)在tk(和Python)中

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

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

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

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

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

答案 1 :(得分:1)

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

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并尝试阅读课程及其工作原理。正如你所看到的,我没有在我的例子中将继承合并到tk.Frametk.Toplevel,因为我想这会让你更复杂,但我真的建议尝试彻底理解和使用这个答案的结构。从长远来看,它会对你有所帮助。