-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgui.py
53 lines (42 loc) · 1.46 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
# -*- coding: utf-8 -*-
from __future__ import division, print_function, unicode_literals
from contextlib import contextmanager
from Tkinter import Tk, Text, BOTH, END, DISABLED, NORMAL
from ttk import Frame
@contextmanager
def widget_locker(widget):
try:
widget.config(state=NORMAL)
yield
finally:
widget.config(state=DISABLED)
class GUI(Frame):
def __init__(self, parent, data_source, lyric_source):
Frame.__init__(self, parent)
self.parent = parent
self.data = data_source(lyric_source)
# UI
self.pack(fill=BOTH, expand=True)
self.txt = Text(self)
self.txt.pack(fill=BOTH, pady=5, padx=5, expand=True)
self.txt.after(500, self.refresh_lyric)
def update_window_title(self):
title = "{} - {}".format(self.data.artist, self.data.title)
if self.data.status:
title += " [{}]".format(self.data.status)
self.parent.title(title)
def refresh_lyric(self):
self.data.process()
if self.data.updated:
self.update_window_title()
with widget_locker(self.txt):
self.txt.delete(1.0, END)
self.txt.insert(END, self.data.lyrics)
self.txt.update()
self.txt.after(500, self.refresh_lyric)
def run_ui(data_source, lyric_source):
root = Tk()
root.geometry('400x400')
root.title('claufield')
GUI(root, data_source, lyric_source)
root.mainloop()