-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtextbox.py
More file actions
43 lines (32 loc) · 1.34 KB
/
textbox.py
File metadata and controls
43 lines (32 loc) · 1.34 KB
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
# search bar that handles the backend for typing into a text box
class TextBox:
def __init__(self, cursor = '_'):
self.cursor_pos = 0
self.typed_string = []
self.cursor = cursor
def insert_letter(self, letter):
self.typed_string.insert(self.cursor_pos, letter)
self.cursor_pos += 1
def clear_searchbar(self):
self.typed_string = []
self.cursor_pos = 0
def backspace(self):
if self.cursor_pos > 0:
self.typed_string = self.typed_string[:self.cursor_pos - 1] + self.typed_string[self.cursor_pos:]
self.cursor_pos = max(self.cursor_pos - 1, 0)
def delete(self):
if self.cursor_pos < len(self.typed_string):
self.typed_string = self.typed_string[:self.cursor_pos] + self.typed_string[self.cursor_pos + 1:]
def cursor_left(self):
self.cursor_pos = max(self.cursor_pos - 1, 0)
def cursor_right(self):
self.cursor_pos = min(self.cursor_pos + 1, len(self.typed_string))
def home(self):
self.cursor_pos = 0
def end(self):
self.cursor_pos = len(self.typed_string)
def get_string(self):
return ''.join(self.typed_string)
def get_string_with_cursor(self):
string_out = self.get_string()
return string_out[:self.cursor_pos] + self.cursor + string_out[self.cursor_pos:]