1 | import tkinter as tk
|
---|
2 | from gui.widgets.ScrolledText import ScrolledText
|
---|
3 | from gui.framsutils.FramsSocket import FramsSocket
|
---|
4 | from typing import Tuple, Callable
|
---|
5 |
|
---|
6 | class ConsoleWindow(tk.Toplevel):
|
---|
7 | def __init__(self, master, frams: FramsSocket) -> None:
|
---|
8 | super().__init__(master)
|
---|
9 |
|
---|
10 | self.frams: FramsSocket = frams
|
---|
11 | self.frams.registerMessageEventCallback(self.messageCallbackEvent)
|
---|
12 |
|
---|
13 | self.protocol("WM_DELETE_WINDOW", self._dismiss)
|
---|
14 | self.title("Console")
|
---|
15 |
|
---|
16 | self.frame_main = tk.Frame(master=self)
|
---|
17 |
|
---|
18 | self.console = ScrolledText(self.frame_main, height=10, state="disabled")
|
---|
19 | self.console.grid(row=0, column=0, columnspan=2, sticky="NSEW")
|
---|
20 |
|
---|
21 | self.input = tk.Entry(self.frame_main)
|
---|
22 | self.input.bind("<Return>", self.buttonSendCommand)
|
---|
23 | self.input.grid(row=1, column=0, sticky="NSEW")
|
---|
24 |
|
---|
25 | self.sendButton = tk.Button(self.frame_main, text="Send", command=self.buttonSendCommand)
|
---|
26 | self.sendButton.grid(row=1, column=1, sticky="SE")
|
---|
27 |
|
---|
28 | self.frame_main.rowconfigure(0, weight=1)
|
---|
29 | self.frame_main.rowconfigure(1, weight=0)
|
---|
30 |
|
---|
31 | self.frame_main.columnconfigure(0, weight=1)
|
---|
32 | self.frame_main.columnconfigure(1, weight=0)
|
---|
33 |
|
---|
34 | self.frame_main.grid(row=0, column=0, sticky="NSEW")
|
---|
35 | self.rowconfigure(0, weight=1)
|
---|
36 | self.columnconfigure(0, weight=1)
|
---|
37 |
|
---|
38 | def parseGeometryString(geometry: str) -> Tuple[int, int, int, int]:
|
---|
39 | widthxrest = geometry.split('x')
|
---|
40 | heightxy = widthxrest[1].split('+')
|
---|
41 | maxwidth = int(widthxrest[0])
|
---|
42 | maxheight = int(heightxy[0])
|
---|
43 | x = int(heightxy[1])
|
---|
44 | y = int(heightxy[2])
|
---|
45 | return maxwidth, maxheight, x, y
|
---|
46 |
|
---|
47 | self.attributes("-alpha", 0)
|
---|
48 | self.update_idletasks()
|
---|
49 |
|
---|
50 | width, height, x, y = parseGeometryString(self.winfo_geometry())
|
---|
51 |
|
---|
52 | self.state('zoomed')
|
---|
53 | self.update()
|
---|
54 | rootx = self.winfo_rootx()
|
---|
55 |
|
---|
56 | maxWidth, maxHeight, x, y = parseGeometryString(self.winfo_geometry())
|
---|
57 |
|
---|
58 | self.state("normal")
|
---|
59 | self.update()
|
---|
60 |
|
---|
61 | x = (maxWidth - width) / 2
|
---|
62 | y = (maxHeight - height) / 2
|
---|
63 |
|
---|
64 | self.geometry("+%d+%d" % (int(rootx + x), int(y)))
|
---|
65 | self.attributes("-alpha", 1)
|
---|
66 | self.update()
|
---|
67 | self.maxsize(maxWidth, maxHeight)
|
---|
68 |
|
---|
69 | def buttonSendCommand(self, event=None):
|
---|
70 | request = self.input.get().strip()
|
---|
71 | if len(request) > 0:
|
---|
72 | self.sendButton.config(state=tk.DISABLED)
|
---|
73 | self.input.delete(0, tk.END)
|
---|
74 |
|
---|
75 | response = self.frams.sendRequest(request)
|
---|
76 |
|
---|
77 | self.console.config(state=tk.NORMAL)
|
---|
78 | self.console.insert(tk.END, ">>> {}\n".format(request))
|
---|
79 | for r in response:
|
---|
80 | self.console.insert(tk.END, r)
|
---|
81 | self.console.see(tk.END)
|
---|
82 | self.console.config(state=tk.DISABLED)
|
---|
83 |
|
---|
84 | self.sendButton.config(state=tk.NORMAL)
|
---|
85 |
|
---|
86 | def _dismiss(self):
|
---|
87 | self.frams.registerMessageEventCallback(None)
|
---|
88 | self.destroy()
|
---|
89 |
|
---|
90 | def messageCallbackEvent(self, block, header):
|
---|
91 | response = self.frams._infoParser(block)
|
---|
92 |
|
---|
93 | self.console.config(state=tk.NORMAL)
|
---|
94 | self.console.insert(tk.END, ">>> {}\n".format(response[0].p["func"]))
|
---|
95 | for r in str(response[0].p["message"]):
|
---|
96 | self.console.insert(tk.END, r)
|
---|
97 | self.console.insert(tk.END, "\n")
|
---|
98 | self.console.see(tk.END)
|
---|
99 | self.console.config(state=tk.DISABLED)
|
---|