Skip to content

Commit 7fd66f5

Browse files
committed
fix scollbar
1 parent dc29a7b commit 7fd66f5

File tree

1 file changed

+55
-43
lines changed

1 file changed

+55
-43
lines changed

fileListApp.py

Lines changed: 55 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,26 @@
66
from open import Open
77

88
class FileListApp:
9-
sdk: SourceSDK
10-
root: tk.Tk
11-
129
def __init__(self, sourceSDK, root):
1310
self.sdk = sourceSDK
1411
self.root = root
1512
self.current_folder = self.sdk.selected_folder
16-
self.firstfolder = self.sdk.selected_folder
13+
self.first_folder = self.sdk.selected_folder
1714
self.thumbnails = {}
1815

1916
self.create_widgets()
2017
self.load_files(self.current_folder)
2118

2219
def create_widgets(self):
20+
self.up_button = ttk.Button(self.root, text="Up", command=self.go_up)
21+
self.up_button.pack(side="top", pady=5)
22+
23+
self.open_dir_button = ttk.Button(self.root, text="Open Directory", command=self.open_directory)
24+
self.open_dir_button.pack(side="top", pady=5)
25+
2326
self.canvas = tk.Canvas(self.root, bg='white')
2427
self.scroll_y = ttk.Scrollbar(self.root, orient="vertical", command=self.canvas.yview)
28+
self.canvas.configure(yscrollcommand=self.scroll_y.set)
2529

2630
self.scroll_frame = ttk.Frame(self.canvas)
2731
self.scroll_frame.bind(
@@ -32,30 +36,33 @@ def create_widgets(self):
3236
)
3337

3438
self.canvas.create_window((0, 0), window=self.scroll_frame, anchor="nw")
35-
self.canvas.configure(yscrollcommand=self.scroll_y.set)
36-
37-
self.up_button = ttk.Button(self.root, text="Up", command=self.go_up)
38-
self.up_button.pack(pady=5)
39-
40-
self.open_dir_button = ttk.Button(self.root, text="Open Directory", command=self.open_directory)
41-
self.open_dir_button.pack(pady=5)
4239

4340
self.canvas.pack(side="left", fill="both", expand=True)
4441
self.scroll_y.pack(side="right", fill="y")
4542

43+
# Bind mouse wheel events to the canvas
44+
self.canvas.bind("<Enter>", self.bind_mouse_wheel)
45+
self.canvas.bind("<Leave>", self.unbind_mouse_wheel)
46+
47+
def bind_mouse_wheel(self, event):
48+
self.canvas.bind_all("<MouseWheel>", self.on_mouse_wheel)
49+
50+
def unbind_mouse_wheel(self, event):
51+
self.canvas.unbind_all("<MouseWheel>")
52+
53+
def on_mouse_wheel(self, event):
54+
self.canvas.yview_scroll(int(-1*(event.delta/120)), "units")
55+
4656
def load_files(self, folder):
4757
for widget in self.scroll_frame.winfo_children():
4858
widget.destroy()
4959

5060
self.current_folder = folder
51-
self.files = [f for f in os.listdir(folder) if os.path.isdir(os.path.join(folder, f)) or f.endswith((
52-
".vmf", ".txt", ".cfg", ".vtf", ".vmt", ".qc", ".mdl", ".vcd", ".res", ".bsp", "dir.vpk", ".tga", ".wav", ".mp3", ".sln"))]
61+
self.files = [f for f in os.listdir(folder) if os.path.isdir(os.path.join(folder, f)) or f.endswith(
62+
(".vmf", ".txt", ".cfg", ".vtf", ".vmt", ".qc", ".mdl", ".vcd", ".res", ".bsp", "dir.vpk", ".tga", ".wav", ".mp3", ".sln"))]
5363

54-
columns = int(self.root.winfo_width() / 150)
55-
if columns < 1:
56-
columns = 1
57-
row = 0
58-
col = 0
64+
columns = max(1, int(self.root.winfo_width() / 150))
65+
row = col = 0
5966

6067
for file in self.files:
6168
file_path = os.path.join(self.current_folder, file)
@@ -73,15 +80,14 @@ def load_files(self, folder):
7380
thumbnail_label.place(relx=0.5, rely=0.55, anchor='center')
7481

7582
if os.path.isdir(file_path):
76-
frame.bind("<Double-Button-1>", lambda e, path=file_path: self.load_files(path))
77-
label.bind("<Double-Button-1>", lambda e, path=file_path: self.load_files(path))
78-
if thumbnail:
79-
thumbnail_label.bind("<Double-Button-1>", lambda e, path=file_path: self.load_files(path))
83+
bind_func = lambda e, path=file_path: self.load_files(path)
8084
else:
81-
frame.bind("<Double-Button-1>", lambda e, path=file_path: self.open_file(path))
82-
label.bind("<Double-Button-1>", lambda e, path=file_path: self.open_file(path))
83-
if thumbnail:
84-
thumbnail_label.bind("<Double-Button-1>", lambda e, path=file_path: self.open_file(path))
85+
bind_func = lambda e, path=file_path: self.open_file(path)
86+
87+
frame.bind("<Double-Button-1>", bind_func)
88+
label.bind("<Double-Button-1>", bind_func)
89+
if thumbnail:
90+
thumbnail_label.bind("<Double-Button-1>", bind_func)
8591

8692
col += 1
8793
if col >= columns:
@@ -95,24 +101,30 @@ def load_thumbnail(self, file_path):
95101
image = None
96102
base_path = os.path.dirname(os.path.abspath(__file__))
97103

98-
if file_path.endswith(".vtf"):
99-
image = Image.open(os.path.join(base_path, "icons", "VTFEdit.png"))
100-
elif file_path.endswith(".mdl"):
101-
image = Image.open(os.path.join(base_path, "icons", "hlmv.png"))
102-
elif file_path.endswith(".tga"):
103-
image = Image.open(file_path)
104-
elif file_path.endswith(".vmf"):
105-
image = Image.open(os.path.join(base_path, "icons", "hammer.png"))
106-
elif file_path.endswith(".vcd"):
107-
image = Image.open(os.path.join(base_path, "icons", "hlposer.png"))
108-
elif file_path.endswith(".bsp"):
109-
image = Image.open(os.path.join(base_path, "icons", "source.png"))
104+
file_icons = {
105+
".vtf": "VTFEdit.png",
106+
".mdl": "hlmv.png",
107+
".tga": None,
108+
".vmf": "hammer.png",
109+
".vcd": "hlposer.png",
110+
".bsp": "source.png",
111+
".txt": "txt.png",
112+
".res": "txt.png",
113+
".vmt": "txt.png",
114+
".qc": "txt.png",
115+
".smd": "txt.png",
116+
".cfg": "txt.png",
117+
".sln": "Visual_Studio.png"
118+
}
119+
120+
ext = os.path.splitext(file_path)[1]
121+
if ext in file_icons:
122+
if file_icons[ext]:
123+
image = Image.open(os.path.join(base_path, "icons", file_icons[ext]))
124+
else:
125+
image = Image.open(file_path)
110126
elif os.path.isdir(file_path):
111127
image = Image.open(os.path.join(base_path, "icons", "fileexplorer.png"))
112-
elif file_path.endswith(".txt") or file_path.endswith(".res") or file_path.endswith(".vmt") or file_path.endswith(".qc") or file_path.endswith(".smd") or file_path.endswith(".cfg"):
113-
image = Image.open(os.path.join(base_path, "icons", "txt.png"))
114-
elif file_path.endswith(".sln"):
115-
image = Image.open(os.path.join(base_path, "icons", "Visual_Studio.png"))
116128

117129
if image:
118130
image.thumbnail((50, 50))
@@ -126,7 +138,7 @@ def load_thumbnail(self, file_path):
126138

127139
def go_up(self):
128140
parent_dir = os.path.dirname(self.current_folder)
129-
if parent_dir and self.current_folder != self.firstfolder:
141+
if parent_dir and self.current_folder != self.first_folder:
130142
self.load_files(parent_dir)
131143

132144
def open_directory(self):

0 commit comments

Comments
 (0)