How do I create a date picker in tkinter?

前端 未结 7 1343
借酒劲吻你
借酒劲吻你 2020-11-28 09:45

Is there any standard way tkinter apps allow the user to choose a date?

相关标签:
7条回答
  • 2020-11-28 10:27

    Not as far as I could find. For anyone who wants to do this in the future:

    I used tkSimpleDialog and ttkcalendar.py (with modifications from this SO post) to make a CalendarDialog. The modified versions of the three files are available on my github.

    Below is the code in my CalendarDialog.py:

    import Tkinter
    
    import ttkcalendar
    import tkSimpleDialog
    
    class CalendarDialog(tkSimpleDialog.Dialog):
        """Dialog box that displays a calendar and returns the selected date"""
        def body(self, master):
            self.calendar = ttkcalendar.Calendar(master)
            self.calendar.pack()
    
        def apply(self):
            self.result = self.calendar.selection
    
    # Demo code:
    def main():
        root = Tkinter.Tk()
        root.wm_title("CalendarDialog Demo")
    
        def onclick():
            cd = CalendarDialog(root)
            print cd.result
    
        button = Tkinter.Button(root, text="Click me to see a calendar!", command=onclick)
        button.pack()
        root.update()
    
        root.mainloop()
    
    
    if __name__ == "__main__":
        main()
    
    0 讨论(0)
提交回复
热议问题