-
Notifications
You must be signed in to change notification settings - Fork 0
/
sidebar.py
110 lines (89 loc) · 4.35 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import tkinter as tk
from tkinter import ttk
class Sidebar:
def __init__(self, root, width, pos):
self.root = root
self.sbar_frame = tk.Frame(self.root, bg='gray', width=width)
self.sbar_frame.pack(side=pos, fill='y')
self.sbar_frame.pack_propagate(False) # Prevent automatic resizing
self.frame = tk.Frame(self.sbar_frame, width=width, bg='gray')
self.frame.pack(side='right', fill='y')
self.frame.pack_propagate(False) # Prevent automatic resizing
self.widgets = {}
self.is_hidden = False # Track the visibility of the sidebar
def add_widget(self, widget, canhide=True):
# Add space between each widget with an empty frame
spacer = tk.Frame(self.frame, bg='gray', height=5)
spacer.pack(side='top', fill='x')
master_frame = tk.Frame(self.frame, bd=1, relief='solid')
master_frame.pack(side='top', fill='x')
if canhide:
hide_show_frame = tk.Frame(master_frame)
hide_show_frame.pack(side='top', fill="x")
widget_frame = tk.Frame(master_frame)
widget_frame.pack(side='bottom', expand=True, fill='x')
if canhide:
hide_button = tk.Button(hide_show_frame, text='Hide', font=('Arial', 10), bg='gray', fg='white', command=lambda widget_frame=widget_frame: self.toggle_widget(widget_frame), bd=1, relief='solid')
hide_button.pack(fill='x', )
# Bind <Enter> and <Leave> events to change button color on hover
def on_hover(event):
event.widget.config(bg='#505050', fg='white')
def on_leave(event):
event.widget.config(bg='gray', fg='white')
hide_button.bind('<Enter>', on_hover)
hide_button.bind('<Leave>', on_leave)
widget.pack(in_=widget_frame, side='top', fill='x')
widget.pack_propagate(False)
if canhide:
self.widgets[widget_frame] = (widget, hide_button, False)
# self.widgets[widget_frame] = (widget, hide_button, False)
def remove_widget(self, widget):
# Remove a widget from the sidebar
for widget_frame, (widget, hide_button, hidden) in self.widgets.items():
if widget == widget_frame.winfo_children()[0]:
widget_frame.pack_forget()
del self.widgets[widget_frame]
break
def toggle_widget(self, widget_frame):
# Toggle visibility of a widget in the sidebar
widget, hide_button, hidden = self.widgets[widget_frame]
if hidden:
widget_frame.pack(side='left', expand=True, fill='x')
hide_button.config(text='Hide')
self.widgets[widget_frame] = (widget, hide_button, False)
else:
widget_frame.pack_forget()
hide_button.config(text='Show')
self.widgets[widget_frame] = (widget, hide_button, True)
def toggle_sidebar(self):
# Toggle visibility of the sidebar
if self.is_hidden:
self.frame.pack(side='right', fill='y')
self.is_hidden = False
self.hide_show_button.config(text='Hide Sidebar')
else:
self.frame.pack_forget()
self.is_hidden = True
self.hide_show_button.config(text='Show Sidebar')
if __name__ == '__main__':
root = tk.Tk()
root.title('Sidebar Example')
WIDTH = 300
root.geometry(str(WIDTH) + "x600")
sidebar = Sidebar(root, WIDTH, 'right')
label = tk.Label(text='My Label', font=('Arial', 12), bg='white')
button = tk.Button(text='Click me', font=('Arial', 12), bg='white', command=lambda: print('Button clicked!'))
top_tree = ttk.Treeview(columns=('Attributes', 'Data'), height=15)
top_tree['show'] = 'headings'
# top_tree.pack(fill='both', expand=True)
# Set column headings
top_tree.heading('Attributes', text='Attributes', anchor='center')
top_tree.heading('Data', text='Data', anchor='center')
top_tree.column('#1', width=int(WIDTH/2)-1, stretch = False)
top_tree.column('#2', width=int(WIDTH/2)-1, stretch = False)
top_tree.bind('<Motion>', 'break')
sidebar.add_widget(label)
sidebar.add_widget(button)
sidebar.add_widget(top_tree, canhide=False)
# top_bar.pack_propagate(False)
root.mainloop()