прежде чем я дам вам рабочий код, я бы хотел сказать вам:
1) в тк (и Python)
ttk.Button(root, text='Last Date', command=example1)
вы объединяете имя с функцией (команда = пример1), ноесли вы переключитесь на
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()