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