Working version of module inspector.

This commit is contained in:
cheezedoodle2
2023-04-19 20:49:15 -04:00
committed by Sloetjes
parent 31278dbc6e
commit 9445050af1
+39
View File
@@ -0,0 +1,39 @@
import sys
import tkinter as tk
from tkinter import ttk
def on_treeview_heading_click(column, tree):
data = [(tree.set(child, column), child) for child in tree.get_children("")]
reverse = (column == tree.heading(column)["text"]) and not column.startswith("_")
data.sort(reverse=reverse, key=lambda t: t[0].lower())
for index, (val, child_id) in enumerate(data):
tree.move(child_id, "", index)
tree.heading(column, text=("_" if not tree.heading(column, option="text").startswith("_") else "") + column)
root = tk.Tk()
root.title('Module explorer')
root.geometry('640x480')
tree = ttk.Treeview(root, columns=('module', 'path'))
tree.heading('module', text='Module Name', anchor=tk.W)
tree.heading('path', text='Path', anchor=tk.W)
for module in sys.modules:
if hasattr(sys.modules[module], '__spec__') and hasattr(sys.modules[module].__spec__, 'origin'):
origin = sys.modules[module].__spec__.origin
else:
origin = "None"
iid = tree.insert('', tk.END, values=(module, origin))
tree.grid(row=0, column=0, sticky='nsew')
root.grid_rowconfigure(0, weight=1)
root.grid_columnconfigure(0, weight=1)
tree.column('#0', width=20, stretch=False)
tree.tag_configure("selected", background="#ccc")
# Call the on_treeview_heading_click function when a column heading is clicked
for col in tree["columns"]:
tree.heading(col, text=col, anchor=tk.W, command=lambda col=col: on_treeview_heading_click(col, tree))
root.mainloop()