1
+ '''
2
+ Created on Oct 21, 2017
3
+
4
+ @author: Aditya
5
+ This program demonstrates the use of spinbox and combobox in tkinter
6
+ '''
7
+
8
+ import tkinter as tk
9
+ from tkinter import ttk
10
+ import datetime
11
+ import calendar
12
+
13
+ class simpleCalender :
14
+ '''
15
+ This class creates a simple date of birth app using tkinter
16
+ '''
17
+ def __init__ (self , master ):
18
+ ttk .Label (master , text = 'DATE OF BIRTH' ).pack ()
19
+ #####################################################################
20
+
21
+ ttk .Label (master , text = 'Select Year' ).pack ()
22
+ self .year = tk .StringVar ()
23
+ self .spinbxyear = tk .Spinbox (master , from_ = 1900 ,
24
+ to = datetime .datetime .now ().year ,
25
+ textvariable = self .year )
26
+ self .spinbxyear .pack ()
27
+
28
+ #####################################################################
29
+
30
+ ttk .Label (master , text = 'Select Month' ).pack ()
31
+ self .month = tk .StringVar ()
32
+ self .combobox = ttk .Combobox (master , textvariable = self .month ) # textvariable - variable tied to value selected in combobox
33
+ self .combobox .pack ()
34
+
35
+ # values which combobox can take
36
+ self .combobox .config (values = ('January' , 'February' , 'March' , 'April' ,
37
+ 'May' , 'June' , 'July' , 'August' , 'September' ,
38
+ 'October' , 'November' , 'December' ))
39
+ self .combobox .set ('January' )
40
+
41
+ #####################################################################
42
+
43
+ ttk .Label (master , text = 'Select Date' ).pack ()
44
+ self .dateofmonth = tk .StringVar ()
45
+ self .lastday = 31
46
+ self .spinbxday = tk .Spinbox (master , from_ = 1 , to = 31 ,
47
+ textvariable = self .dateofmonth )
48
+ self .spinbxday .pack ()
49
+
50
+ #####################################################################
51
+
52
+ ttk .Button (master , text = "Get Date of Birth" , command = self .getDOB ).pack ()
53
+
54
+ #####################################################################
55
+ # Display Date of Birth
56
+ self .displaydob = ttk .Label (master , text = "Please select a date" )
57
+ self .displaydob .pack ()
58
+
59
+ ################ METHODS ####################################################
60
+ def getyear (self ):
61
+ if int (self .year .get ()) in range (int (self .spinbxyear ['from' ]), int (self .spinbxyear ['to' ]+ 1 )):
62
+ return int (self .year .get ())
63
+ else :
64
+ return - 1
65
+
66
+ def getmonth (self ):
67
+ if self .month .get () in self .combobox ['values' ]:
68
+ return self .month .get ()
69
+ else :
70
+ return - 1
71
+
72
+ def getdate (self ):
73
+ if int (self .dateofmonth .get ()) in range (1 , 32 ):
74
+ return int (self .dateofmonth .get ())
75
+ else :
76
+ return - 1
77
+
78
+ def getDOB (self ):
79
+ year = self .getyear ()
80
+ month = self .getmonth ()
81
+
82
+ if calendar .isleap (year ) and month == 'February' :
83
+ self .lastday = 29
84
+ elif not calendar .isleap (year ) and month == 'February' :
85
+ self .lastday = 28
86
+ elif month in ('January' , 'March' , 'May' , 'July' , 'August' , 'October' , 'December' ):
87
+ self .lastday = 31
88
+ elif month in ('April' , 'June' , 'September' , 'November' ):
89
+ self .lastday = 30
90
+ else :
91
+ self .lastday = - 1
92
+
93
+ if self .getdate () > self .lastday or self .getdate ()< 1 or self .lastday == - 1 :
94
+ date = - 1
95
+ else :
96
+ date = self .getdate ()
97
+
98
+ if year != - 1 and month != - 1 and date != - 1 :
99
+ self .displaydob .config (text = '{0} {1}, {2}' .format (month , date , year ))
100
+ else :
101
+ self .displaydob .config (text = "Invalid Date" )
102
+
103
+ def launchSimpleCalenderApp ():
104
+ root = tk .Tk ()
105
+ simpleCalender (root )
106
+ tk .mainloop ()
107
+
108
+ def test ():
109
+ launchSimpleCalenderApp ()
110
+
111
+ if __name__ == '__main__' : test ()
0 commit comments