-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathuser_credentials_dialog.py
73 lines (53 loc) · 2.63 KB
/
user_credentials_dialog.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
import tkinter as tk
class UserCredentialsDialog:
"""Create input dialog with username and password fields"""
def __init__(self) -> None:
# initialize entry fields
self.username_entry = None
self.password_entry = None
# initialize tk
self.root = tk.Tk()
self.root.title('Provide Credentials')
# declare padding for modal elements
self.padding = {'padx': 50, 'pady': 5}
# set the window's dimensions and position
self.window_width, self.window_height = self.calculate_dialog_dimensions()
self.x, self.y = self.calculate_dialog_pos()
self.root.geometry(f'{self.window_width}x{self.window_height}+{self.x}+{self.y}')
# create input fields
username_label = tk.Label(self.root, text="username:")
username_entry = tk.Entry(self.root)
username_entry.focus_set() # set focus on the firs entry element
username_label.pack(self.padding)
username_entry.pack(self.padding)
password_label = tk.Label(self.root, text="password:")
password_entry = tk.Entry(self.root, show='*') # show='*' -> hides characters
password_label.pack(self.padding)
password_entry.pack(self.padding)
def handle_input_values() -> None:
username = username_entry.get()
password = password_entry.get()
# kill dialog
self.root.destroy()
print(username, password) # TODO: delete it later
# save username and password as instance variables
self.username_entry = username
self.password_entry = password
# handle submitting
submit_button = tk.Button(self.root, text="Submit", command=handle_input_values)
self.root.bind('<Return>', lambda event=None: submit_button.invoke()) # submit dialog on enter click
submit_button.pack(self.padding)
self.root.mainloop()
def calculate_dialog_dimensions(self) -> (int, int):
"""Calculate the dimensions of the dialog based on your device's screen resolution"""
width = self.root.winfo_screenwidth()
height = self.root.winfo_screenheight()
return width // 7, height // 6
def calculate_dialog_pos(self) -> (int, int):
"""Calculate where to position the dialog on your screen"""
x = (self.root.winfo_screenwidth() - self.window_width) // 2
y = (self.root.winfo_screenheight() - self.window_height) // 3
return x, y
def get_credentials(self) -> (str, str):
"""Getter for user credentials (username, password)"""
return self.username_entry, self.password_entry