python日历小部件-返回用户选择的日期 [英] python calendar widget - return the user-selected date

查看:375
本文介绍了python日历小部件-返回用户选择的日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ttk日历类基于tkinter的日历。如何使其返回所选日期的值?以下是我尝试过的操作,它返回了 NoneType对象不可调用:

This ttk calendar class makes a calendar based on tkinter. How do make it return the value of the selected date? Below is what I tried, it returned 'NoneType object is not callable':

def test():
    import sys
    root = Tkinter.Tk()
    root.title('Ttk Calendar')
    ttkcal = Calendar(firstweekday=calendar.SUNDAY)
    ttkcal.pack(expand=1, fill='both')

    x = ttkcal.selection()  #this and the following line are what i inserted
    print 'x is: ', x  #or perhaps return x

    if 'win' not in sys.platform:
        style = ttk.Style()
        style.theme_use('clam')
    root.mainloop()

if __name__ == '__main__':
    test()


推荐答案

选择是@属性,因此需要执行以下代码才能执行代码:

Selection is a @property, so you need to the following to have your code exectued:

x = ttkcal.selection

此外,使用此日历,您还可以在您关闭callendar小部件(即在mainloop()之后)。因此,您的代码应为:

Also, using this calendar, you can get the selected date, after you close the callendar widget (i.e. after mainloop()). Thus your code should be:

def test2():
    import sys
    root = Tkinter.Tk()
    root.title('Ttk Calendar')
    ttkcal = Calendar(firstweekday=calendar.SUNDAY)
    ttkcal.pack(expand=1, fill='both')

    if 'win' not in sys.platform:
        style = ttk.Style()
        style.theme_use('clam')

    root.mainloop()

    x = ttkcal.selection    
    print 'x is: ', x  

以防万一。如果您不想关闭日历窗口以获取选定的值,但是希望在单击它们时使它们实时显示,例如在其他窗口的标签中显示它们,您可以执行以下操作:

Just in case. If you dont want to close the calendar window to get the selected value, but you want to get them "live" as they are clicked, to e.g. display them in other window's label, you can do the following:

首先扩展Calendar类以添加每次选择某个日期时都会调用的回调函数:

First extend the Calendar class to add the callback function that will be called each time you select some date:

class Calendar2(Calendar):
    def __init__(self, master=None, call_on_select=None, **kw):
        Calendar.__init__(self, master, **kw)
        self.set_selection_callbeck(call_on_select)

    def set_selection_callbeck(self, a_fun):
         self.call_on_select = a_fun


    def _pressed(self, evt):
        Calendar._pressed(self, evt)
        x = self.selection
        #print(x)
        if self.call_on_select:
            self.call_on_select(x)

有这样,您可以创建新的test2示例,其中有两个窗口。一个用于日历,另一个用于带有标签的窗口(例如):

With this, you can make the new test2 example, which has two windows. One for Calendar, and one window with some label (for instance):

class SecondFrame(Tkinter.Frame):

    def __init__(self, *args, **kwargs):
        Tkinter.Frame.__init__(self, *args, **kwargs)
        self.l = Tkinter.Label(self, text="Selected date")
        self.l.pack()
        self.pack()

    def update_lable(self, x):
        self.l['text'] = x;



def test2():
    import sys
    root = Tkinter.Tk()
    root.title('Ttk Calendar')


    ttkcal = Calendar2(firstweekday=calendar.SUNDAY)
    ttkcal.pack(expand=1, fill='both')

    if 'win' not in sys.platform:
        style = ttk.Style()
        style.theme_use('clam')           


    sf = SecondFrame(Tkinter.Toplevel())

    ttkcal.set_selection_callbeck(sf.update_lable)        

    root.mainloop()

在此示例中,每次在日历中选择某个日期时,都会更新SecondFrame中的标签。

In this example, the label in SecondFrame, will be updated each time you select some date in the calendar.

< img src = https://i.stack.imgur.com/W7SS9.png alt =在此处输入图片描述>

这篇关于python日历小部件-返回用户选择的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆