This repository has been archived by the owner on Jun 25, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gui.py
148 lines (129 loc) · 5.18 KB
/
gui.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
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# -*- coding: utf-8 -*-
# Copyright (C) 2017 by RedFantom
# For license see LICENSE
import tkinter as tk
from tkinter import ttk
from queue import Queue
from tkinter import filedialog as fd
from ttkthemes.themed_tk import ThemedTk
# Own modules
from tools import Settings, ColorScheme, SettingsWindow, OCRLoop
from simpleocr import open_image
from tools.utilities import get_assets_directory
import os
class MainWindow(ThemedTk):
"""
Main program GUI window
"""
def __init__(self):
"""
Create all widgets and call grid_widgets() to set them up
"""
ThemedTk.__init__(self)
self.set_theme("arc")
self.style = ttk.Style()
self.style.configure("TButton", font=("default", 10))
self.style.configure("TLabel", font=("default", 10))
self.style.configure(".", font=("default", 10))
self.settings = Settings()
self.colors = ColorScheme()
self.colors.set_scheme(self.settings["colors"])
self.protocol("WM_DELETE_WINDOW", self.exit)
self.bind("<Configure>", self.grid_widgets)
self.resizable(width=False, height=False)
self.wm_title("SWTOR Chat Logger")
# Create menu and add it to window
self.menu = tk.Menu(self)
self.config(menu=self.menu)
# Add file menu and its subcommands
self.filemenu = tk.Menu(self.menu, tearoff=0)
self.filemenu.add_command(label="Open file", command=self.open_file)
self.filemenu.add_command(label="Save as", command=self.save_to_file)
self.filemenu.add_separator()
self.filemenu.add_command(label="Exit", command=self.exit)
self.menu.add_cascade(label="File", menu=self.filemenu)
# Add edit menu and its subcommands
self.editmenu = tk.Menu(self.menu, tearoff=0)
self.editmenu.add_command(label="Preferences", command=self.show_settings)
self.menu.add_cascade(label="Edit", menu=self.editmenu)
# Add all main widgets
self.text_widget = tk.Listbox(self, font=("Consolas", 10), height=16, width=60)
self.text_widget.bind("<<ListBoxSelect>>", self.set_highlight_color)
# Create variables for keeping track of lines
self.line = 0
self.line_types = {}
self.scrollbar = ttk.Scrollbar(self, orient=tk.VERTICAL)
self.scrollbar.config(command=self.text_widget.yview)
self.text_widget.config(yscrollcommand=self.scrollbar.set)
self.start_logging_button = ttk.Button(self, text="Start logging", command=self.start_stop_logging)
self.logging = False
self.logger = None
self.current_file = None
self.exit_queue = Queue()
self.insert_into_listbox("self", "SWTOR Chat Logger (C) Copyright by RedFantom 2017")
self.grid_widgets()
def grid_widgets(self, *args):
self.text_widget.grid(row=0, column=0, sticky="nswe")
self.start_logging_button.grid(row=1, column=0, columnspan=2, sticky="nswe", padx=5, pady=5)
self.scrollbar.grid(row=0, column=1, sticky="ns")
def exit(self):
if self.logging:
self.stop_logging()
self.destroy()
exit()
def start_stop_logging(self):
if not self.logging:
image_path = os.path.join(get_assets_directory(), "template.png")
image = open_image(image_path)
self.start_logging(ground=image.is_grounded)
else:
self.stop_logging()
self.logging = not self.logging
def start_logging(self, ground=False):
self.logger = OCRLoop("Redfantom", callback=self.insert_into_listbox, exitq=self.exit_queue)
if not ground:
self.logger.train()
self.logger.start()
def stop_logging(self):
self.exit_queue.put(True)
while self.logger.is_alive():
pass
self.logger = None
def save_to_file(self):
filename = fd.asksaveasfilename(parent=self, title="Save log",
filetypes=[("SWTOR Chat Log", ".swl"), ("Plain text", ".txt")],
defaultextension=".swl")
def read_from_file(self):
pass
def open_file(self):
self.read_from_file()
def show_settings(self):
SettingsWindow(self, self.settings)
def insert_into_listbox(self, type, string, line=None):
if not line:
line = self.line
for item in self.wrap_line(string):
self.text_widget.insert(line, item)
self.text_widget.itemconfig(line, foreground=self.colors[type][0], background=self.colors[type][1])
self.line += 1
line += 1
self.line_types[line] = type
self.text_widget.see(tk.END)
@staticmethod
def wrap_line(string):
words = string.split(" ")
temp = ""
results = []
for word in words:
if not len(temp + " " + word) > 60:
temp += " " + word
else:
results.append(temp)
temp = ""
for result in results:
yield result
def set_highlight_color(self, *args):
print(self.text_widget.curselection())
if __name__ == '__main__':
window = MainWindow()
window.mainloop()