Files
python-bootcamp/module-inspector/module-inspector.py
T
2023-04-19 20:49:15 -04:00

44 lines
1.6 KiB
Python

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', 'loader'))
tree.heading('module', text='Module Name', anchor=tk.W)
tree.heading('path', text='Path', anchor=tk.W)
tree.heading('loader', text='Loader', 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"
if hasattr(sys.modules[module], '__loader__'):
loader = sys.modules[module].__loader__
else:
loader = "None"
iid = tree.insert('', tk.END, values=(module, origin, loader))
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()