-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsidebar.py
88 lines (78 loc) · 2.94 KB
/
sidebar.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
from tkinter import Frame, Button
from functools import partial
import ui.images as img
class Sidebar(Frame):
"""
A custom Tkinter frame representing a sidebar with buttons.
Args:
master (Tk): The parent Tkinter window or frame.
bg (str): The background color of the sidebar.
activebg (str): The background color when a button is active.
draw_files (callable): A function to handle drawing files based on the selected button.
Attributes:
buttons (dict): A dictionary to store the buttons.
Methods:
button_clicked(button): Handles the button click event.
"""
def __init__(self, master, bg, activebg, draw_files, **kwargs):
"""
Initializes the Sidebar.
Args:
master (Tk): The parent Tkinter window or frame.
bg (str): The background color of the sidebar.
activebg (str): The background color when a button is active.
draw_files (callable): A function to handle drawing files based on the selected button.
**kwargs: Additional keyword arguments to be passed to the Frame constructor.
"""
kwargs['bg'] = bg
super().__init__(master, **kwargs)
self.draw_files = draw_files
# dictionary to store buttons
self.buttons = {}
buttons = [
('Home', img.home),
('Desktop', img.desktop),
('Downloads', img.downloads),
('Documents', img.documents),
('Pictures', img.pictures),
('Music', img.music),
('Videos', img.videos),
]
for button_text, button_image in buttons:
button = Button(
self,
text=button_text,
image=button_image,
activeforeground='white',
compound='left',
bg=bg,
fg='white',
activebackground=activebg,
relief='sunken',
bd=0,
highlightthickness=0,
cursor='hand2',
anchor='w',
width=170,
)
button.pack(side='top', padx=0, pady=0, fill='x')
button.selected = False
button.config(command=partial(self.button_clicked, button))
self.buttons[button_text] = button
def button_clicked(self, button):
"""
Handles the button click event.
Args:
button (Button): The button that was clicked.
"""
for b in self.buttons.values():
if b == button:
b.config(bg='#404040')
b.selected = True
else:
b.config(bg=self['bg'], activebackground='#3a3a3a')
b.selected = False
# Perform actions based on active button
print(f"{button['text']} button clicked!")
# Calling the draw_files function with button text
self.draw_files(button['text'])