-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathView.py
647 lines (554 loc) · 26.3 KB
/
View.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
import sys
import tkinter as tk
import tkinter.ttk as ttk
import player
import random
import sys
import threading
import time
from tkinter import filedialog,messagebox
from cx_Oracle import DatabaseError
from pygame import mixer
import tkinter as tk
import tkinter.ttk as ttk
from myexceptions import *
import musicplayer_support
def vp_start_gui():
'''Starting point when module is the main routine.'''
global val, w, root
root = tk.Tk()
top = view (root)
musicplayer_support.init(root, top)
root.mainloop()
class view:
def __init__(self, top=None):
'''This class configures and populates the toplevel window.
top is the toplevel containing window.'''
_bgcolor = '#d9d9d9' # X11 color: 'gray85'
_fgcolor = '#000000' # X11 color: 'black'
_compcolor = '#d9d9d9' # X11 color: 'gray85'
_ana1color = '#d9d9d9' # X11 color: 'gray85'
_ana2color = '#ececec' # Closest X11 color: 'gray92'
font11 = "-family {Avenir Next Cyr Medium} -size 23 -weight " \
"normal -slant roman -underline 0 -overstrike 0"
font12 = "-family {Avenir Next Cyr} -size 9 -weight bold " \
"-slant roman -underline 0 -overstrike 0"
font13 = "-family {Vivaldi} -size 22 -weight " \
"bold -slant roman -underline 0 -overstrike 0"
self.style = ttk.Style()
if sys.platform == "win32":
self.style.theme_use('winnative')
self.style.configure('.',background=_bgcolor)
self.style.configure('.',foreground=_fgcolor)
self.style.configure('.',font="TkDefaultFont")
self.style.map('.',background=
[('selected', _compcolor), ('active',_ana2color)])
top.geometry("687x526+558+155")
top.title("New Toplevel")
top.configure(background="#fff")
self.top=top
self.songName = tk.Label(top)
self.songName.place(relx=0.437, rely=0.038, height=44, width=281)
self.songName.configure(background="#fff")
self.songName.configure(disabledforeground="#a3a3a3")
self.songName.configure(font=font13)
self.songName.configure(foreground="#000000")
self.songName.configure(text='''MY MUSIC''')
self.songProgress = ttk.Progressbar(top)
self.songProgress.place(relx=0.393, rely=0.209, relwidth=0.495
, relheight=0.0, height=7)
self.songTotalDuration = ttk.Label(top)
self.songTotalDuration.place(relx=0.844, rely=0.171, height=19, width=29)
self.songTotalDuration.configure(background="#fff")
self.songTotalDuration.configure(foreground="#3399ff")
self.songTotalDuration.configure(font=font12)
self.songTotalDuration.configure(relief='flat')
self.songTimePassed = ttk.Label(top)
self.songTimePassed.place(relx=0.393, rely=0.171, height=19, width=29)
self.songTimePassed.configure(background="#ffffff")
self.songTimePassed.configure(foreground="#000000")
self.songTimePassed.configure(font=font12)
self.songTimePassed.configure(relief='flat')
self.pauseButton = tk.Button(top)
self.pauseButton.place(relx=0.568, rely=0.266, height=34, width=34)
self.pauseButton.configure(activebackground="#ececec")
self.pauseButton.configure(activeforeground="#000000")
self.pauseButton.configure(background="#fff")
self.pauseButton.configure(borderwidth="0")
self.pauseButton.configure(disabledforeground="#a3a3a3")
self.pauseButton.configure(foreground="#000000")
self.pauseButton.configure(highlightbackground="#d9d9d9")
self.pauseButton.configure(highlightcolor="black")
self._img1 = tk.PhotoImage(file="./icons/pause.png")
self.pauseButton.configure(image=self._img1)
self.pauseButton.configure(pady="0")
self.pauseButton.configure(text='''Button''')
self.playButton = tk.Button(top)
self.playButton.place(relx=0.64, rely=0.266, height=34, width=34)
self.playButton.configure(activebackground="#ececec")
self.playButton.configure(activeforeground="#000000")
self.playButton.configure(background="#fff")
self.playButton.configure(borderwidth="0")
self.playButton.configure(disabledforeground="#a3a3a3")
self.playButton.configure(foreground="#000000")
self.playButton.configure(highlightbackground="#d9d9d9")
self.playButton.configure(highlightcolor="black")
self._img2 = tk.PhotoImage(file="./icons/play.png")
self.playButton.configure(image=self._img2)
self.playButton.configure(pady="0")
self.playButton.configure(text='''Button''')
self.stopButton = tk.Button(top)
self.stopButton.place(relx=0.713, rely=0.266, height=34, width=34)
self.stopButton.configure(activebackground="#ececec")
self.stopButton.configure(activeforeground="#000000")
self.stopButton.configure(background="#fff")
self.stopButton.configure(borderwidth="0")
self.stopButton.configure(disabledforeground="#a3a3a3")
self.stopButton.configure(foreground="#000000")
self.stopButton.configure(highlightbackground="#d9d9d9")
self.stopButton.configure(highlightcolor="black")
self._img3 = tk.PhotoImage(file="./icons/stop.png")
self.stopButton.configure(image=self._img3)
self.stopButton.configure(pady="0")
self.stopButton.configure(text='''Button''')
self.vinylRecordImage = tk.Label(top)
self.vinylRecordImage.place(relx=0.0, rely=0.0, height=204, width=204)
self.vinylRecordImage.configure(background="#d9d9d9")
self.vinylRecordImage.configure(disabledforeground="#a3a3a3")
self.vinylRecordImage.configure(foreground="#000000")
self._img4 = tk.PhotoImage(file="./icons/vinylrecord.png")
self.vinylRecordImage.configure(image=self._img4)
self.vinylRecordImage.configure(text='''Label''')
self.playList = ScrolledListBox(top)
self.playList.place(relx=0.0, rely=0.38, relheight=0.532, relwidth=0.999)
self.playList.configure(background="white")
self.playList.configure(disabledforeground="#a3a3a3")
self.playList.configure(font="TkFixedFont")
self.playList.configure(foreground="black")
self.playList.configure(highlightbackground="#d9d9d9")
self.playList.configure(highlightcolor="#d9d9d9")
self.playList.configure(selectbackground="#c4c4c4")
self.playList.configure(selectforeground="black")
self.playList.configure(width=10)
self.previousButton = tk.Label(top)
self.previousButton.place(relx=0.509, rely=0.285, height=16, width=16)
self.previousButton.configure(background="#fff")
self.previousButton.configure(borderwidth="0")
self.previousButton.configure(disabledforeground="#a3a3a3")
self.previousButton.configure(foreground="#000000")
self._img5 = tk.PhotoImage(file="./icons/previous.png")
self.previousButton.configure(image=self._img5)
self.previousButton.configure(text='''Label''')
self.bottomBar = ttk.Label(top)
self.bottomBar.place(relx=0.0, rely=0.913, height=49, width=686)
self.bottomBar.configure(background="#d9d9d9")
self.bottomBar.configure(foreground="#000000")
self.bottomBar.configure(font="TkDefaultFont")
self.bottomBar.configure(relief='flat')
self.bottomBar.configure(width=686)
self.bottomBar.configure(state='disabled')
self.vol_scale = ttk.Scale(top)
self.vol_scale.place(relx=0.015, rely=0.932, relwidth=0.146, relheight=0.0
, height=26, bordermode='ignore')
self.vol_scale.configure(takefocus="")
self.addSongsToPlayListButton = tk.Button(top)
self.addSongsToPlayListButton.place(relx=0.961, rely=0.323, height=17
, width=17)
self.addSongsToPlayListButton.configure(activebackground="#ececec")
self.addSongsToPlayListButton.configure(activeforeground="#d9d9d9")
self.addSongsToPlayListButton.configure(background="#fff")
self.addSongsToPlayListButton.configure(borderwidth="0")
self.addSongsToPlayListButton.configure(disabledforeground="#a3a3a3")
self.addSongsToPlayListButton.configure(foreground="#000000")
self.addSongsToPlayListButton.configure(highlightbackground="#d9d9d9")
self.addSongsToPlayListButton.configure(highlightcolor="black")
self._img6 = tk.PhotoImage(file="./icons/add.png")
self.addSongsToPlayListButton.configure(image=self._img6)
self.addSongsToPlayListButton.configure(pady="0")
self.addSongsToPlayListButton.configure(text='''Button''')
self.deleteSongsFromPlaylistButton = tk.Button(top)
self.deleteSongsFromPlaylistButton.place(relx=0.917, rely=0.323
, height=18, width=18)
self.deleteSongsFromPlaylistButton.configure(activebackground="#ececec")
self.deleteSongsFromPlaylistButton.configure(activeforeground="#000000")
self.deleteSongsFromPlaylistButton.configure(background="#fff")
self.deleteSongsFromPlaylistButton.configure(borderwidth="0")
self.deleteSongsFromPlaylistButton.configure(disabledforeground="#a3a3a3")
self.deleteSongsFromPlaylistButton.configure(foreground="#000000")
self.deleteSongsFromPlaylistButton.configure(highlightbackground="#d9d9d9")
self.deleteSongsFromPlaylistButton.configure(highlightcolor="black")
self._img7 = tk.PhotoImage(file="./icons/delete.png")
self.deleteSongsFromPlaylistButton.configure(image=self._img7)
self.deleteSongsFromPlaylistButton.configure(pady="0")
self.deleteSongsFromPlaylistButton.configure(text='''Button''')
self.Button9 = tk.Button(top)
self.Button9.place(relx=0.932, rely=0.913, height=42, width=42)
self.Button9.configure(activebackground="#ececec")
self.Button9.configure(activeforeground="#000000")
self.Button9.configure(background="#d9d9d9")
self.Button9.configure(borderwidth="0")
self.Button9.configure(disabledforeground="#a3a3a3")
self.Button9.configure(foreground="#000000")
self.Button9.configure(highlightbackground="#d9d9d9")
self.Button9.configure(highlightcolor="black")
self._img8 = tk.PhotoImage(file="./icons/like.png")
self.Button9.configure(image=self._img8)
self.Button9.configure(pady="0")
self.Button9.configure(text='''Button''')
self.Button9.configure(width=42)
self.Button10 = tk.Button(top)
self.Button10.place(relx=0.873, rely=0.913, height=42, width=42)
self.Button10.configure(activebackground="#ececec")
self.Button10.configure(activeforeground="#000000")
self.Button10.configure(background="#d9d9d9")
self.Button10.configure(borderwidth="0")
self.Button10.configure(disabledforeground="#a3a3a3")
self.Button10.configure(foreground="#000000")
self.Button10.configure(highlightbackground="#d9d9d9")
self.Button10.configure(highlightcolor="black")
self._img9 = tk.PhotoImage(file="./icons/broken-heart.png")
self.Button10.configure(image=self._img9)
self.Button10.configure(pady="0")
self.Button10.configure(text='''Button''')
self.Button10.configure(width=48)
self.Button11 = tk.Button(top)
self.Button11.place(relx=0.83, rely=0.932, height=26, width=26)
self.Button11.configure(activebackground="#ececec")
self.Button11.configure(activeforeground="#000000")
self.Button11.configure(background="#d9d9d9")
self.Button11.configure(borderwidth="0")
self.Button11.configure(disabledforeground="#a3a3a3")
self.Button11.configure(foreground="#000000")
self.Button11.configure(highlightbackground="#d9d9d9")
self.Button11.configure(highlightcolor="black")
self._img10 = tk.PhotoImage(file="./icons/refresh.png")
self.Button11.configure(image=self._img10)
self.Button11.configure(pady="0")
self.Button11.configure(text='''Button''')
self.setup_player()
def setup_player(self):
try:
self.my_player=player.Player()
if self.my_player.get_db_status():
messagebox.showinfo("Suuccess!","connected successfully to the Db!!")
else:
raise Exception("sorry You can not or save favourites")
except Exception as ex:
messagebox.showerror("db Error ",ex)
self.Button9.configure(state="disabled")
self.Button10.configure(state="diabled")
self.Button11.configure(state="disabled")
self.vol_scale.configure(from_=0,to=100,command=self.change_volume)
self.vol_scale.set(50)
self.addSongsToPlayListButton.configure(command=self.add_song)
self.deleteSongsFromPlaylistButton.configure(command=self.remove_song)
self.playButton.configure(command=self.play_song)
self.stopButton.configure(command=self.stop_song)
self.pauseButton.configure(command=self.pause_song)
self.playList.configure(font="vivaldi 12")
self.playList.bind("<Double-1>",self.list_double_click)
self.previousButton.bind("<Button>",self.load_previous_song)
img=tk.PhotoImage(file="./Icons/like.png")
self.top.title("MOUZIKKA-Dance to the rhythem of your Heart")
self.top.protocol("WM_DELETE_WINDOW",self.closewindow)
self.Button10.configure(command=self.remove_song_from_favourites)
self.Button9.configure(command=self.add_song_to_favourites)
self.Button11.configure(command=self.load_songs_from_favourites)
self.isPaused=False
self.isPlaying=False
self.isThreadRunning=False
# self.playList.bind("<Button>",self.load_previous_song)
def change_volume(self,val):
volume_level=float(val)/100
self.my_player.set_volume(volume_level)
def add_song(self):
song_name=self.my_player.add_song()
if song_name is None:
return
self.playList.insert(tk.END,song_name)
rcolor=lambda: random.randint(0,255)
red=hex(rcolor())
green=hex(rcolor())
blue=hex(rcolor())
red=red[2:]
green=green[2:]
blue=blue[2:]
if len(red)==1:
red="0"+red
if len(green)==1:
green="0"+green
if len(blue)==1:
blue="0"+blue
mycolor="#"+red+green+blue
print(red,green,blue)
print(mycolor)
self.playList.configure(fg=mycolor)
def show_song_details(self):
self.song_length=self.my_player.get_song_length(self.song_name)
min,sec=divmod(self.song_length,60)
min=round(min)
sec=round(sec)
self.songTotalDuration.configure(text=str(min)+':'+str(sec))
self.songTimePassed.configure(text="0:0")
ext_index=self.song_name.rfind(".")
song_name_str=self.song_name[0:ext_index]
if(len(song_name_str)>14):
song_name_str=song_name_str[0:14]+"..."
self.songName.configure(text=song_name_str)
def play_song(self):
self.sel_song_index_tuple=self.playList.curselection()
try:
if len(self.sel_song_index_tuple)==0:
raise NosongSelectedError("please select song to play")
self.song_name=self.playList.get(self.sel_song_index_tuple[0])
self.show_song_details()
if (self.isThreadRunning==False):
self.isThreadRunning=True
else:
self.my_player.stop_song()
time.sleep(1)
self.my_player.play_song()
self.change_volume(self.vol_scale.get())
self.setup_thread()
self.isPlaying=True
except(NosongSelectedError) as ex1:
messagebox.showerror("Error !",ex1)
def list_double_click(self,e):
self.play_song()
def stop_song(self):
self.my_player.stop_song()
self.isPlaying=False
def pause_song(self):
if self.isPlaying:
if self.isPaused==False:
self.my_player.pause_song()
self.isPaused=True
else:
self.my_player.unpause_song()
self.isPaused=False
def remove_song(self):
self.sel_song_index_tuple=self.playList.curselection()
print(type(self.sel_song_index_tuple))
print(self.sel_song_index_tuple)
try:
if len(self.sel_song_index_tuple)==0:
raise NosongSelectedError("please select song to remove:")
song_name=self.playList.get(self.sel_song_index_tuple[0])
self.playList.delete(self.sel_song_index_tuple[0])
#self.my_player.remove_song(song_name)
except(NosongSelectedError) as ex1:
messagebox.showerror("Error",ex1)
def closewindow(self):
result=messagebox.askyesno("App closing Do You Wantto Quit")
if(result):
self.my_player.close_player()
messagebox.showinfo("Have a Good day","Thank You For Using #MOUZIKKA#")
self.top.destroy()
def load_previous_song(self,e):
try:
if hasattr(self,"sel_song_index_tuple")==False:
raise NosongSelectedError("please select a song first")
self.prev_song_index=self.sel_song_index_tuple[0]-1
if self.prev_song_index==-1:
self.prev_song_index=self.playList.count()-1
self.playList.select_clear(0,tk.END)
self.playList.selection_set(self.prev_song_index)
print("prev song index",self.prev_song_index)
self.play_song()
except(NosongSelectedError) as ex1:
messagebox.showerror("ERRROR!",ex1)
def setup_thread(self):
self.my_thread=threading.Thread(target=self.show_timer,args=(self.song_length,))
self.my_thread.start()
def show_timer(self,total_sec):
curr_sec=0
prev_perc_incr=0
self.songProgress.stop()
while curr_sec<=total_sec and mixer.music.get_busy():
if (self.isPaused):
continue
min,sec=divmod(curr_sec,60)
min=round(min)
sec=round(sec)
if sec<10:
self.songTimePassed.configure(text=str(min)+":"+str(sec))
else:
self.songTimePassed.configure(text=str(min)+":"+str(sec))
curr_sec+=1
percent_incr=curr_sec*100/total_sec
#print(percent_incr)
if int(percent_incr)-int(prev_perc_incr)>=1:
self.songProgress.step()
prev_perc_incr=percent_incr
time.sleep(1)
if abs(curr_sec-int(total_sec))==0 or abs(curr_sec-int(total_sec))==1:
self.load_next_song()
def add_song_to_favourites(self):
fav_song_index_tuple=self.playList.curselection()
try:
if len(fav_song_index_tuple)==0:
raise NosongSelectedError("please select a song to add to favourites")
song_name=self.playList.get(fav_song_index_tuple[0])
result=self.my_player.add_song_to_favourites(song_name)
messagebox.showinfo("success",result)
except(NosongSelectedError) as ex1:
messagebox.showerror("Error ",ex1)
print(ex1)
except(DatabaseError)as ex2:
messagebox.showerror("Db Error ","Song can not be added")
print(ex2)
def load_songs_from_favourites(self):
try:
load_result=self.my_player.load_songs_from_favourites()
result=load_result[0]
if result.find("No songs present")!=-1:
messagebox.showinfo("favourites Empty !!!","no songs in your favourites")
return
song_dict=load_result[1]
self.playList.delete(0,tk.END)
for song_name in song_dict:
self.playList.insert(tk.END,song_name)
print("from db:",song_name)
rcolor=lambda:random.randint(0,255)
red=hex(rcolor())
green=hex(rcolor())
blue=hex(rcolor())
mycolor='#'+red[2:3]+green[2:3]+blue[2:3]
print(red,green,blue)
print(mycolor)
self.playList.configure(fg=mycolor)
messagebox.showinfo("success!!!","List populated from your favourites")
except(DatabaseError)as ex1:
messagebox.showerror("Db Error!","sorry songs can not be loaded from favourites ")
def remove_song_from_favourites(self):
fav_song_index_tuple=self.playList.curselection()
try:
if len(fav_song_index_tuple)==0:
raise NosongSelectedError("please select song")
self.song_name=self.playList.get(fav_song_index_tuple[0])
result=self_name=self.my_player.remove_songs_from_favourites(self.song_name)
messagebox.showinfo("success",result)
except(NosongSelectedError) as ex1:
messagebox.showerror("error",ex1)
print(ex1)
except(DatabaseError) as ex2:
messagebox.showerror("Db Error","song can not be deletd")
print(ex2)
def load_next_song(self):
try:
self.load_next_song_index=self.sel_song_index_tuple[0]+1
if self.load_next_song_index==self.playList.count():
self.load_next_song_index=0
self.playList.select_clear(0,tk.END)
self.playList.selection_set(self.load_next_song_index)
print("next song index:",self.load_next_song_index)
self.isPlaying=True
self.play_song()
except(NosongSelectedError) as ex1:
messagebox.showerror("error ",ex1)
# The following code is added to facilitate the Scrolled widgets you specified.
class AutoScroll(object):
'''Configure the scrollbars for a widget.''' #data member has not formed then atribute error comes
def __init__(self, master):
# Rozen. Added the try-except clauses so that this class
# could be used for scrolled entry widget for which vertical
# scrolling is not supported. 5/7/14.
try:
vsb = ttk.Scrollbar(master, orient='vertical', command=self.yview)
except:
pass
hsb = ttk.Scrollbar(master, orient='horizontal', command=self.xview)
#self.configure(yscrollcommand=_autoscroll(vsb),
# xscrollcommand=_autoscroll(hsb))
try:
self.configure(yscrollcommand=self._autoscroll(vsb))
except:
pass
self.configure(xscrollcommand=self._autoscroll(hsb))
self.grid(column=0, row=0, sticky='nsew')
try:
vsb.grid(column=1, row=0, sticky='ns')
except:
pass
hsb.grid(column=0, row=1, sticky='ew')
master.grid_columnconfigure(0, weight=1)
master.grid_rowconfigure(0, weight=1)
# Copy geometry methods of master (taken from ScrolledText.py)
methods = tk.Pack.__dict__.keys() | tk.Grid.__dict__.keys() \
| tk.Place.__dict__.keys()
for meth in methods:
if meth[0] != '_' and meth not in ('config', 'configure'):
setattr(self, meth, getattr(master, meth))
@staticmethod
def _autoscroll(sbar):
'''Hide and show scrollbar as needed.'''
def wrapped(first, last):
first, last = float(first), float(last)
if first <= 0 and last >= 1:
sbar.grid_remove()
else:
sbar.grid()
sbar.set(first, last)
return wrapped
def __str__(self):
return str(self.master)
def _create_container(func):
'''Creates a ttk Frame with a given master, and use this new frame to
place the scrollbars and the widget.'''
def wrapped(cls, master, **kw):
container = ttk.Frame(master)
container.bind('<Enter>', lambda e: _bound_to_mousewheel(e, container))
container.bind('<Leave>', lambda e: _unbound_to_mousewheel(e, container))
return func(cls, container, **kw)
return wrapped
class ScrolledListBox(AutoScroll, tk.Listbox):
'''A standard Tkinter Text widget with scrollbars that will
automatically show/hide as needed.'''
@_create_container
def __init__(self, master, **kw):
tk.Listbox.__init__(self, master, **kw)
AutoScroll.__init__(self, master)
def count(self):
return tk.Listbox.size(self)
import platform
def _bound_to_mousewheel(event, widget):
child = widget.winfo_children()[0]
if platform.system() == 'Windows' or platform.system() == 'Darwin':
child.bind_all('<MouseWheel>', lambda e: _on_mousewheel(e, child))
child.bind_all('<Shift-MouseWheel>', lambda e: _on_shiftmouse(e, child))
else:
child.bind_all('<Button-4>', lambda e: _on_mousewheel(e, child))
child.bind_all('<Button-5>', lambda e: _on_mousewheel(e, child))
child.bind_all('<Shift-Button-4>', lambda e: _on_shiftmouse(e, child))
child.bind_all('<Shift-Button-5>', lambda e: _on_shiftmouse(e, child))
def _unbound_to_mousewheel(event, widget):
if platform.system() == 'Windows' or platform.system() == 'Darwin':
widget.unbind_all('<MouseWheel>')
widget.unbind_all('<Shift-MouseWheel>')
else:
widget.unbind_all('<Button-4>')
widget.unbind_all('<Button-5>')
widget.unbind_all('<Shift-Button-4>')
widget.unbind_all('<Shift-Button-5>')
def _on_mousewheel(event, widget):
if platform.system() == 'Windows':
widget.yview_scroll(-1*int(event.delta/120),'units')
elif platform.system() == 'Darwin':
widget.yview_scroll(-1*int(event.delta),'units')
else:
if event.num == 4:
widget.yview_scroll(-1, 'units')
elif event.num == 5:
widget.yview_scroll(1, 'units')
def _on_shiftmouse(event, widget):
if platform.system() == 'Windows':
widget.xview_scroll(-1*int(event.delta/120), 'units')
elif platform.system() == 'Darwin':
widget.xview_scroll(-1*int(event.delta), 'units')
else:
if event.num == 4:
widget.xview_scroll(-1, 'units')
elif event.num == 5:
widget.xview_scroll(1, 'units')
if __name__ == '__main__':
vp_start_gui()