This repository was archived by the owner on Sep 8, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
100 lines (77 loc) · 3.17 KB
/
app.py
File metadata and controls
100 lines (77 loc) · 3.17 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
import logging
try:
import thread
import Tkinter as tk
import ttk
import ScrolledText as tkst
except ImportError:
import _thread as thread
import tkinter as tk
import tkinter.ttk as ttk
import tkinter.scrolledtext as tkst
from twitchbitsinfo import TwitchBitsInfo
logger = logging.getLogger('twitch_bits_info')
class CMTk(tk.Tk):
def __init__(self, parent=None):
tk.Tk.__init__(self, parent)
self.parent = parent
self.log_text = tkst.ScrolledText(self, state=tk.DISABLED)
self.log_text.configure(font='TkFixedFont')
self.log_text.pack(side=tk.LEFT, padx=5, pady=5)
self.start_button = ttk.Button(parent, text='Start ConsoleMini', state=tk.NORMAL,
command=self.start_twitch_bits_info)
self.start_button.pack(side=tk.TOP, padx=12, pady=12)
self.stop_button = ttk.Button(parent, text='Stop ConsoleMini', state=tk.DISABLED,
command=self.stop_twitch_bits_info)
self.stop_button.pack(side=tk.TOP, padx=12, pady=12)
self.update_button = ttk.Button(parent, text='Manual update JSON', state=tk.DISABLED,
command=self.manual_update_json)
self.update_button.pack(side=tk.TOP, padx=12, pady=12)
def manual_update_json(self):
self.bits.cm.update_trending_games()
def start_twitch_bits_info(self):
self.start_button.config(state=tk.DISABLED)
self.stop_button.config(state=tk.NORMAL)
self.update_button.config(state=tk.NORMAL)
self.bits = TwitchBitsInfo()
def run(*args):
"""
Starts TwitchBitsInfo in another thread
"""
self.bits.start()
thread.start_new_thread(run, ())
def stop_twitch_bits_info(self):
# Shutdown Twitch API access etc...
self.start_button.config(state=tk.NORMAL)
self.stop_button.config(state=tk.DISABLED)
self.update_button.config(state=tk.DISABLED)
self.bits.shutdown()
class TextHandler(logging.StreamHandler):
"""
This class allows you to log to a Tkinter Text or ScrolledText widget.
"""
def __init__(self, text):
# run the regular StreamHandler __init__
logging.StreamHandler.__init__(self)
# Store a reference to the Text it will log to
self.text = text
def emit(self, record):
msg = self.format(record)
def append():
self.text.configure(state=tk.NORMAL)
self.text.insert(tk.END, msg + '\n')
self.text.configure(state=tk.DISABLED)
# Autoscroll to the bottom
self.text.yview(tk.END)
# This is necessary because we can't modify the Text from other threads
self.text.after(0, append)
if __name__ == "__main__":
# Create Tk object instance
app = CMTk()
app.title('ConsoleMini')
# Create TextHandler
text_handler = TextHandler(app.log_text)
logger.addHandler(text_handler)
logger.setLevel(logging.INFO)
# Start Tk mainloop
app.mainloop()