Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x TTKCalendar选择返回Python_Python 3.x_Calendar_Tkinter_Anaconda_Ttk - Fatal编程技术网

Python 3.x TTKCalendar选择返回Python

Python 3.x TTKCalendar选择返回Python,python-3.x,calendar,tkinter,anaconda,ttk,Python 3.x,Calendar,Tkinter,Anaconda,Ttk,我有ttkcalendar,当按下calendar小部件中的日期时,我的程序将更新一个字段 以下是开始日期和结束日期字段: start_date = StringVar() start_date = ttk.Entry(f2, width=15, textvariable=start_date) start_date.grid(column=2, row=1, sticky=E) ttk.Label(f2, text="Start date:", width=10).grid(column=1

我有
ttk
calendar,当按下calendar小部件中的日期时,我的程序将更新一个字段

以下是开始日期和结束日期字段:

start_date = StringVar()
start_date = ttk.Entry(f2, width=15, textvariable=start_date)
start_date.grid(column=2, row=1, sticky=E)

ttk.Label(f2, text="Start date:", width=10).grid(column=1, row=1, sticky=E)

end_date = StringVar()
end_date = ttk.Entry(f2, width=15, textvariable=end_date)
end_date.grid(column=2, row=2, sticky=E)

ttk.Label(f2, text="End date:", width=10).grid(column=1, row=2, sticky=E)
以下是按钮触发的功能:

def callbackCal():
    root2=Toplevel(f2)
    ttkcal = ttkcalendar.Calendar(root2,firstweekday=calendar.SUNDAY)
    ttkcal.pack(expand=1, fill='both')
    root2.update()
    root2.minsize(root2.winfo_reqwidth(), root2.winfo_reqheight())
b=ttk.Button(f2, width=4, text="Cal", command=callbackCal).grid(column=3,row=1, sticky=W)
这是按钮代码:

def callbackCal():
    root2=Toplevel(f2)
    ttkcal = ttkcalendar.Calendar(root2,firstweekday=calendar.SUNDAY)
    ttkcal.pack(expand=1, fill='both')
    root2.update()
    root2.minsize(root2.winfo_reqwidth(), root2.winfo_reqheight())
b=ttk.Button(f2, width=4, text="Cal", command=callbackCal).grid(column=3,row=1, sticky=W)
多亏了帮助,我才走到了这一步。我知道ttk日历有方法_pressed()、_show_selection()和selection()。但我不知道如何使用它们,以便在单击时显示所选日期。并且,一旦完成,就关闭日历小部件


非常感谢!很抱歉这些新手的问题。

我并不假装理解代码,但我找到了另一个问题的答案,建议做一些修改,答案来自kalgasnik

然后我做了这个改变:-

    def __init__(self, master=None, selection_callback=None, **kw):
并将其添加到init函数中

        self.selection_callback = selection_callback
在_pressed函数中,我添加了

        if self.selection_callback:
            self.selection_callback(self.selection)
基本上是在单击日期时添加回调以获取值

我的示例回调程序是:-

import calendar
import tkinter as TK
import tkinter.font
from tkinter import ttk
from ttk_calendar import Calendar
import sys


class Test():
    def __init__(self, root):
        self.root = root
        self.root.title('Ttk Calendar')
        frame = ttk.Frame(self.root)
        frame.pack()
        quit_button = ttk.Button(frame, text="Calendar", command=self.use_calendar)
        quit_button.pack()
        self.calendarroot = None
    def use_calendar(self):
        if not self.calendarroot:
            self.calendarroot=TK.Toplevel(self.root)
            ttkcal = Calendar(master=self.calendarroot, selection_callback=self.get_selection, firstweekday=calendar.SUNDAY)
            ttkcal.pack(expand=1, fill='both')
            self.calendarroot.update()
            self.calendarroot.minsize(self.calendarroot.winfo_reqwidth(), self.calendarroot.winfo_reqheight())
        else:
            self.calendarroot.deiconify() # Restore hidden calendar

    def get_selection(self, selection):
        print (selection)
        self.calendarroot.withdraw() # Hide calendar - if that is what is required

if __name__ == '__main__':
    root = tkinter.Tk()
    x = Test(root)
    root.mainloop()
我试图破坏顶层框架,但出现了一个错误,因此我使用了收回和去锥化,这不是最好的,但至少我得到了一些工作

我知道答案有点混乱,但你可能很乐意想出一个更好的解决方案