Implement a date setter in Tkinter

The source code is as follows.

import tkinter as tk
from tkinter import ttk
import datetime
import calendar

year_list = ['2020', '2021']
month_list = [str(date).zfill(2) for date in range(1, 13)]
date_list = [str(date).zfill(2) for date in range(1, 32)]
hours_list = [str(date).zfill(2) for date in range(24)]
minutes_list = [str(date).zfill(2) for date in range(60)]
seconds_list = [str(date).zfill(2) for date in range(60)]


def change_date():
    def inner(self):
        last_day = calendar.monthrange(int(cb_year.get()), int(cb_month.get()))[1]
        if int(cb_date.get()) > last_day:
            cb_date.set(str(last_day).zfill(2))
        cb_date.config(values=[str(date).zfill(2) for date in range(1, last_day+1)])

        #Output in datetime format
        # date = cb_year.get()+'-'+cb_month.get()+'-'+cb_date.get()+' '+cb_hours.get()+':'+cb_minutes.get()+':'+cb_seconds.get()
        # print("Date = ", date)
    return inner


root = tk.Tk()
root.title('Date Picker')


cb_year = ttk.Combobox(root, values=year_list, width=5, state='readonly')
cb_year.set(year_list[0])
cb_year.bind('<<ComboboxSelected>>', change_date())
cb_year.pack(side='left')

label_slash = ttk.Label(root, text='/')
label_slash.pack(side='left')

cb_month = ttk.Combobox(root, values=month_list, width=5, state='readonly')
cb_month.set(month_list[0])
cb_month.bind('<<ComboboxSelected>>', change_date())
cb_month.pack(side='left')

label_slash = ttk.Label(root, text='/')
label_slash.pack(side='left')

cb_date = ttk.Combobox(root, values=date_list, width=5, state='readonly')
cb_date.set(date_list[0])
cb_date.pack(side='left')

label_space = ttk.Label(root, text=' ')
label_space.pack(side='left')

cb_hours = ttk.Combobox(root, values=hours_list, width=5, state='readonly')
cb_hours.set(hours_list[0])
cb_hours.pack(side='left')

label_colon = ttk.Label(root, text=':')
label_colon.pack(side='left')

cb_minutes = ttk.Combobox(root, values=minutes_list, width=5, state='readonly')
cb_minutes.set(minutes_list[0])
cb_minutes.pack(side='left')

label_colon = ttk.Label(root, text=':')
label_colon.pack(side='left')

cb_seconds = ttk.Combobox(root, values=seconds_list, width=5, state='readonly')
cb_seconds.set(seconds_list[0])
cb_seconds.pack(side='left')

root.mainloop()

Differences in the last day depending on the year and month are handled by the calender module. After startup, the following screen will appear so you can select the date and time. キャプチャ.PNG

Thank you for reading until the end. Let's meet again.

Recommended Posts

Implement a date setter in Tkinter
Implement a custom View Decorator in Pyramid
Implement a Custom User Model in Django
How to implement a gradient picker in Houdini
Implement similar face search in half a day
Insert a date without line breaks in CotEditor
Implement Enigma in python
I want to easily implement a timeout in python
Date manipulation in Python
Tab app in Tkinter
Implement recommendations in Python
I tried to implement a pseudo pachislot in Python
Implement XENO in python
dict in dict Makes a dict a dict
Get date in Python
Date calculation in python
Implement sum in Python
Date calculation in Python
Implement Traceroute in Python 3
I tried to implement a one-dimensional cellular automaton in Python
Loop through a generator that returns a date iterator in Python
Specify a date range with a comparison operator in Python's datetime
How to temporarily implement a progress bar in a scripting language
I made a puzzle game (like) with Tkinter in Python
I tried to make a stopwatch using tkinter in python
Let's create a function to hold down Button in Tkinter
A story about trying to implement a private variable in Python.
Implement LSTM AutoEncoder in Keras
Implement a 3-layer neural network
Take a screenshot in Python
Implement follow functionality in Django
Create a function in Python
Create a dictionary in Python
Embed matplotlib graphs in Tkinter
Implement timer function in pygame
Collaborate in a remote environment
Implement Style Transfer in Pytorch
Implement recursive closures in Go
Implement naive bayes in Python 3.3
Implement UnionFind (equivalent) in 10 lines
Implement ancient ciphers in python
Make a bookmarklet in Python
Implement Redis Mutex in Python
Implement extension field in Python
Implement fast RPC in Python
Implement method chain in Python
Implement Dijkstra's Algorithm in python
Implement Slack chatbot in Python
String date manipulation in Python
Draw a heart in Python
Implement Gaussian process in Pyro
Sort by date in python
Easy! Implement a Twitter bot that runs on Heroku in Python
Make a Spinbox that can be displayed in Binary with Tkinter
Implement a deterministic finite automaton in Python to determine multiples of 3
I tried to implement a misunderstood prisoner's dilemma game in Python
Make a Spinbox that can be displayed in HEX with Tkinter