-
Notifications
You must be signed in to change notification settings - Fork 0
/
gui.py
166 lines (124 loc) · 5.11 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
from Tkinter import *
import tkMessageBox
class Interface:
def __init__(self):
self.window = Tk()
self.window.title("Music Player")
def show_player(self, songs, player):
def cmd_play_pause(*args):
player.play_pause()
def cmd_next(*args):
player.play(1)
def cmd_prev(*args):
player.play(-1)
def cmd_volume(vol):
player.set_volume(vol)
def cmd_list(event):
index = Lb.nearest(event.y)
if index > -1:
player.play_by_index(index)
def on_song_change(song):
# title
song_label.set(song.title)
curr = Lb.curselection()
if curr:
Lb.select_clear(curr[0])
# song list
index = songs.index(song)
Lb.select_set(index)
Lb.see(index)
Lb.activate(index)
# song progress
progress.configure(to=song.duration)
print('set song duration: {}'.format(song.duration))
self.disable_progress_update = False
def cmd_progress(e):
prog = progress.get() / float(progress.cget('to'))
print('set progress: {}'.format(prog))
player.set_progress(prog)
self.disable_progress_update = False
def cmd_progress_click(e):
# disable auto update
self.disable_progress_update = True
# fix default increment
# this cause crash program
# direct = progress.identify(e.x, e.y)
# if direct == 'trough1':
# inc = -1
# elif direct == 'trough2':
# inc = 1
# else:
# inc = 0
# if inc:
# sec = player.inc_progress(inc)
# progress.set(sec)
def on_play_progress(percent):
if not self.disable_progress_update:
progress.set(percent * progress.cget('to'))
player.on_song_change = on_song_change
player.on_play_progress = on_play_progress
self.clear_frame(self.window)
menu = Frame(self.window, relief=RAISED, pady=6, padx=6)
song_label = StringVar()
Label(menu, textvariable=song_label, pady=4).pack()
Button(menu, text="Play/Pause", command=cmd_play_pause).pack(side=LEFT)
Button(menu, text="Prev", command=cmd_prev).pack(side=LEFT)
Button(menu, text="Next", command=cmd_next).pack(side=LEFT)
volume = Scale(menu, from_=0, to=100, orient=HORIZONTAL, showvalue=0,
length=150, sliderlength=20, command=cmd_volume)
volume.pack(side=RIGHT)
volume.set(100)
progress = Scale(menu, from_=0, to=100, orient=HORIZONTAL, showvalue=0,
length=300, sliderlength=20)
progress.bind("<Button-1>", cmd_progress_click)
progress.bind("<ButtonRelease-1>", cmd_progress)
progress.pack(side=BOTTOM, fill=X, expand=1)
menu.pack(fill=X, side=TOP)
frame = Frame(self.window, relief=RAISED, borderwidth=1, height=50)
Lb = Listbox(frame)
scrollbar = Scrollbar(frame, command=Lb.yview)
scrollbar.pack(side=RIGHT, fill=Y)
Lb.configure(yscrollcommand=scrollbar.set)
for song in songs:
Lb.insert(END, song.title)
Lb.bind("<Button-1>", cmd_list)
Lb.pack(fill=BOTH, expand=1)
logout_link = Label(frame, text="Logout")
logout_link.pack()
logout_link.bind("<Button-1>", self.logout)
frame.pack(fill=BOTH, side=BOTTOM, expand=1)
if not player.paused:
on_song_change(songs[player.current_song_index])
def show_login(self):
self.clear_frame(self.window)
login_frame = Frame(self.window, pady=10, padx=10)
Label(login_frame, text="Username").grid(row=0, sticky=E)
Label(login_frame, text="Password").grid(row=1, sticky=E)
login_input = Entry(login_frame)
pass_input = Entry(login_frame, show="*")
login_input.grid(row=0, column=1)
pass_input.grid(row=1, column=1)
def _login_btn_clicked():
username = login_input.get().strip()
password = pass_input.get().strip()
if username and password:
if self.on_credentials_entered(username, password):
tkMessageBox.showinfo("Login info", "Success!")
else:
tkMessageBox.showerror("Login error", "Incorrect login or password entered!")
else:
tkMessageBox.showerror("Login error", "Login and password both required!")
Button(login_frame, text="Login", command=_login_btn_clicked).grid(columnspan=2)
login_frame.pack()
@staticmethod
def clear_frame(frame):
for widget in frame.winfo_children():
Interface.clear_frame(widget)
widget.destroy()
def on_credentials_entered(self, login, password):
pass
def logout(self, *args):
print(args)
def update(self):
self.window.update_idletasks()
self.window.update()