0

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
j_4321
  • 15,431
  • 3
  • 34
  • 61
Ann
  • 95
  • 2
  • 7

2 Answers2

1

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()
  • Hi, thank you for your answer, but that's not exactly what I need. Sorry, my fault did not quite accurately describe the problem. I need that the selected values from the calendar be written into memory and they could be used later as a variable with a value. – Ann Jun 12 '18 at 11:19
  • this is to easy if you will use variables. and if use classes it will be more easy, because you will have access to variables in any place of class – Konstantin Kozlenko Jun 12 '18 at 11:37
  • I am not sure how i can use class. I modificated function like this, but i still cannot get value..: def print_sel(): a=str( cal.selection_get()) print(a) return a – Ann Jun 12 '18 at 11:57
  • try this. it must work good. and you have access from a different place of class and you can use at any time – Konstantin Kozlenko Jun 12 '18 at 12:14
  • Yes, It works. Thanks a lot. So i can get data with this command: date1=tt.last_date – Ann Jun 12 '18 at 14:33
  • not quite, because from button you could not get information as it is with text area (text_area_obj.get()). to button concatenate some function without params and without return result. but you can change some variable – Konstantin Kozlenko Jun 13 '18 at 05:23
  • How to store the picked date into variable without using a class? – Wizytor Jan 09 '23 at 10:56
1

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.

fhdrsdg
  • 10,297
  • 2
  • 41
  • 62
  • This code make sence for me and now i am understand about Button commands, and that need use Class in this case. Many thanks for help. – Ann Jun 12 '18 at 15:29