forked from leenlab2/trip-planner-paris
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinput.py
137 lines (116 loc) · 5.29 KB
/
input.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
"""This module gathers input from the user about which hotel they are staying at, how long they are
staying for, when they would like to leave from and return to the hotel, and, optionally, any
locations they want to visit.
This file is Copyright (c) 2021 Leen Al Lababidi, Michael Rubenstein, Maria Becerra and Nada Eldin
"""
import tkinter
from tkinter.ttk import Combobox
from tkinter import messagebox
import datetime
class PopUp:
""" A Tk object used to preset the windows popup settings, display and functions.
Instance Attributes:
- root: holds the Tk object representing the window
- cb: allows us to make the drop-down menu
- t1: the leave time input box
- t2: the return time input box
- user_input: A dictionary containing the input of the user, which can be filled
through the get_user_input function
"""
root: tkinter.Tk
cb: Combobox
t1: tkinter.Entry
t2: tkinter.Entry
user_input: dict
def __init__(self, win: tkinter.Tk, hotel_options: tuple) -> None:
"""Initialize a pop up window with multiple graphical elements
"""
self.root = win
lbl1 = tkinter.Label(win, text='Hotel')
lbl2 = tkinter.Label(win, text='Leave time')
lbl3 = tkinter.Label(win, text='Return time')
# UI Scroll down options for hotel
var = tkinter.StringVar()
var.set("one")
data = hotel_options
self.cb = Combobox(win, values=data)
self.cb.place(x=200, y=50)
# UI Text input options
self.t1 = tkinter.Entry(bd=3)
self.t1.insert(0, 'YYYY-MM-DD HH:MM')
self.t2 = tkinter.Entry()
self.t2.insert(0, 'YYYY-MM-DD HH:MM')
# UI Button customization
lbl1.place(x=100, y=50)
self.t1.place(x=200, y=100)
lbl2.place(x=100, y=100)
self.t2.place(x=200, y=150)
lbl3.place(x=100, y=150)
b1 = tkinter.Button(win, text='Submit', command=self.get_user_input)
b1.place(x=150, y=250)
b2 = tkinter.Button(win, text='Close', command=win.destroy)
b2.place(x=250, y=250)
def get_user_input(self) -> None:
"""Return a dictionary containing data inputted by the user, using a graphical user
interface implemented using tkinter.
The returned dictionary will contain the keys 'hotel', 'leave', 'return',
'locations'.
- 'hotel' contains the <name> of the hotel the user is staying at. It is selected from a
drop-down menu of a few select hotels from the dataset
- 'leave' is the time the user plans to leave the hotel. It is a datetime object
referring to a date and a particular hour in the day.
- 'return' is the time the user prefers to return to the hotel. It is a datetime object
referring to a date and a particular hour in the day. It is after 'leave' time.
Preconditions
- both 'leave' and 'return' must be datetime objects with the format
YYYY MMM DD - HH:MM:SS.MICROS
- 'return' > 'leave'
"""
# Retrieve data from the hotel
hotel = self.cb.get()
# Retrieve strings for the date time objects
leave_raw = self.t1.get()
# Converts string objects into datetime objects
dt_tuple_leave = tuple([int(x) for x in leave_raw[:10].split('-')]) + tuple(
[int(x) for x in leave_raw[11:].split(':')])
datetimeobj1 = datetime.datetime(*dt_tuple_leave)
# Retrieve strings for the date time objects
returns_raw = self.t2.get()
# Converts string objects into datetime objects
dt_tuple_return = tuple([int(x) for x in returns_raw[:10].split('-')]) + tuple(
[int(x) for x in returns_raw[11:].split(':')])
datetimeobj2 = datetime.datetime(*dt_tuple_return)
# Compiles the retirieved input into a dictionary
input_dict = {
'hotel': hotel,
'leave': datetimeobj1,
'return': datetimeobj2
}
self.user_input = input_dict
if datetimeobj1 >= datetimeobj2:
messagebox.showerror('Times incorrect',
'Please ensure that return time is after leaving time')
elif datetimeobj1.day != datetimeobj2.day:
messagebox.showerror('Times incorrect',
'Please ensure that return time is after leaving time')
def open_input_window(hotels: tuple) -> PopUp:
"""Opens a window that allows the user to input their preferences."""
window = tkinter.Tk()
mywin = PopUp(window, hotels)
window.title('User Input Settings')
window.geometry("400x300+10+10")
window.mainloop()
return mywin
if __name__ == "__main__":
import doctest
doctest.testmod()
import python_ta
python_ta.check_all(config={
'extra-imports': ['tkinter', 'datetime', 'tkinter.ttk'],
'allowed-io': [], # the names (strs) of functions that call print/open/input
'max-line-length': 100,
'disable': ['E1136']
})
import python_ta.contracts
python_ta.contracts.DEBUG_CONTRACTS = False
python_ta.contracts.check_all_contracts()