source: framspy/gui/widgets/ConsoleWindow.py @ 1277

Last change on this file since 1277 was 1202, checked in by Maciej Komosinski, 22 months ago

Introduced common utility functions for setting window sizes; untangled some repeated code

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