-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathossas.py
211 lines (171 loc) · 7.25 KB
/
ossas.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
from tkinter import PhotoImage, Listbox, StringVar, Button as Btn
import customtkinter
from PIL import Image, ImageTk
from lecteur import lecteur
config = lecteur.read_config(".config")
# Modes: "System" (standard), "Dark", "Light"
customtkinter.set_appearance_mode(config["mode"])
customtkinter.set_default_color_theme(config["theme"])
class MusicPlayer(customtkinter.CTk):
def __init__(self) -> None:
super().__init__()
self.title("ossas Music player")
self.geometry("1000x500")
self.__titre_text = StringVar()
self.__play_image = PhotoImage(file="assets/play-button.png")
self.__pause_image = PhotoImage(file="assets/pause.png")
self.__text_image = PhotoImage(file="assets/next.png")
self.__prev_image = PhotoImage(file="assets/previous.png")
self.__album_image = customtkinter.CTkImage(
Image.open("assets/music.png"), size=(150, 150)
)
self.__play_status: bool = False
self.__theme: str = "white"
self.__activate_now: int = 0
self.__scrolloff: int = 8
self.__position = 0
self.__pathMp3Dict: dict = lecteur.find_audio_file(config["path"])
self.__all_mp3_title: list[str] = [key for key in self.__pathMp3Dict]
self.__container = customtkinter.CTkFrame(self)
self.__left_frame = customtkinter.CTkFrame(self.__container)
self.__search_frame = customtkinter.CTkFrame(self.__left_frame)
self.__search_entry = customtkinter.CTkEntry(self.__search_frame, placeholder_text="search")
self.__search_entry.pack(fill="x", side="left", expand=1)
self.__search_frame.pack(fill="x", pady=5)
self.__scrollbar = customtkinter.CTkScrollbar(self.__left_frame)
self.__scrollbar.pack(side="left", fill="y")
self.__music_listbox = Listbox(
self.__left_frame,
bg="white",
fg="gray",
height=80,
width=60,
selectbackground="black",
selectforeground="white",
yscrollcommand=self.__scrollbar.set,
)
self.__music_listbox.pack(fill="x")
self.__scrollbar.configure(command=self.__music_listbox.yview)
# insert all title on listbox
for music_title in self.__all_mp3_title:
self.__music_listbox.insert("end", music_title)
self.__left_frame.pack(pady=20, side="left", fill="y", padx=5)
self.__right_frame = customtkinter.CTkFrame(self.__container)
self.__titre_text = StringVar()
self.__titre = customtkinter.CTkLabel(self.__right_frame, textvariable=self.__titre_text).pack(pady=50)
self.__image_music = customtkinter.CTkLabel(self.__right_frame, text="", image=self.__album_image)
self.__image_music.pack()
self.__right_frame_bottom = customtkinter.CTkFrame(self.__right_frame)
self.__frame_center_button = customtkinter.CTkFrame(
self.__right_frame_bottom)
Btn(
self.__frame_center_button,
text=None,
relief="groove",
borderwidth=0,
command=self.prev,
image=self.__prev_image,
).pack(side="left")
self.__play_button = Btn(
self.__frame_center_button,
text="",
relief="groove",
borderwidth=0,
command=self.play,
image=self.__play_image,
)
self.__play_button.pack(padx=10, side="left")
Btn(
self.__frame_center_button,
text="",
relief="groove",
borderwidth=0,
command=self.next,
image=self.__text_image,
).pack(side="left")
self.__frame_center_button.pack(anchor="center")
self.__right_frame_bottom.pack(side="bottom", pady=10)
self.scale = customtkinter.CTkSlider(
self.__right_frame, command=lambda x: (self.change_position(x))
)
self.scale.pack(side="bottom", fill="x", ipadx=150, pady=10)
self.__right_frame.pack(side="right", fill="y", expand=1, padx=5)
self.__container.pack(fill="y", anchor="sw")
self.__music_listbox.bind(
"<Double-Button-1>", lambda x: self.select_item_in_listbox(x))
self.__search_entry.bind("<KeyRelease>", self.search)
self.play_time()
def music_now(self, index) -> str:
key = self.__music_listbox.get(index)
return self.__pathMp3Dict[key]
def update_title_and_image(self, index) -> None:
titre, _, artist, image = lecteur.get_tags(self.music_now(index))
if image is not None:
with open(".tmp.jpg", "wb") as file:
file.write(image)
image = customtkinter.CTkImage(
Image.open(".tmp.jpg"), size=(250, 230))
else:
image = self.__album_image
self.__image_music.configure(image=image)
self.__titre_text.set(f"{titre} {artist}")
def change_position(self, position: float):
lecteur.set_position(position)
self.__position = position
def play_time(self) -> None:
# update scale postion
if self.__play_status:
self.__position += 1
_, time_left = lecteur.get_position()
self.scale.set(self.__position)
if time_left == "59:59":
self.next()
self.after(1000, self.play_time)
def play(self, index: int | str = "active") -> None:
self.__play_status = True
total_len_music = len(self.__music_listbox.get(0, "end"))
if index == total_len_music:
index = 0
song = self.music_now(index)
self.scale._to = lecteur.play(song)
self.scale.set(0)
self.__position = 0
self.__play_button.config(image=self.__pause_image, command=self.pause)
self.__music_listbox.selection_clear(0, "end")
self.__music_listbox.activate(index)
self.__music_listbox.selection_set(index, last=None)
self.__activate_now: int = self.__music_listbox.curselection()[0]
self.__music_listbox.yview(
"moveto", (self.__activate_now - self.__scrolloff) /
total_len_music,
) # update yview
self.update_title_and_image(self.__activate_now)
def pause(self) -> None:
if self.__play_status:
self.__play_button.config(image=self.__play_image)
else:
self.__play_button.config(image=self.__pause_image)
self.__play_status: bool = lecteur.pause()
def next(self) -> None:
self.__activate_now += 1
self.play(self.__activate_now)
def prev(self) -> None:
self.__activate_now -= 1
self.play(self.__activate_now)
def select_item_in_listbox(self, e: str) -> None:
self.play()
def update_music_listbox(self, data: list[str]) -> None:
self.__music_listbox.delete(0, "end")
for result in data:
self.__music_listbox.insert("end", result)
def search(self, e: str) -> None:
search_output = self.__search_entry.get()
search_result = [
title
for title in self.__all_mp3_title
if search_output.lower() in title.lower()
]
self.update_music_listbox(search_result)
if __name__ == "__main__":
app = MusicPlayer()
app.mainloop()