-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgui.py
More file actions
113 lines (88 loc) · 4.14 KB
/
gui.py
File metadata and controls
113 lines (88 loc) · 4.14 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
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
from tkinter import *
from typing import List, Union
class ColorData:
start_index: int
end_index: int
color: str
def __init__(self, start_index: int, end_index: int, color: str):
self.start_index = start_index
self.end_index = end_index
self.color = color
class ChatWindow:
bg_color = "#303030"
fg_color = "#d0d0d0"
active_bg = "#404040"
active_fg = "#e0e0e0"
select_bg = "#5050f0"
default_font = ("Courier", 10)
button_style = {"background": bg_color,
"foreground": fg_color,
"activebackground": active_bg,
"activeforeground": active_fg,
"relief": RAISED,
"font": default_font}
label_style = {"background": bg_color,
"foreground": fg_color,
"relief": FLAT,
"font": default_font}
frame_style = {"background": bg_color,
"borderwidth": 0}
listbox_style = {"background": active_bg,
"foreground": active_fg,
"font": default_font,
"relief": FLAT,
"highlightthickness": 0,
"selectbackground": select_bg}
text_style = {"background": active_bg,
"foreground": active_fg,
"font": default_font,
"relief": FLAT,
"highlightthickness": 0,
"selectbackground": select_bg,
"insertbackground": active_fg}
window: Tk
chat_box: Text
bottom_frame: Frame
input_box: Entry
select_color: Button
def __init__(self):
# TODO: Make default size based on native resolution
# This probably won't happen because it would be annoying to do it cross-platform
# (and I'd rather not have it at all than have it be platform-dependent)
self.window = Tk()
self.window.config(background=self.bg_color)
self.window.title("Python Chat Application")
self.chat_box = Text(self.window, **self.text_style)
self.chat_box.config(state=DISABLED, width=40, height=8, wrap=WORD)
self.chat_box.pack(side=TOP, padx=4, pady=4, expand=TRUE, fill=BOTH)
self.bottom_frame = Frame(self.window, **self.frame_style)
self.input_box = Entry(self.bottom_frame, **self.text_style)
self.input_box.config(width=40)
self.input_box.pack(side=LEFT, padx=4, pady=4, expand=TRUE, fill=X)
self.select_color = Button(self.bottom_frame, **self.button_style)
self.select_color.config(text="Set name color")
self.select_color.pack(side=RIGHT, padx=4, pady=4)
self.bottom_frame.pack(side=TOP, expand=FALSE, fill=BOTH, ipadx=0, ipady=0, padx=0, pady=0)
def log_msg(self, msg: str, colors: Union[List[ColorData], str] = None):
self.chat_box.config(state=NORMAL)
view_is_bottomed = self.chat_box.yview()[1] == 1
self.chat_box.mark_set("prev_end", END + " - 1 chars") # Weird tk behavior makes the - 1 chars necessary.
self.chat_box.mark_gravity("prev_end", LEFT)
self.chat_box.insert(END, msg + "\n")
if colors is not None:
if type(colors) == list:
for col_dat in colors:
self.chat_box.tag_add(col_dat.color,
"prev_end + {} chars".format(col_dat.start_index),
"prev_end + {} chars".format(col_dat.end_index))
self.chat_box.tag_configure(col_dat.color, foreground=col_dat.color)
else:
self.chat_box.tag_add(colors, "prev_end", END + " - 1 chars")
self.chat_box.tag_configure(colors, foreground=colors)
if view_is_bottomed: # If already scrolled down all the way, keep it so
self.chat_box.yview_moveto(1)
self.chat_box.config(state=DISABLED)
def clear_entry(self):
self.input_box.delete(0, END)
def get_entry(self) -> str:
return self.input_box.get()