【问题标题】:How save date from Calendar in tkcalendar (Python)?如何在 tkcalendar (Python) 中保存日历中的日期?
【发布时间】:2018-06-12 10:10:13
【问题描述】:

我想将日历中的选定日期保存到变量中。这是我找到的代码,但我不明白如何保存日期。

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

【问题讨论】:

    标签: python tkinter tkcalendar


    【解决方案1】:

    在我给你工作代码之前,我想告诉你:

    1) 在 tk(和 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()
    

    【讨论】:

    • 您好,感谢您的回答,但这并不是我所需要的。对不起,我的错没有很准确地描述问题。我需要将日历中选定的值写入内存,以后可以将它们用作具有值的变量。
    • 如果您要使用变量,这很容易。如果使用类会更容易,因为你可以在类的任何地方访问变量
    • 我不确定如何使用类。我修改了这样的函数,但我仍然无法获取值..:def print_sel(): a=str(cal.selection_get()) print(a) return a
    • 试试这个。它必须运作良好。并且您可以从不同的上课地点访问,并且可以随时使用
    • 是的,它有效。非常感谢。所以我可以用这个命令获取数据:date1=tt.last_date
    【解决方案2】:

    因为在 tkinter 中无法从 Button 命令返回任何内容,处理此类事情的最简单方法是将其包装在一个类中并使用类变量来存储您生成的数据。我添加了wait_windowgrab_set 命令以使根窗口等待不可点击,直到日历选择器窗口关闭。

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

    如果此代码对您没有意义,请参阅this post 并尝试深入了解类及其工作原理。如您所见,在我的示例中,我没有将继承合并到tk.Frametk.Toplevel,因为我想这会使您更加复杂,但我真的建议您尝试彻底理解和使用结构这个答案。从长远来看,它将对您有所帮助。

    【讨论】:

    • 这段代码对我来说很有意义,现在我了解 Button 命令,在这种情况下需要使用 Class。非常感谢您的帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-21
    • 2021-07-11
    • 2019-12-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多