Ann
Ann

Reputation: 95

How save date from Calendar in tkcalendar (Python)?

I want to save the selected dates from the calendar to the variables. Here is the code that i found, but i didn't understand how save date.

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

This code only print data(for example), but i cannot save it to last_date and next_date:

2018-02-07
2018-02-28

Dates should be kept in memory (for example)

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

enter image description here Could you please help me with it.

Also i try this, but stil cannot get value. It still prints only the values, but does not save:

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

Upvotes: 0

Views: 5603

Answers (2)

fhdrsdg
fhdrsdg

Reputation: 10532

Because there is no way to return anything from Button commands in tkinter, the easiest way to deal with things like this is to wrap it in a class and use class variables to store your generated data. I added the wait_window and grab_set commands to make the root window wait an be unclickable until the calendar chooser window is closed.

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

If this code makes no sense to you, please see this post and try to read up into classes and how they work. As you can see, I didn't incorporate the inheritance to tk.Frame and tk.Toplevel in my example because I guess that would make it more complicated for you, but I really would advise to try and thoroughly understand and use the structure from this answer. It will help you in the long run.

Upvotes: 1

Konstantin Kozlenko
Konstantin Kozlenko

Reputation: 186

before i givet you work code i would like tell you:

1) in tk (and Python)

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

you concatenate name with function (command=example1), but if you change on

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

you will get two windows, because you run automatic function

2) i am not sure that is is good practices, but it situation you will need create two functions almost the same, but with one different

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

here is full work code:

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

if use classes and get values:

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

Upvotes: 1

Related Questions