python - 如何在 tkcalendar (Python) 中保存日历中的日期?
问题描述
我想将日历中的选定日期保存到变量中。这是我找到的代码,但我不明白如何保存日期。
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"
我也试试这个,但仍然无法获得价值。它仍然只打印值,但不保存:
def print_sel():
a=str( cal.selection_get())
print(a)
return a
解决方案
在我给你工作代码之前,我想告诉你:
1)在传统知识(和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()
推荐阅读
- c++ - 最小移动到相等的数组元素有助于理解解决方案
- python - Python 在不同的编辑器或 IDE 中是否以不同的方式编写?
- wordpress - 从 wordpress 中删除了所有插件,但仍然显示广告
- c# - itext7 HTML to PDF 忽略列宽
- date - 如何使用微秒(2020-10-06T08:52:54.3556219Z)和使用 Java 8 获得 now() 和 TimeStamp 之间的秒差?
- r - 将包含时间的列转换为日期
- android-activity - 当手机被锁定并且屏幕在 android 10 中关闭时,如何从 ForegroundService 打开活动?
- c# - 在 .NET Core 中使用 CMS 和 RSACng 进行签名
- azure - 使用 Azure 帐户登录时在 VS 代码中“未找到订阅”
- floating-point - Why is "The [integer<->float] conversion strategy is changed depending on the size of the float" in Erlang and how can I observe this?